Setting Up Docker for Python Projects: A Step-by-Step Guide
Last Updated :
09 Oct, 2024
Docker provides a standardized environment to develop, test and deploy applications in an isolated container ensuring that your code works seamlessly regardless of where it’s run.
Setting Up Docker for Python ProjectsIn this article, we will learn about the basics of Docker and containers, how to set Docker for Python projects, etc.
Introduction to Docker and Containers
Docker is a platform designed to automate the deployment of applications inside a lightweight portable container. These containers include everything the application needs to run such as external libraries, dependencies, databases, and the application code itself. By isolating applications in containers, Docker ensures that they behave the same way across different environments from your local machine to the production server.
A container is a standard unit of software that encapsulates everything needed to run an application including code, runtime, libraries and configurations. Unlike virtual machines, containers share the host system's OS kernel and are much lighter which makes them faster to start and more efficient in resource usage.
Why Use Docker for Python Projects?
- Consistency Across Environments: Docker ensures that your development, staging, and production environments are identical.
- Simplified Dependency Management: Docker containers include all necessary libraries and dependencies making it easier to manage Python environments and avoid version conflicts.
- Isolation: Each python project inside docker container runs in its own isolated environment that prevents conflicts between different applications or services.
- Scalability: Docker containers are lightweight and can be scaled easily.
- Portability: Docker containers can be easily shared and deployed on any system that supports Docker.
Installing Docker
Windows and macOS
Install Docker Desktop by downloading it from Docker's official website. Follow the installation instructions and make sure Docker Desktop is running
Linux
Run the following commands to install Docker on Ubuntu/Debian-based systems:
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
Once installed, verify Docker is working by running:
docker --version
You can refer the below article for installation of docker:
Steps to Setting Up Docker for a Python Projects
Follow the below steps to Step up a docker for python project
Step 1: Create a Python Project
First, create a simple Python project locally and add Python Files: Create a simple Python script inside your project directory. For example: app.py
Python
# app.py
print("Hello, from GFG!")
Step 2: Writing a Dockerfile for Python
A Dockerfile is a text file that contains instructions to build a Docker image which is a snapshot of the environment your project needs to run. For Python projects, a Dockerfile typically defines the base Python image, installs dependencies and sets up the application environment.
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container (Optional, only for web apps)
EXPOSE 80
# Define environment variable (optional)
ENV NAME World
# Run app.py when the container launches
CMD ["python", "./app.py"]
Step 3: Managing Dependencies Inside Docker (optional for our example)
Managing dependencies in Docker is simple because you define them in a requirements.txt file (for Python) and then install them during the image build process.
the following requirements.txt file can specify the dependencies needed for your Python app:
flask==2.0.1
requests==2.25.1
Step 4: Build the Docker Image
Now, build your Docker image based on the Dockerfile. Run the following command from the root of your project directory (where the Dockerfile is located):
docker build -t my-python-app .
Step 5: Deploying Python Applications with Docker
After building the image, you can run your Python app inside a Docker container. Use the following command:
docker run -it --rm --name my-running-app my-python-app
For Applications that is going to continuously run on a PORT, you have to mentions the outside port in DockerFile and use the following command when running the container:
docker run -d -p 5000:5000 --restart unless-stopped flask-app
In this command:
- docker run: This command runs a new container based on the specified image (flask-app in this case).
- -d: The -d flag runs the container in detached mode meaning the container will run in the background.
- -p 5000:5000: This option maps port 5000 of the host (your local machine) to port 5000 inside the container. Flask apps by default, run on port 5000, so this ensures that the Flask app inside the container can be accessed via localhost:5000 on your machine.
- --restart unless-stopped: This flag ensures that the container restarts automatically if it crashes or the Docker daemon restarts. The container will continue to restart unless explicitly stopped using docker stop or another similar command.
- flask-app: This is the name of the Docker image being used to run the container.
Docker Compose for Multi-Container Applications
If your Python project involves multiple services, such as a web application and a database, you can use Docker Compose to manage them in a single YAML configuration file.
Here’s an example docker-compose.yml file for a Python Flask app and PostgreSQL database:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
environment:
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=secret
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
To run this setup, simply execute (where your compose file is present):
docker-compose up
Conclusion
Docker is a powerful tool that provides consistency, portability and ease of deployment across different environments. By containerizing your application, you can ensure that it behaves the same way everywhere from development to production. By following this article, you can easily setup your python projects in docker environment.
Similar Reads
How to Load a Docker Image: Step-by-Step Guide
Docker is essential for managing and deploying apps in containerized environments. One of Docker's core functions is working with images, which are standalone, portable, and executable software packages. The 'docker image load' command is a crucial tool for loading Docker images from a tarball into
4 min read
Top 10 Docker Projects Ideas for Beginners [2025]
Docker is a revolutionary tool used in the software world for developing, packaging, deploying, and managing applications efficiently. It is going to become the most demanding technology in 2025 and having projects of docker in skill bank is more valuable for developers. This article will provide yo
10 min read
How to Run a Python Script using Docker?
Docker helps you to run your Python application very smoothly in different environments without worrying about underlying platforms. Once you build an image using dockerfile you can run that image wherever you want to run. Docker image will help you to package all the dependencies required for the a
8 min read
How to Use Docker For Local Development and Testing?
Whenever you want to start a project you first check for the requirement i.e. we do the requirement analysis then we look for prerequisites dependencies. We install those dependencies and prerequisites and we simply built our project. Suppose you are the person who built the project and you want to
3 min read
How to Generate a Dockerfile from an Image?
When working with Docker, you may sometimes need to recreate or reverse-engineer a Dockerfile from an existing image. While Docker doesnât offer a direct method to generate a Dockerfile from an image, you can still extract useful information from the imageâs layers and commands. This guide will walk
5 min read
Log in to Docker for Pulling and Pushing Images
Docker is a platform that provides a set of PaaS ( Platform as a Service ) products that help developers containerize applications, to run them consistently in all supported environments. Docker utilizes virtualization at the OS-level to deliver software in containers. At its core Docker utilizes Do
4 min read
Configuring HTTPD server on Docker Container and setting up Python Interpreter
In this article, we are going to discuss a step-by-step guide on how to configure the apache web server on top of a docker container, and setting up a Python interpreter. Basically, we are installing the product of apache which is HTTPD on the docker container. What is Docker? In simple words, if I
4 min read
Top Hosting Platforms For Python Projects
It feels great when you see your work being viewed and praised by everyone. Right? Hosting gives you such an offer where you can see your project working live on the Internet. You can host your website free or paid depending on your convenience. What is Hosting? Hosting is an online service where yo
6 min read
How To Use Docker For Gaming Servers ?
In this article, we will see a tutorial that will help us get into the world of Docker and explore it in such a way that it can broaden your horizons of knowledge to effectively utilize it for hosting gaming servers. Docker is a leading containerization platform, that offers a seamless way to packag
6 min read
Linux Post-Installation Steps for Docker Engine
When it comes to containerization Docker is the go to choice allowing developers and system admins to bundle applications and their requirements into containers. After setting up Docker Engine on your Linux setup it's essential to carry out installation tasks to make the most of its capabilities. Le
6 min read