I've learned a little more about Docker-compose.yml, so I wrote this article as an output and a memorandum. When reading this article, it is recommended that you understand the contents of the links (① to ⑥) as prerequisite knowledge. I am also indebted. -Introduction to Docker ① ~ What is Docker? ~【Beginners】
It was written as an introductory article for those who want to know broadly and shallowly (mainly beginners like me), so if you want to know more deeply after reading this article, please read the official document etc. and input it!
If you find any mistakes, please let us know.
docker-compose.yml
version: “3”
services:
app:
build:
context: .
dockerfile: docker/app/Dockerfile
ports:
- “8000:80"
volumes:
- ./src:/var/www/html
depends_on:
- db
db:
image: mysql:8.0.21
ports:
- "53306:3306"
volumes:
- ./docker/db/my.cnf:/etc/mysql/conf.d/my.cnf
- ./docker/db/mysql_data:/var/lib/mysql
env_file:
- ./docker/db/db-variables.env
The detailed settings of app: are described in the Dockerfile, but the explanation is omitted. Then! Let's look at each one!
version
Defines the version used by docker-compose. Please note that ** ComposeFile is written differently ** depending on this version. Unless you are particular about it, write the ** latest one **.
services
"Service" is the definition of the settings of the container to be started.
In Docker-Compose, each element to run the application is Service.
The service definition contains the settings that are applied to each container started for that service, and the contents of each Service are nested and described.
Where app :, db: is written, write the name of each element used to run the application. By the way, names defined like ** app: and db: are displayed in the docker log, so I think it's better to make the names easy for anyone to see. ** **
image
Specify the image to start the container from Docker Hub. Specify the repository/tag or partial imageID.
As for how to write, write as follows.
MySQL(Latest edition)in the case of | When specifying the version |
---|---|
mysql:latest | mysql:5.7 |
build
Configuration options applied at build time. This is the path when ComposeFile is executed and built. build can be specified as a string containing the path to the build context.
context
The path to the directory that contains the Dockerfile.
If the value given is a relative path, it will be interpreted as a relative path from the location of the Composefile.
dockerfile
An alternative Dockerfile.
Compose builds with an alternate file. You also need to specify the build path. Specify the original docker file here.
ports
The value on the host side is the key, and the value on the container side is the value. Host-side port and container-side port [HOST: CONTAINER], or specify only the container-side port. * At this time, the port on the host side is randomly selected.
As a way of writing
xx
format
xx: yy
format
There is a xx: yy: zz
format.
ports is a section for mapping the port on the host side and the port on the container side, but it behaves differently as follows depending on the writing method.
The xx
format is a format that specifies only the port number on the container side, and the specified container side port is mapped to a random port on the host side.
The xx: yy
format is a format that specifies the port on the host side and the port on the container side. xx is the host side port, yy is the container side port, and xx port on the host side and yy port on the container side are mapped.
In the xx: yy: zz
format, xx is the connection source IP that allows connection to the host side port, yy is the host side port, and zz is the container side port. Access to port yy on the host side is permitted only when connecting from IPxx, and it is mapped to port zz on the container side.
Note When mapping ports in HOST: CONTAINER format, if you use container ports lower than 60, YAML will parse numbers in xx: yy format as base 60 values, which may give incorrect results. For this reason, it is recommended that you always explicitly specify the port mapping as a string. ** **
For the time being, write it as a character string such as "8000: 80".
volumes
This is a function for data persistence, and even if the container is deleted, the data in volume will be retained unless volume is explicitly destroyed.
Describe the directory on the host side as key
and the directory on the container side as value
.
depends_on
You can set the dependency between services and change the start timing of the container.
In the above example, since db is specified in depends_on of app :, db will start before app. According to this description, docker-compose up will start the services in order of dependency.
Note depends_on doesn't wait for the db to be in the "ready" state before launching the app, it just waits for them to launch. If you need to wait for the service to be ready, see https://docs.docker.com/compose/startup-order/.
env_file
Add environment variables from the .env file.
If you specify a Compose file with docker-compose -f FILE, the path to env_file is relative to the directory where the file is located. This time, I'm setting environment variables in the .env file. (Env_file is difficult to understand unless you move your hand and set environment variables, so please scan it.)
Basically, if you read the official document, you will get the answer to what you do not understand, so if you do not understand or are interested in this article, please read the official document.
We look forward to helping you even a little.
Recommended Posts