[Docker] Introduction to docker compose Basic summary of docker-compose.yml

As a memorandum

I wanted to organize how to write docker-compose.yml and its outline once, so I will write it.


-Host OS: Windows 10 Home ・ Guest OS: wsl2 Ubuntu20.04 ・ Docker ver19.03 ・ Docker Compose ver1.25.0

Directory structure / file contents

In this article, the following directory structure is taken as an example. (I referred to this article.)

├── infra
│   ├── mysql
│   │   ├── Dockerfile
│   │   └── my.cnf
│   ├── nginx
│   │   └── default.conf
│   └── php
│       ├── Dockerfile
│       └── php.ini
├── docker-compose.yml
└── backend
└── Directory to install Laravel

・ Application server ・ Web server -Db server

It is a configuration to build a LEMP architecture consisting of three layers. In the original article, laravel is adopted as the PHP framework, but this time it is not dealt with because the purpose is to organize the knowledge of docker-compose.yml.

The contents of docker-compose.yml are as follows.

version: "3"
    build: ./infra/php
      - ./backend:/work
      - web
    image: nginx:1.18-alpine
      - "10080:80"
      - ./backend:/work
      - ./infra/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - db
    working_dir: /work

    build: ./infra/mysql
      - db-store:/var/lib/mysql


docker-compose.yml is written in a file format called yml (yaml). For yml, you should first keep the following in mind.

-Express the hierarchical structure with indentation. In order to ensure readability, one layer is often represented by two indents. -The --at the beginning of the line represents an array. --In the same hierarchy are elements of the same array. -Descriptions such as key: value represent hashes. It is necessary to put a half-width space after the :.

Frequently used keys

build: Defined when generating an image from a Dockerfile. .. Describe the file path of Dockerfile in value. (It doesn't matter whether it's a relative path or an absolute path. Also, docker compose looks for a file named Dockerfile in the described file path, so you can omit Dockerfile.)

  build: ./infra/php

In this case, the image that will be the basis of the app container will be created from the Dockerfile in ./infra/php.

image: Specifies the base image on which the container is based. For example, if you want to use the official image as it is, define this instead of build.

   image: nginx:1.18-alpine

Here, the web container is generated from the official image of nginx.

ports: Specifies the port that the container exposes. The format is "Host side port number: Container side port number".

  image: nginx:1.18-alpine
    - "10080:80"

In this example, port 10080 on the host side and port 80 on the container side are associated. (Port 80 is the default port number for nginx)


Although it is not defined this time, if you do not expose the port of the container to the host side and open it only to another container that cooperates, use expose instead of ports.


Defines container dependencies.

    image: nginx:1.18-alpine
      - "10080:80"
      - ./backend:/work
      - ./infra/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - db

The above example defines that the web container depends on the db container. Specifically, the order is defined that the db container is activated and then the web container is activated.

However, it only controls the order of container start, and the web server does not wait until the db server on the container is ready.

If you want to make such specifications, you need to take measures on the application side.


Mount the volume in the container. I think it's a term that many people don't understand, so the following is a brief explanation.

-Volume: A data storage area for retaining data even after the container is destroyed. Simply put, the directory that stores the data in the container -Mount: To load an external file into linux.

Roughly speaking, it's about Shioume, "Containers are not suitable for long-term storage, so let's store them in an external OS." (I think it's inconvenient to go into a container and edit php with vim ...)

volumes are defined in the format of "mountable host side directory: container side directory".

  build: ./infra/php
    - ./backend:/work

Here, the work directory (and the files and directories under it) in the app container is mounted on the ./backend directory on the host side.

Summary / Supplement

There are many other keys that can be defined, but for now, I've summarized the basic ones. First of all, I want to understand build and volumes.

Recommended Posts

[Docker] Introduction to docker compose Basic summary of docker-compose.yml
[Docker] Introduction of basic Docker Instruction
Docker basic summary
Introduction of Docker --Part 1--
[Summary of technical books] Summary of reading "Introduction to Docker / Kubernetes Practical Container Development"
[Introduction to Docker x ECS] ECS deployment with docker compose up
Introduction to Java for beginners Basic knowledge of Java language ①
Summary of basic functions of ImageJ
Introduction to RSpec-Everyday Rails Summary-
Introduction to Linux Container / Docker (Part 1)
Introduction to Linux Container / Docker (Part 2)
Summary of frequently used Docker commands
From introduction to use of ActiveHash
From introduction to usage of byebug
Summary about the introduction of Device
Introduction to Docker (1) Frequently used commands
Introduction to Ruby basic grammar with yakiniku
[java] Summary of how to handle char
Summary of how to write annotation arguments
Summary of Docker understanding by beginners ② ~ docker-compose ~
[Java] Personal summary of conditional statements (basic)
Summary of going to JJUG CCC 2019 Spring
Introduction of Docker Hub and commands Self-learning ①
[Java] [Maven3] Summary of how to use Maven3
Output of the book "Introduction to Java"
Introduction to Docker / Kubernetes Practical Container Development
[Docker] Introduction of basic options (sharing, user, port, CPU / memory) during Docker run
[Introduction to Docker] Official Tutorial (Japanese translation)
[For beginners] Introduction to Java Basic knowledge of Java language ③ Array, selection structure, iteration structure
Summary of how to select elements in Selenium
[java] Summary of how to handle character strings
[Rails] Introduction of pry-rails ~ How to debug binding.pry
Summary of how to create JSF self-made tags
Summary of basic knowledge of Rails acquired by progate
[Java] Personal summary of classes and methods (basic)
[Java] Summary of how to abbreviate lambda expressions
Introduction and basic usage of Simple Calendar gem
Summary of moss when updating from JMockit 1.4 to 1.30
I tried to chew C # (basic of encapsulation)
How to use enum (introduction of Japanese notation)
How to use docker compose with NVIDIA Jetson
Summary of Docker understanding by beginners ① ~ docker run -p ~
[Introduction to Java] Basics of java arithmetic (for beginners)
Introduction to Ruby 2
Introduction of pay.jp
Introduction to SWING
Introduction of milkode
docker basic command
Docker basic commands
[Memo] docker summary
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
Docker command summary
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
[Docker] Command summary
Summary of frequently used commands in Rails and Docker
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
From the introduction of devise to the creation of the users table