0% found this document useful (0 votes)
3 views

Docker_compose_creating_multi-container_servicesapplications_with_Docker.

Docker Compose is a tool for defining and running multi-container Docker applications using a YAML configuration file, allowing for easy management of services on a single host. It supports various environments and provides commands for starting, stopping, and managing the lifecycle of applications. Key features include isolated environments, volume data preservation, and the ability to specify service dependencies.

Uploaded by

engellbarrera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Docker_compose_creating_multi-container_servicesapplications_with_Docker.

Docker Compose is a tool for defining and running multi-container Docker applications using a YAML configuration file, allowing for easy management of services on a single host. It supports various environments and provides commands for starting, stopping, and managing the lifecycle of applications. Key features include isolated environments, volume data preservation, and the ability to specify service dependencies.

Uploaded by

engellbarrera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Docker compose: creating multi-

container services/applications
with Docker.
Created @December 27, 2022 8:19 PM

Tags docker

Brief overview
Docker compose is such a declarative way of working and dealing with multiple
containers and services on a single host machine. Is built on top of Dockerfiles and
Images/Containers. As a result, is not a replacement of them. In order to create custom
images you will still needing Dockerfiles as well. Nonetheless, with compose all things
became straightforward to handle.
Compose is a tool for defining and running multi-container Docker applications. With
Compose, you use a YAML file to configure your application’s services. Then, with a
single command, you create and start all the services from your configuration.
Compose works in all environments: production, staging, development, testing, as well

Docker compose: creating multi-container services/applications with Docker. 1


as CI workflows. It also has commands for managing the whole lifecycle of your
application:

Start, stop, and rebuild services

View the status of running services

Stream the log output of running services

Run a one-off command on a service

The key features of Compose that make it effective are:

Have multiple isolated environments on a single host

Preserves volume data when containers are created

Only recreate containers that have changed

Supports variables and moving a composition between environments

Creating a compose file


Docker compose uses the yaml extension as the configuration file, a tab sensitive
format mainly used to write configuration files in a declarative way. An example is shown
below.

⚠ The version key in a docker compose file has side effect on the features
available for use. The docker compose syntax stills under development. A
newer version providers new features but has lasting compatibility with olde
versions of the docker engine.
Look further on the compatibility matrix from docker’s website:
Compose file versions and upgrading | Docker Documentation

version: "3.8"
services:
service1:
image: 'mongo'
volumes:
- data:/data/db
environment:

Docker compose: creating multi-container services/applications with Docker. 2


- MONGO_INITDB_ROOT_USERNAME=engels
- MONGO_INITDB_ROOT_PASSWORD=secret
env_file:
- ./env/mongo.env
networks:
- sample-net
ports:
- "8000:8000"

volumes:
data:

⚠ The Compose V2 file specification does not require any version key and it’s
actually the recommended way to write compose files, there’s no further step
to follow from the given example, just erase the version key. Nevertheless,
compatibility with older versions of docker engine is lost.

Overview to the example compose file


There’s a more declarative way to tune our docker services using the compose
specification. Nevertheless, there’s a bunch of new concepts to embrace before starting
to use compose.

Always tab once below the current line to indicate the content of some key pair (i.e.
take a look over the services key).

All the enumerations must be prefixed by a minus sign.

Some enumerations like the data volume must be specified in another key before
being use elsewhere.

Looking at the service1 sub key, we should notice how many docker build instructions
even Dockerfile configurations are composed by a key format. In a simple context, most
of the tags pictures a setup of how I want my services to be running and behaving.

⚠ On the volumes section below the compose file, It is important to know that
only named volumes are needed there, anonymous volumes and bind
mounts

Docker compose: creating multi-container services/applications with Docker. 3


⚠ Use build instead of image to build containers from Dockerfiles, using build
requires a relative path scheme to the Dockerfile whence compose will take
the build instructions.

build: ./backend

build:
context: ./backend
dockerfile: Dockerfile-dev

Starting up composer files


Open a terminal window into the path where the docker-compose file is located and
execute docker compose up to bring up the containers, network and volumes specified
into the compose file.

docker compose up simple start, use the flag -d to run in detached mode.

turn off, erase containers and network but left volumes alive, to
docker compose down

erase with volumes use the -v flag.

Dependencies in compose
In Docker compose we can specify dependencies in each services to other service, this
is possible using the depends_on key along with the service name, such as:

version: "3.8"
services:
service1:
image: 'mongo'
volumes:
- data:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=engels
- MONGO_INITDB_ROOT_PASSWORD=secret
env_file:
- ./env/mongo.env

Docker compose: creating multi-container services/applications with Docker. 4


networks:
- sample-net
ports:
- "8000:8000"
service2:
image: 'alpine'
volumes:
- data:/data/app
depends_on:
- service1
volumes:
data:

In this example service 2 has a dependency to service1, so it means that service2 will
be only started until service 1 is up.

Interactive mode on compose containers


Use the stdin_open key set to true in order to bring input support, plus tty key set to
true to bring the console. As a result, we will be running the container in interactive
mode.

Setting custom container names


Use the container_name key with a value wrapped with single colons to give to any
container a custom name to be displayed on the container dashboard.

Extra attachments

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f83397e3-5788-4
a04-ad16-fb700cfe5ea6/Cheat-Sheet-Docker-Compose.pdf

Docker compose: creating multi-container services/applications with Docker. 5

You might also like