This article corresponds to ** Part.3 ** of the articles related to Learn how to use Docker through Django + MySQL environment construction.
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
**.
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
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.
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 " "
.
pip install
to automatically synchronize the packages used in the host and container **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.
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.
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 **.
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 **.
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
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