Compose#
Docker compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you can use a YAML file to configure your application’s services. Then, with a single command, you can create and start all the services from your configuration. This page overview core concepts of the docker compose.
In the special config you can specify the behaviour of our application as a group of containers.
Check official instrcutions for the docker compose file.
Up/down#
To get started with Docker Compose, follow these steps:
Create a
docker-compose.yml
file: This file defines your application’s services, their dependencies, and configurations.Run
docker compose up
: This command will build, start, and run all the services defined in yourdocker-compose.yml
file.To stop and remove all containers and networks created by Docker Compose, use
docker compose down
.
Find out more in a specific page.
To run docker compose, you need to specify a special config for dockercompose. The following cell defines compose config needed two services linux1
and linux2
.
cat << EOF > compose.yml
services:
linux1:
image: alpine
linux2:
image: alpine
EOF
To run a defined container you need to run compose up
, this command will automatically find compose.yml
in the execution folder and run it.
docker compose up
?25l[+] Running 0/0
⠋ Network docker_default Creating 0.1s
?25h?25l[+] Running 1/1
✔ Network docker_default Created 0.1s
⠋ Container docker-linux1-1 Creating 0.1s
⠋ Container docker-linux2-1 Creating 0.1s
?25h?25l[+] Running 1/3
✔ Network docker_default Created 0.1s
⠙ Container docker-linux1-1 Creating 0.2s
⠙ Container docker-linux2-1 Creating 0.2s
?25h?25l[+] Running 3/3
✔ Network docker_default Created 0.1s
✔ Container docker-linux1-1 Created 0.2s
✔ Container docker-linux2-1 Created 0.2s
?25hAttaching to linux1-1, linux2-1
linux2-1 exited with code 0
linux1-1 exited with code 0
Containers we’ve specified to exit right after run - there’s nothing specified to do for them. But in the docker ps -a
we can still see them with exited
status.
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e482380d1c3 alpine "/bin/sh" 2 seconds ago Exited (0) 1 second ago docker-linux2-1
e246e12ea924 alpine "/bin/sh" 2 seconds ago Exited (0) 1 second ago docker-linux1-1
To remove everything you’ve created, use the docker compose down
command.
You need to run it in the folder where compose config is located - it will automatically detect what was created by this compose and delete it.
docker compose down
?25l[+] Running 2/0
✔ Container docker-linux1-1 Removed 0.0s
✔ Container docker-linux2-1 Removed 0.0s
⠋ Network docker_default Removing 0.1s
?25h?25l[+] Running 2/3
✔ Container docker-linux1-1 Removed 0.0s
✔ Container docker-linux2-1 Removed 0.0s
⠙ Network docker_default Removing 0.2s
?25h?25l[+] Running 2/3
✔ Container docker-linux1-1 Removed 0.0s
✔ Container docker-linux2-1 Removed 0.0s
⠹ Network docker_default Removing 0.3s
?25h?25l[+] Running 3/3
✔ Container docker-linux1-1 Removed 0.0s
✔ Container docker-linux2-1 Removed 0.0s
✔ Network docker_default Removed 0.3s
?25h
So, after all, containres should disappear.
docker ps -a
rm compose.yml
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Service configuration#
The following cell shows directives that you can use to configure your service in docker compose:
Directive |
Description |
---|---|
|
Specifies the Docker image to use for the service. |
|
Defines build configurations for creating an image. |
|
Overrides the default command for the container. |
|
Overrides the default entrypoint of the container. |
|
Defines environment variables. |
|
Specifies an external file containing environment variables. |
|
Maps container ports to the host. |
|
Mounts host directories or named volumes into the container. |
|
Connects the service to specific networks. |
|
Specifies dependencies on other services. |
|
Defines the restart policy for the container. |
|
Specifies deployment settings (mainly for Swarm). |
|
Defines a health check command for the container. |
|
Configures logging options for the service. |
|
Sets resource limits for the container. |
|
Adds custom host-to-IP mappings. |
|
Specifies DNS servers for the container. |
|
Defines DNS search domains. |
|
Configures kernel parameters. |
|
Sets security options for the container. |
|
Grants additional Linux capabilities. |
|
Removes Linux capabilities. |
|
Allows access to host devices inside the container. |
|
Defines secrets to be used in the container. |
|
Specifies configuration files from Docker Swarm. |
|
Mounts a temporary filesystem inside the container. |
|
Sets the size of the |
|
Grants extended privileges to the container. |
|
Runs the container filesystem in read-only mode. |
|
Defines the system signal used to stop the container. |
|
Specifies a timeout before forcibly stopping a container. |
|
Uses an init process inside the container. |
|
Allows to specify a name for the result container. |
For more details check:
Corresponding page on this website.
Corresponding page of the offical documentation.