Practical Exercises for Docker Compose: Part 1

Prerequisites

You need access to a server/VPS with a recent version of Docker already installed. In this tutorial, I will be using an Alibaba Cloud Elastic Compute Service (ECS) instance with CentOS and Docker.

Clean Up Preparation

It will really help if you have only a few containers (preferably none) running. That way you can easily find your tutorial container in docker ps -a output lists.

docker-compose down -t 0 --remove-orphans
docker stop $(docker ps -a -q) #stop ALL containers
docker rm -f $(docker ps -a -q) # remove ALL containers

Separate Directory and Editor

Its better to have your docker-compose files in separate directories. One directory per docker-compose service / stack or swarm: neat, organized, small, efficient, understandable in its entirely due to all these attributes.

mkdir compose-tuts
cd compose-tuts
apt install nano
yum install nano
* nano filename-to-edit ... this starts the nano editor
* cursor keys move around as expected
* cut tutorials text from alibaba tutorials web pages work as expected
* pasting into your docker-compose file work as expected
* press F3 to save your work
* press F2 to exit the editor.

image

This works identical to image in Dockerfiles. Image specifies the image to build the container from.

tmpfs

Reference : https://docs.docker.com/compose/compose-file/#tmpfs

nano docker-compose.ymlversion: "3.7"
services:
alpine:
image: alpine:3.8 command: sleep 600 tmpfs: - /my-run:size=10M - /my-app-tmp:size=20M
docker-compose up -d -t 0      
docker exec -it compose-tuts_alpine_1 /bin/sh
/ # df -h /my*
Filesystem Size Used Available Use% Mounted on
tmpfs 20.0M 0 20.0M 0% /my-app-tmp
tmpfs 10.0M 0 10.0M 0% /my-run
/ # dd if=/dev/zero of=/my-run/output.dat  bs=1M  count=15
dd: writing '/my-run/output.dat': No space left on device
11+0 records in
10+0 records out
/ # echo $?
1
/ # df -h /my*
Filesystem Size Used Available Use% Mounted on
tmpfs 20.0M 0 20.0M 0% /my-app-tmp
tmpfs 10.0M 10.0M 0 100% /my-run
/ # dd if=/dev/zero of=/my-app-tmp/output.dat  bs=1M  count=15
15+0 records in
15+0 records out
/ # echo $?
0
/ # df -h /my*
Filesystem Size Used Available Use% Mounted on
tmpfs 20.0M 15.0M 5.0M 75% /my-app-tmp
tmpfs 10.0M 10.0M 0 100% /my-run

env_file and environment

env_file and environment adds environment variables to your containers.

  1. create environment files
  2. refer to those files via docker-compose
  3. then enter / exec container to see if environment variables is defined as expected.
ENV_VAR_A=1
ENV_VAR_B=10
ENV_VAR_C=apple
ENV_VAR_F=CentOS
ENV_VAR_A=2
ENV_VAR_C=pear
ENV_VAR_D=Docker
cat <<EOT >> mybestvars.env
ENV_VAR_A=3
ENV_VAR_B=333
ENV_VAR_C=lemon
ENV_VAR_E=Kubernetes
EOT
nano docker-compose.ymlversion: "3.7"
services:
alpine:
image: alpine:3.8 command: sleep 600 env_file:
- myvars.env
- mynewvars.env
- mybestvars.env
environment:
- ENV_VAR_A=42
- ENV_VAR_E=Apache web server
docker-compose up -d -t 0 
docker exec -it compose-tuts_alpine_1 /bin/sh
/ # printenv
HOSTNAME=1c8abf6141ec
SHLVL=1
HOME=/root
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV_VAR_A=42
ENV_VAR_B=333
ENV_VAR_C=lemon
PWD=/
ENV_VAR_D=Docker
ENV_VAR_E=Apache web server
ENV_VAR_F=CentOS
/ # exit
ENV_VAR_B=333 ... the value from the last file: mybestvars.env
ENV_VAR_C=lemon ... the value from the last file: mybestvars.env
ENV_VAR_D=Docker ... value from mynewvars.env: never overridden anywhere
ENV_VAR_E=Apache web server... the value from environment: in the docker-compose file.
ENV_VAR_F=CentOS ... value from first file: myvars.env : never overridden anywhere

extra_hosts

extra_hosts links hostnames to their ip addresses in the /etc/hosts file inside the container.

nano docker-compose.ymlversion: "3.7"
services:
alpine:
image: alpine:3.8 command: sleep 600 extra_hosts:
- "myhost:123.242.195.42"
- "myotherhost:150.31.209.42"
docker-compose up -d -t 0 docker exec -it compose-tuts_alpine_1 /bin/sh
/ # ip r
default via 172.22.0.1 dev eth0
172.22.0.0/16 dev eth0 scope link src 172.22.0.2
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
123.242.195.42 myhost
150.31.209.42 myotherhost
172.22.0.2 024481ed6ce7
/ # exit

healthcheck

If you are already familiar with Dockerfile health checks, docker-compose health checks are easy: its just some minor syntax differences.

nano docker-compose.ymlversion: "3.7"
services:
alpine:
image: alpine:3.8 command: sleep 600 healthcheck:
test: exit 0
interval: 1s
timeout: 1s
retries: 3
start_period: 0s
docker-compose up -d -t 0docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                           PORTS               NAMES
2979837d1a56 alpine:3.8 "sleep 600" 3 seconds ago Up 1 second (health: starting)
docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
d7ceefcc2ad2 alpine:3.8 "sleep 600" 6 seconds ago Up 4 seconds (healthy) compose-tuts_alpine_1
test: exit 1
docker-compose up -d -t 0 
docker ps -a
docker ps -a
docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
ed54a3189e7a alpine:3.8 "sleep 600" 6 seconds ago Up 5 seconds (unhealthy) compose-tuts_alpine_1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

4.97K Followers

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com