Dockerize a Django app with a MySQL container

Dockerize a Django app with a MySQL container

·

5 min read

There are a lot of irrelevant articles on the Internet, so I prepared this publication. Basically, when writing the article, I relied on the example of configuring docker for django in the docker documentation.

Here we will describe the docker setup for the django framework and the mysql database, which will be stored outside the container, so you don't have to worry about something happening to our container.

This guide was written using:

  • ubuntu 20.04
  • docker 19.03.12
  • docker-compose 1.26.2
  • python 3.8
  • mysql 5.7
  • django 3.1.1
  • mysqlclient 2.0.1
  • django-mysql 3.8.1

Workspace setup

Before starting the setup, you need to prepare the workspace:

  1. Install docker and docker-compose. The installation process is detailed in the docker documentation.
  2. Create a directory where our project will be located (in this example, project). In this directory, create another one, which will store the configuration for running the python image (in this example, web).
./project
      /web

This completes the preparation and you can start setting up our small project.

requirements.txt

In the web directory, create a file called requirements.txt. In this file, we declare packages for our django application to work.

/project/web/requirements.txt

Django==3.1.1

mysqlclient==2.0.1

django-mysql==3.8.1

As you can see, django itself is registered here, as well as packages for communicating with the mysql database.

Next, we will use this file to configure the contents of the image.

Dockerfile

In the web directory, create a file called Dockerfile. The Dockerfile configures the content of the application image using one or more commands. After building, we will launch the container from the configured image.

Add the following content to this file:

/ project / web / Dockerfile

FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /web_django
WORKDIR /web_django
COPY requirements.txt /web_django/
RUN pip install --upgrade pip && pip install -r requirements.txt
ADD . /web_django/

This setting creates a directory with our project in the python image, copies there a list of packages that need to be installed and installs them. Also, the folder with the code from the image is linked to the web_django directory in the project root.

docker-compose.yml

Create a docker-compose.yml file in the root of the project.

This file contains the service settings used by the application. In this example, these services are a web server and a database. The compose file also describes which Docker images are using these services, how they communicate with each other, what volumes they need inside the containers. The ports used by these services are also installed.

Add the following content to this file.

/project/docker-compose.yml

version: '3'

services:
  web:
    build: ./web
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/web_django
    ports:
      - '8000:8000'
    depends_on:
      - db
  db:
    image: mysql:5.7
    ports:
      - '3306:3306'
    environment:
      MYSQL_DATABASE: 'db_django'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password'
    restart: always
    volumes:
      - ./db_django:/var/lib/mysql

In the first part, the image is built according to our instructions in the web / Dockerfile file and the container is launched. Next, the django server starts on port 8000. The volume of our django project is configured and the service with the mysql image is assigned to the dependency.

The second part describes the mysql service. It is extremely simple here: the image used, the ports that are listened to by this service, the database environment (database name, user, password) and the volume in which our database will be stored are indicated. Thanks to the fact that we specified the volume, we don't have to worry that data will be lost if our container with mysql stops working.

Finished the docker setup. Now let's start running the container and creating a django project.

Creating a django project

Go to your project root (project /) and run the command. This command creates a django project called web_django.

sudo docker-compose run web django-admin startproject web_django .

Next, you need to change the owner of the folder. the container runs as root user.

sudo chown -R $USER:$USER web_django

Setting up the project database

Open the project django settings file at web_django / settings.py path In this file, we will replace the database connection settings that we specified in docker-compose.yml.

/project/web_django/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_django',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': '3306',
    }
}

Project launch

To start containers, just run this command

docker-compose up --build

Database migration

In order to migrate the database, run the commands:

docker-compose run web python manage.py makemigrations
docker-compose run web python manage.py migrate

That's all.