Practical Exercises for Docker Compose: Part 1

Prerequisites

Clean Up Preparation

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

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

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_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

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

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