Open In App

Setting Up Docker for Python Projects: A Step-by-Step Guide

Last Updated : 09 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Docker-Setup-Guide-for-Python-copy
Setting Up Docker for Python Projects

In 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 .
2

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
3

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.


Next Article
Article Tags :
Practice Tags :

Similar Reads