About
A docker-compose.yml is a configuration file that permits:
- to set docker option command in a yaml file
- and to define in one file multiple docker command (called services)
Example
Basic
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: username/repo:tag
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
where:
- web define the name of the service as web
- image: defines the image to pull
- replicas defines the number of instances to run
- limits limits each replica to use, at most, 10% of the CPU (across all cores), and 50MB of RAM.
- restart_policy defines that a replica must restart on failure
- ports map the port 80 on the host to web’s port 80. See Docker - Port
- webnet defines a load-balanced network that shares the port 80 (Internally, the containers themselves publish to web’s port 80 at an ephemeral port.). The default settings is a load-balanced overlay network.
From docker options to yaml
Theses options:
docker container run -d \
--env DOKKU_HOSTNAME=dokku.me \
--env DOKKU_HOST_ROOT=/var/lib/dokku/home/dokku \
--env DOKKU_LIB_HOST_ROOT=/var/lib/dokku/var/lib/dokku \
--name dokku \
--publish 3022:22 \
--publish 8080:80 \
--publish 8443:443 \
--volume /var/lib/dokku:/mnt/dokku \
--volume /var/run/docker.sock:/var/run/docker.sock \
dokku/dokku:0.34.4
Are equivalent to
version: "3.8"
services:
dokku:
image: dokku/dokku:0.34.4
container_name: dokku
ports:
- "3022:22"
- "8080:80"
- "8443:443"
volumes:
- "/var/lib/dokku:/mnt/dokku"
- "/var/run/docker.sock:/var/run/docker.sock"
environment:
DOKKU_HOSTNAME: dokku.me
DOKKU_HOST_ROOT: /var/lib/dokku/home/dokku
DOKKU_LIB_HOST_ROOT: /var/lib/dokku/var/lib/dokku
restart: unless-stopped
Managment
Run the specified Compose file. See Docker - App
docker swarm init
docker stack deploy -c <composefile> <appname>
Configuration
Port
Port linkage
version: '2'
services:
serviceName:
ports:
- "8080:8080"
- "80:80"
- ...
Network
version: '2'
services:
serviceName:
networks:
- netWorkName
networks:
networkName:
driver: bridge