Dockerize a Python-Django Application
Introduction:
Python is a powerful and widely-used language in the software development industry. When combined with Django, a high-level Python web framework, developers can create powerful web applications. However, setting up a development environment can be a time-consuming task, particularly when working with multiple developers. Fortunately, Docker can help solve this problem by providing an easy way to package and distribute applications in a portable container.
Prerequisites
- Basic knowledge of Docker and Docker Compose
- Python 3.x installed on your local machine
- Django installed on your local machine
- A basic understanding of Dockerfiles
Step 1: Create a Django project First, let’s create a Django project on your local machine.
Open a terminal and run the following command to create a new Django project:
django-admin startproject myproject
This will create a new directory named myproject
with the basic files required for a Django project.
Step 2: Create a Dockerfile Next, we need to create a Dockerfile that will be used to build a Docker image for our app. In the same directory where your manage.py
file is located, create a file named Dockerfile
and add the following code:
# Use an official Python runtime as a parent image
FROM python:3.9-alpine
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set the working directory to /app
WORKDIR /app
# Copy the requirements file to the container
COPY requirements.txt .
# Install app dependencies
RUN apk update && \
apk add postgresql-dev gcc python3-dev musl-dev && \
pip install --upgrade pip && \
pip install -r requirements.txt && \
apk del postgresql-dev gcc python3-dev musl-dev
# Copy the rest of the application code to the container
COPY . .
# Expose the port the app listens on
EXPOSE 8000
# Run the app
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
This Dockerfile is based on the official Python 3.9-alpine image and sets some environment variables required for Django. It also installs some dependencies required for running Django with PostgreSQL and copies the application code to the container.
Step 3: Create a requirements.txt file Create a requirements.txt
file in the same directory as your Dockerfile with the following content:
Django==3.2.4
psycopg2-binary==2.9.1
This file lists the Python packages required by your app.
Step 4: Create a Docker Compose file Next, create a Docker Compose file named docker-compose.yml
in the same directory as your Dockerfile with the following content:
version: "3"
services:
db:
image: postgres
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
POSTGRES_DB: mydb
volumes:
- db_data:/var/lib/postgresql/data/
app:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
volumes:
db_data:
You can run this Docker Compose file using the command,
docker-compose up -d
Conclusion:
Python-Django app with Docker and Docker Compose is an effective way to package and deploy your application in a consistent and reproducible manner. By creating a Dockerfile and Docker Compose file, you can easily build and run your app in a containerized environment. This allows you to avoid dependency issues and easily scale your app in a production environment. By following the steps outlined in this tutorial, you should now have a basic understanding of how to deploy a Python-Django app with Docker.