Compose

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:

  1. Create a docker-compose.yml file: This file defines your application’s services, their dependencies, and configurations.

  2. Run docker compose up: This command will build, start, and run all the services defined in your docker-compose.yml file.

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

image

Specifies the Docker image to use for the service.

build

Defines build configurations for creating an image.

command

Overrides the default command for the container.

entrypoint

Overrides the default entrypoint of the container.

environment

Defines environment variables.

env_file

Specifies an external file containing environment variables.

ports

Maps container ports to the host.

volumes

Mounts host directories or named volumes into the container.

networks

Connects the service to specific networks.

depends_on

Specifies dependencies on other services.

restart

Defines the restart policy for the container.

deploy

Specifies deployment settings (mainly for Swarm).

healthcheck

Defines a health check command for the container.

logging

Configures logging options for the service.

ulimits

Sets resource limits for the container.

extra_hosts

Adds custom host-to-IP mappings.

dns

Specifies DNS servers for the container.

dns_search

Defines DNS search domains.

sysctls

Configures kernel parameters.

security_opt

Sets security options for the container.

cap_add

Grants additional Linux capabilities.

cap_drop

Removes Linux capabilities.

devices

Allows access to host devices inside the container.

secrets

Defines secrets to be used in the container.

configs

Specifies configuration files from Docker Swarm.

tmpfs

Mounts a temporary filesystem inside the container.

shm_size

Sets the size of the /dev/shm shared memory.

privileged

Grants extended privileges to the container.

read_only

Runs the container filesystem in read-only mode.

stop_signal

Defines the system signal used to stop the container.

stop_grace_period

Specifies a timeout before forcibly stopping a container.

init

Uses an init process inside the container.

container_name

Allows to specify a name for the result container.

For more details check: