[PYTHON] Consider the description of docker-compose.yml (Django + MySQL ③)

About this article

This article corresponds to ** Part.3 ** of the articles related to Learn how to use Docker through Django + MySQL environment construction.

  1. Build a Python virtual environment using venv
  2. Consider the description of Dockerfile
  3. ** Consider the description of docker-compose.yml (this article) **
  4. Edit the configuration file and execute docker-compose up
  5. Adjust container launch timing between dependent services

Introduction

In this article, ** so that you can create a Docker container as you want ** as much as possible in the future ** while minimizing the effort to build the environment and the commands that need to be executed **, ** Let's think about the description of docker-compose.yml **.

As for the Docker container, I think of it as a realization of the ** Docker image ** that I learned so far, and the current state of the ** "small machine for the project" that was moved.

The Django container is based on the Dockerfile created last time, while the MySQL container uses the official image as it is, and we will proceed with the work.

This time, ** docker-compose.yml is also created in the same hierarchy as Dockerfile **.

Description

The overall description of docker-compose.yml is as follows.

docker-compose.yml


version: "3"
services:
    web:
        container_name: djst_django
        build: .
        restart: always
        command: >
            bash -c "
                pip install -r requirements.txt &&
                python manage.py runserver 0.0.0.0:8000
            "
        working_dir: /code
        ports: 
            - 172.0.0.1:8000:8000
        volumes:
            - .:/code
        depends_on:
            - db
    db:
        container_name: djst_mysql
        image: mysql:5.7
        restart: always
        environment: 
            MYSQL_DATABASE: $DB_NAME
            MYSQL_USER: $DB_USER
            MYSQL_PASSWORD: $DB_PASSWORD
            MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
            MYSQL_TCP_PORT: 3306
            TZ: 'Asia/Tokyo'
        volumes: 
            - ./mysql/data:/var/lib/mysql/
            - ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
        ports: 
            - 3306:3306

Think about the meaning of the description

version: "3"

The first line is the file format version of docker-compose. If the version displayed by executing $ docker-compose version is 1.13.0 or later, " 3 ", if it is 1.10.0 or later,"2", if neither of these is available, "1" It will be ".

services:

From the second line, we will start describing the services managed by this file.

Django side

web:

Line 3 begins with the service that runs the Django application. Here the image name (at the end of) is named.

container_name: djst_django

Line 4, ** the name of the container to be created **. I will name it here.

build: .

On the 5th line, when viewed from this docker-compose.yml, ** specify where to generate the image based on the Dockerfile **. Since it is placed in the same layer this time, it is described as ..

The sixth line will be considered later in the MySQL section.

command: >
    bash -c "
        pip install -r requirements.txt &&
        python manage.py runserver 0.0.0.0:8000
    "

Lines 7-12 describe the command you want to run from inside the container when you run the $ docker-compose up command later. $ bash -c" " will execute the command described in " ".

  1. ** Execute pip install to automatically synchronize the packages used in the host and container **
  2. ** Start the Django server with no host restrictions ( 0.0.0.0) **

They are separated by && so that they are executed in order.

We'll limit host later from the django config file.

working_dir: /code

On line 13, ** specify the working directory **. Specifies the code directory inside the container. This directory is created based on the description of Dockerfile described in Last time.

ports: 
    - 172.0.0.1:8000:8000

Lines 14-15, ** specify the port to use **. The left side 172.0.0.1:8000 is the description about the host machine side host and the port, and the right side is the description about the container side port sandwiched between :. If you connect to 127.0.0.1:8000 from the host machine side, you will be connected to the 8000 number of host automatically assigned by the container side. Regarding the specified port, it matches the port specified by runserver in the above command.

volumes:
    - .:/code

Lines 16-17, ** Specify data mount **. As in the previous section, the left side with ** : is the host path, and the right side is the container side path **. The changes are synchronized and the left and right sides keep the same management. This item seems to be There are various other ways to describe it (official document), and it seems difficult to use it freely. ..

depends_on:
    - db

Lines 18-19, ** specify service dependencies **. ** It seems that when you start web, db is also started **. Problems can occur in the boot order, but for the time being, you can use MySQL as the DB of the Django application.

MySQL side

Next, let's consider the description of the service of MySQL.

db:

Line 20 begins the description of the service that MySQL runs. As before, this will be the image name (at the end of).

container_name: djst_mysql

The 21st line names the container.

image: mysql:5.7

The 22nd line specifies ** which image to create based on **. Unlike the item of web mentioned above, if you do not customize with Dockerfile and use the officially prepared image as it is, it will be described like this. Of course, if you create another Dockerfile in a separate directory, you can specify it and use it.

restart: always

Line 23, ** Settings related to restart when container is stopped **. Here, it is set to ʻalwaysso that it always restarts except for manual operation. The default is" no ", and it seems that you can select ʻunless-stopped and ʻon-failure in addition to ʻalways.

environment: 
    MYSQL_DATABASE: $DB_NAME
    MYSQL_USER: $DB_USER
    MYSQL_PASSWORD: $DB_PASSWORD
    MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
    MYSQL_TCP_PORT: 3306
    TZ: 'Asia/Tokyo'

Lines 24-30 are ** environment variable settings **. Those with $ will be described in the .env file that will be created later, in case you want to publish it to GitHub etc. These are the DB information to connect from Django.

MYSQL_TCP_PORT should be consistent with the ports option we will see later. Here, I specified the default number 3306 for MySQL. TZ is a regional setting.

volumes: 
    - ./mysql/data:/var/lib/mysql/
    - ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf

Lines 31-33 are ** data mount settings **. As before, the host is to the left of ** : and the path inside the container is to the right. ** **

The ascending line is ** mount settings for persistence **. The information stored in the DB is synced to the host's ./mysql/data directory as the application is used. The bottom line is ** Mount settings in the configuration file **. If you prepare a ./mysql/my.cnf file that describes the language setting etc., it will be reflected in the MySQL setting on the container side.

Besides, if you prepare SQL files etc. in the form of mounting in the /docker-entrypoint.init.d directory in the container, it seems that you can also input the initial data to the DB.

ports: 
    - 3306:3306

Finally, ** setting the port to use **. It is OK if it is consistent with the environment variable MYSQL_TCP_PORT described earlier.

Creating an .env file

Create a ** .env file ** to describe the environment variables. This is also placed in the same hierarchy.

.env


DB_NAME=********
DB_USER=********
DB_PASSWORD=********
DB_ROOT_PASSWORD=********

The description is ** environment variable definition ** corresponding to the ʻenvironment option described earlier in docker-compose.yml. Please describe as you like in the place of *********. If you want to publish to GitHub` etc., ** remove this file from management **.

Creating MySQL configuration related files

Create a MySQL mount directory ./mysql and place the configuration file my.cnf in it.

mysql/my.cnf


[mysqld]
character-set-server=utf8mb4

[client]
default-character-set=utf8mb4

Enter the variables you want to change from the beginning here in advance.

The ./mysql/data/ specified as the DB data mount destination is automatically generated when $ docker-compose up is executed. If you want to publish this on GitHub etc. **, remove it from the management target in advance **.

Current directory structure

django_starter
    ├── .venv
    │   └── (Abbreviation)
    ├── config
    │     ├── __init__.py
    │     ├── asgi.py
    │     ├── settings.py
    │     ├── urls.py
    │     └── wsgi.py
    ├── mysql                 <- New!
    │     └── my.cnf
    ├── .env                  <- New!
    ├── docker-compose.yml    <- New!
    ├── Dockerfile
    ├── manage.py
    └── requirements.txt

At the end

You have now created a docker-compose.yml to build your Django + MySQL environment.

This time, I tried various things to understand the meaning of each option, and the deeper I deepened my knowledge of Docker, of course, **" technology I want to handle "such as MySQL and Django, the more. I felt once again that I could do various things freely **, so I wanted to continue learning, including those.

Next time, I'll consider ** editing the Django config file ** and executing the ** $ docker-compose up command **. Click here for the next article ↓ "4. Edit the configuration file and execute docker-compose up"

Thank you for visiting.

Recommended Posts

Consider the description of docker-compose.yml (Django + MySQL ③)
Consider the description of Dockerfile (Django + MySQL②)
The meaning of ".object" in Django
I tried the asynchronous server of Django 3.0
Understand the benefits of the Django Rest Framework
Script to change the description of fasta
How to check the version of Django
The meaning of {version-number} in the mysql rpm package
I tried to summarize the settings for various databases of Django (MySQL, PostgreSQL)
Exclusive release of the django app using ngrok
I checked the session retention period of django
Consider improving the accuracy of VAE abnormality detection
The story of viewing media files in Django
Django + MySQL settings
The wall of changing the Django service from Python 2.7 to Python 3
[Django] Change the Default IP address of the runserver command
[Python] Let's change the URL of the Django administrator site
The meaning of self
Impressions of touching Django
the zen of Python
The story of sys.path.append ()
[Django] Rename the project
Use MySQL with Django
Revenge of the Types: Revenge of types
Edit the config file and run docker-compose up (Django + MySQL ④)
I participated in the translation activity of Django official documents
Django returns the contents of the file as an HTTP response
Django + MongoDB development environment maintenance (in the middle of writing)
Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi
The story of a Django model field disappearing from a class
Summary of stumbling blocks in Django for the first time
Until the start of the django tutorial with pycharm on Windows
[Django] Memo to create an environment of Django + MySQL + Vue.js [Python]