I tried to summarize the words that I often see in docker-compose.yml

Introduction

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.

Describe docker-compose.yml

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.)

At the end

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.

References

Recommended Posts

I tried to summarize the words that I often see in docker-compose.yml
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I tried to summarize the methods used
I tried to summarize the Stream API
I tried to organize the session in Rails
[Must see !!!] I tried to summarize object orientation!
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I tried to organize the cases used in programming
I tried to summarize the state transition of docker
I tried to summarize again the devise that was difficult at first sight
I tried to implement the Euclidean algorithm in Java
I tried to summarize the basics of kotlin and java
I tried to summarize the basic grammar of Ruby briefly
I tried to summarize object orientation in my own way.
I tried to summarize iOS 14 support
I tried to explain the method
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to summarize what was asked at the site-java edition-
I tried to illuminate the Christmas tree in a life game
I tried to sort the data in descending order, ascending order / Rails
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to summarize the methods of Java String and StringBuilder
Special Lecture on Multi-Scale Simulation: I tried to summarize the 7th
I tried to summarize Java lambda expressions
I tried the new era in Java
I tried to implement the Iterator pattern
I tried the AutoValue library in Intellij
What is Docker? I tried to summarize
I tried to summarize the stumbling points when developing an Android application
I tried to summarize the key points of gRPC design and development
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to make full use of the CPU core in Ruby
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
I tried to summarize about JVM / garbage collection
I tried to implement polymorphic related in Nogizaka.
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to implement deep learning in Java
I tried to output multiplication table in Java
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
I want to get the value in Ruby
I tried to easily put CentOS-7 in a PC that I no longer need
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
[JavaScript] The strongest case when I tried to summarize the parts I do not understand
Fall 2017 Security Specialist I checked the frequency of words that appeared in the morning 2
I was addicted to the NoSuchMethodError in Cloud Endpoints
I tried to summarize the points to consider when acquiring location information with the iOS application ③
I want to embed any TraceId in the log
Tokoro I rewrote in the migration from Wicket 7 to 8
[Swift] I already have a lot of information, but I tried to summarize the cast (as, as !, as?) In my own way.
I tried to implement Firebase push notification in Java
I tried to decorate the simple calendar a little
05. I tried to stub the source of Spring Boot
# 2 [Note] I tried to calculate multiplication tables in Java.