Dockerizing a Django + MySQL project
If you are here, I assume you have already embraced the merits of using Docker to set up your development environment such that it matches your staging and production environments. In this guide I show you how to run your Django app in one Docker container and your MySQL database in another and make them talk to each other. We are going to manage and run these two containers using Docker Compose.
👉 If you want to learn how to do this for a Django + Postgres app read this post.
I am assuming you already have a Django project locally that is using a local database and that your project has a
requirements.txt file. You should also install Docker on your machine if you haven’t already.
Let’s dive right in:
The first step is to create a
Dockerfile in the root of your project (this is where you have your
Let’s go through that line by line:
FROM python:3.8is the base image your Docker image will be built upon. This will be pulled from DockerHub.
ENV PYTHONUNBUFFERED 1ensures that the output Django writes to the terminal comes out in real time without being buffered somewhere. This makes your Docker logs useful and complete.
WORKDIR /appcreates a new folder in your container called
appwhich will be your project’s root inside the container. It then sets that as the work directory in which the subsequent commands will be executed in.
COPY requirements.txt /app/requirements.txtcopies your local
requirements.txtfile to the Docker container.
RUN pip install -r requirements.txtwill make sure you have all your project dependencies installed inside the container.
COPY . /appand finally it’s time to copy your project’s content into the Docker container.
Now create a new file in your local project root. This one is called
docker-compose.yml and is used for configuring Docker Compose.
This defines two services: db and web.
db uses an official MySQL image from DockerHub. We attach local port 3306 to port 3306 of this container. We then set a bunch of environment variables that are required for MySQL. Make sure you also set these in your Django database settings. (You can put these in a
.env file to avoid committing sensitive information to version control. Read here to learn how.)
volume define two volume mappings:
- The first one maps the contents of
var/run/mysqldof the container to a local folder on your machine. This file contains socket information that enables the web service (your Django app) to talk to the database service.
./db:/var/lib/mysqlcreates a local
dbfolder in your project root so that database information can be saved if you destroy the db service.
The web service builds its image using the
Dockerfile we created earlier. It runs the Django server and exposes port 8000 to your machine. We map your local project folder to the app folder and we bring in the socket file. We tell Docker Compose that this service is dependant on the db service.
Now all we have to do is to start everything up. In your project root run:
This should build your images and start the containers. Open a browser and go to
0.0.0.0:8000. You should see your app. Now if you open a new terminal and run a
docker ps you will see the web and db containers are running:
If you want to stop Docker Compose, press Ctrl+C in the first terminal or run
docker-compose stop in the project root folder in another terminal.
manage.py commands in the Django container (web):