Docker_compose_creating_multi-container_servicesapplications_with_Docker.
Docker_compose_creating_multi-container_servicesapplications_with_Docker.
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
⚠ 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:
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.
Always tab once below the current line to indicate the content of some key pair (i.e.
take a look over the services key).
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
build: ./backend
build:
context: ./backend
dockerfile: Dockerfile-dev
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
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
In this example service 2 has a dependency to service1, so it means that service2 will
be only started until service 1 is up.
Extra attachments
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f83397e3-5788-4
a04-ad16-fb700cfe5ea6/Cheat-Sheet-Docker-Compose.pdf