Docker Compact Manual (3: docker-compose)

At first

This article "Docker Compact Manual" consists of 4 items. Other articles are here.

Post number Subtitle and access destination
1 Elementary / basic commands
2 Mount and data persistence
3 docker-compose (* This article)
4 Create a custom image

Purpose of this article

In this article, I would like to summarize Docker-compose that starts multiple containers at once.

docker-compose Using multiple containers in combination on a Docker host This is possible using the Docker network. However, as a problem, it is necessary to be aware of the order of startup and execute it. Also in that case, do docker run and name it with --name. Set the mount with --mount ... You definitely need to execute the long commands in sequence. ~~ It's annoying! ~~

By reading the definition file, a series of operations of creating, stopping, and destroying this complicated container You can run docker-compose.

1d54fa053d39.jpg

docker-compose is in the definition file (docker-compose.yml) Collect the files you want to execute / copy in one directory. When this file is executed with a tool called docker-compose, volumes and networks are created. The containers will start together.

Benefits of docker-compose

  1. There is no need to specify long arguments such as docker run, which is required in the past.
  2. You can start multiple containers at once.
  3. You can also specify the startup order
  4. Containers can be stopped / destroyed at once
  5. Initialization operations such as command execution can be performed after the container is started.

Install docker-compose

A Python tool that assists docker-compose and Docker. Since it is separate from Docker Engine, it needs to be installed separately, Since Docker for Mac and Windows include Compose If you have the above installed, you don't need to be aware of it. Just in case, you can check the version of docker-compose below.

docker-compose --version
docker-compose version 1.27.2, build 18f557f9

docker-compose operation

docker-compose is a docker-compose command option argument Process and execute the docker-compose.yml file.

There are many commands, but the one I use most often I think there are six of them: ʻup down` `start stop run ʻexec. The table below is an excerpt of the other command types.

-Docker-compose command

command Command content
up Create and start a container
down Stop and delete containers, networks, images, and volumes at once.
However, the image and volume must be specified by optional arguments.
start Start service
stop Stop the service
run Run the container
exec Execute a command
ps List containers
config Check and view Compose files
kill Forcibly stop the container
restart Restart the service
build Build or rebuild images for services
events Real-time event reception from container

Also, the command arguments of ʻupthat are often used are as follows. I think the most frequently used option is-d`.

-Docker-compose up optional argument

up option Option contents
-d Executed in detach mode (background operation). I think I use it often.
--no-deps Don't show linked services
--force-recreate Regenerate the container even if there are no changes to the settings or images.
--no-Cannot be specified at the same time as recreate
--no-create If the container already exists, it will not be regenerated.
--force-Cannot be specified at the same time as recreate
--no-build Don't build if image not found
--build Build the image before starting the container
--abort-on-container-exit If even one container is stopped, stop all containers.
-Cannot be specified at the same time as the d option
-t,--timeout ** The number of seconds to time out when stopping the container. The regulation is 10 seconds
--remove-orpahns Remove container for services not defined in Compose file

Similarly, down has optional arguments. If you have a lot of images or you don't want to use them next time I think it's better to delete it with down with --rmi. If you do docker-compose down --rmi all --volumes You can delete all images and volumes at once.

-Docker-compose down optional argument

down option Option contents
--rmi all, local The image is also deleted after it is destroyed.
If all is specified, all used images will be deleted.
In the case of local, only images without custom tags in image will be available.
--v,--volumes Delete the volumes described in volumes.
However, those specified as external are excluded.
--remove-orphans docker-compose.Delete containers for services not defined in yml

Note on docker-compose down, up

If the docker-compose.yml files are different when doing docker-compose up and down docker-compose down works by looking at the current docker-compose.yml file. If the container description has been deleted since ʻup, the container will not be destroyed even if it is down`. Be careful not to leave the container deleted or delete the container or network unintentionally.

How to write docker-compose.yml file

docker-compose.yml requires three definitions: service, network and volume. Also, in the yml format file, it is described in indented block units. If the indentation is incorrect, the description will not be reflected and an error will occur. Also note that tab characters cannot be used for indentation.

The description required for docker-compose.yml is as follows.

1. Version definition

As of September 2020.9, version 3.8 is the latest. In the version specification, if " 3 " is described, 3.0 is applied. If you want to reflect the setting of the minor version, it will not be reflected unless you describe it as " 3.8 ".

Note: When specifying the Compose file version to use, make sure to specify both the major and minor numbers. If no minor version is given, 0 is used by default and not the latest minor version. As a result, features added in later versions will not be supported.

Compose-versioning

2. Service

It refers to each container that makes up the whole. It is the basis of docker-compose.yml settings.

Service settings are described in the following format.

services:
Service A name:
Service A settings
・ ・ ・
Service B name:
Service B settings

・ Main service settings The following is an extract of the service settings that you think you will often use based on the official website. For details, please visit the official website. Compose file referende

Service item Contents Supplementary information
image Specify the image to use redis,postgres,mysql etc.
ports Use port mapping postgres 5432:5432 etc.
build Set at build time.
As a string containing the path to the build context
Can be specified.
build .in the case of
Dockerfile in the current directory
refer.
depends_on Indicates that it depends on another service.
docker-compose up,When down
The specified service starts or ends first
Will come to do.
Used for setting to db on the app side, etc.
volumes Define bind mounts and volume mounts --type like the mount option:,source:etc
It is also possible to describe in detailed settings
command Override the default command at startup bundle exec rails s etc.
environment Set environment variables. DB password, user name, etc.

There are many service setting items other than the above, and it is recommended that you read them once. From version 3.8, a descriptive setting that specifies the number of replica sets has been added. There are updates even for minor versions, so be careful when specifying the version.

3. Network

Define the network in which the service participates. Since it is often omitted, I will omit it.

4. Volume

Defines the volume used by the container. You can specify the driver name etc. as an option. The following are the main options.

item Contents
driver Volume driver name
driver_opts Volume options. NFS etc.
external docker-compose Specifies that the volume is not managed.
This volume must have been created in advance.
external true, external: name:When specified by etc.
docker-compose down -It is not deleted even if you do v.

Individual service operation with docker-compose

Sometimes you may want to start or operate only one container. At that time, you can use the docker command, but by using the docker-compose command Based on the docker-compose.yml file, you can execute commands considering dependencies. (* The depends-on setting that starts the database before executing the application is reflected, etc.)

--Excerpt of comparison between docker-compose command and docker command

docker-compose command Corresponding docker command motion
docker-compose exec docker exec Command execution in the container
docker-compose run docker run Run a specific container
docker-compose start docker start Start a specific service
docker-compose stop docker stop Stop certain services
docker-compose rm docker rm Delete a stopped container
docker-compose logs docker logs View the output of the container

When operating individual containers, execute the command as shown below. I think the most commonly used are ʻexec and run. The following is when executing a shell with / bin / bash in a container called web. The docker command required -it, but compose does not require these options. This will change the command prompt and allow you to enter commands inside the container. (* 12e7a08bb0bd` is the container ID. It depends on the environment.)

❯ docker-compose exec web bash
root@12e7a08bb0bd:~# 

--Example of results when working with shell (excerpt from ls -a command execution)

❯ docker-compose exec web bash
root@12e7a08bb0bd:~# ls -a
.		 .gem	     .rspec		Dockerfile	 README.md	  config	      log		 public       storage	      yarn.lock
..		 .git	     .rubocop.yml	Gemfile		 Rakefile	  config.ru	      memo.md		 public_data  tmp
.bash_history	 .github     .rubocop_todo.yml	Gemfile.lock	 app		  db

This article ends here. Thank you for reading.

Recommended Posts

Docker Compact Manual (3: docker-compose)
Docker Compact Manual (1: Basic / Basic Command)
Docker Compact Manual (2: Mount and Data Persistence)
Docker Compact Manual (4: Create a custom image)
EC2 ✕ Manual deployment with Docker / docker-compose / Asset compilation
Install docker, docker-compose for debin10
People using docker Try using docker-compose
Create Laravel environment with Docker (docker-compose)
docker
Install docker and docker-compose on Alpine Linux