Practical Exercises for Docker Compose: Part 3

depends_on

depends_on declares dependencies between services.

nano docker-compose.ymlversion: "3.7"services:alpine:image: alpine:3.8command: sleep 600depends_on:- service-2- service-3- service-4container_name: main-containerservice-2:image: alpine:3.8container_name: service-2command: sleep 600service-3:image: alpine:3.8container_name: service-3command: sleep 600service-4:image: alpine:3.8container_name: service-4command: sleep 600
docker-compose up -d -t 0
Creating service-3 ... doneCreating service-4 ... doneCreating service-2 ... doneRecreating compose-tuts_alpine_1 ... done
docker-compose down -t 0
Stopping main-container ... doneStopping service-4  ... doneStopping service-3  ... doneStopping service-2  ... doneRemoving main-container ... doneRemoving service-4  ... doneRemoving service-3  ... doneRemoving service-2  ... doneRemoving network compose-tuts_default
docker-compose up -d -t 0
Creating network "compose-tuts_default" with the default driverCreating service-3 ... doneCreating service-2 ... doneCreating service-4 ... doneCreating main-container ... done

volumes

volumes are used to mount named volumes.

nano docker-compose.ymlversion: "3.7"services:alpine:image: alpine:3.8command: sleep 600depends_on:- service-2- service-3- service-4container_name: main-containerservice-2:image: alpine:3.8container_name: service-2command: sleep 600volumes:- demo-data-volume:/root/dir1/dir2service-3:image: alpine:3.8container_name: service-3command: sleep 600volumes:- demo-data-volume:/root/dira/dirbservice-4:image: alpine:3.8container_name: service-4command: sleep 600volumes:- demo-data-volume:/root/1/2/3/4volumes:demo-data-volume:
docker-compose up -d -t 0
Creating network "compose-tuts_default" with the default driverCreating volume "compose-tuts_demo-data-volume" with default driverCreating service-2 ... doneCreating service-4 ... doneCreating service-3 ... doneCreating main-container ... done
docker volume ls
DRIVER  VOLUME NAMElocal  compose-tuts_demo-data-volume
service-2:volumes:- demo-data-volume:/root/dir1/dir2service-3:volumes:- demo-data-volume:/root/dira/dirbservice-4:volumes:- demo-data-volume:/root/1/2/3/4
docker exec -it service-2 /bin/sh
/ # ls /root/dir1dir2/ # echo from service 2 > /root/dir1/dir2/myoutfile/ # exit
docker exec -it service-3 /bin/sh
/ # ls /root/dira/dirbmyoutfile/ # cat /root/dira/dirb/myoutfilefrom service 2/ # exit
service-3:volumes:- demo-data-volume:/root/dira/dirb
service-4:volumes:- demo-data-volume:/root/1/2/3/4
docker exec -it service-4 /bin/sh
/ # cat /root/1/2/3/4/myoutfilefrom service 2/ # exit

restart

restart is applied when starting a service using docker-compose up

  1. restart: “no” … the default value
  2. restart: always
  3. restart: on-failure
  4. restart: unless-stopped

Demo for restart: “no” … the default value

Add the following to your docker-compose.yml using

nano docker-compose.ymlversion: "3.7"services:alpine:image: alpine:3.8command: sleep 1
docker-compose up -d -t 0
docker ps -a

Demo for restart: unless-stopped

Add the following to your docker-compose.yml using

nano docker-compose.ymlversion: "3.7"services:alpine:image: alpine:3.8command: sleep 1restart: unless-stopped
docker container prune -f;docker ps -a
docker-compose up -d -t 0
docker ps -a

Demo for restart: always

Add the following to your docker-compose.yml using

nano docker-compose.ymlversion: "3.7"services:alpine:image: alpine:3.8command: sleep 1restart: always
docker-compose up -d -t 0
docker ps -a
docker container stop your-container-id does not stop it.docker-compose down removes the container within seconds.
command: sleep 1; exit 1

Demo for restart: on-failure

You learn new software by reading the docs and then using it.

init: not set to true

From https://docs.docker.com/compose/compose-file/#init

stop_grace_period = 10 seconds default.
nano docker-compose.ymlversion: "3.7"services:alpine:image: alpine:3.8
docker-compose up -d -t 0
docker ps -adocker exec -it  your-container-id  /bin/sh
/ # psPID  USER  TIME  COMMAND1 root  0:00 sleep 60011 root  0:00 /bin/sh16 root  0:00 ps/ # exit
docker container stop your-container-id
2018-11-12T09:51:46.112995194+02:00 container kill b676e3bf51f9f1f99edc531ffa04b0ab164834cb218fe9b78abb65b396782d6a (com.docker.compose.config-hash=c19f3ca3bb22826fee98596d3cb40c4f403f8a51cede518945f5ef37bb989589, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=compose-tuts, com.docker.compose.service=alpine, com.docker.compose.version=1.22.0, image=alpine:3.8, name=compose-tuts_alpine_1, signal=15)2018-11-12T09:51:56.134707392+02:00 container kill b676e3bf51f9f1f99edc531ffa04b0ab164834cb218fe9b78abb65b396782d6a (com.docker.compose.config-hash=c19f3ca3bb22826fee98596d3cb40c4f403f8a51cede518945f5ef37bb989589, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=compose-tuts, com.docker.compose.service=alpine, com.docker.compose.version=1.22.0, image=alpine:3.8, name=compose-tuts_alpine_1, signal=9)2018-11-12T09:51:56.307015745+02:00 container die b676e3bf51f9f1f99edc531ffa04b0ab164834cb218fe9b78abb65b396782d6a (com.docker.compose.config-hash=c19f3ca3bb22826fee98596d3cb40c4f403f8a51cede518945f5ef37bb989589, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=compose-tuts, com.docker.compose.service=alpine, com.docker.compose.version=1.22.0, exitCode=137, image=alpine:3.8, name=compose-tuts_alpine_1)2018-11-12T09:51:56.387969293+02:00 network disconnect 9802e2506ea80e7597f0b018450f86fa0c7dcc045a920702cc0e463aacfda84f (container=b676e3bf51f9f1f99edc531ffa04b0ab164834cb218fe9b78abb65b396782d6a, name=compose-tuts_default, type=bridge)2018-11-12T09:51:56.454351535+02:00 container stop b676e3bf51f9f1f99edc531ffa04b0ab164834cb218fe9b78abb65b396782d6a (com.docker.compose.config-hash=c19f3ca3bb22826fee98596d3cb40c4f403f8a51cede518945f5ef37bb989589, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=compose-tuts, com.docker.compose.service=alpine, com.docker.compose.version=1.22.0, image=alpine:3.8, name=compose-tuts_alpine_1)

init: true

Now observe how init handles the stop properly:

nano docker-compose.ymlversion: "3.7"services:alpine:image: alpine:3.8command: sleep 600init: true
docker-compose up -d -t 0
docker ps -adocker exec -it  your-container-id  /bin/sh
/ # psPID  USER  TIME  COMMAND1 root  0:00 /dev/init -- sleep 6006 root  0:00 sleep 6007 root  0:00 /bin/sh12 root  0:00 ps/ # exit
docker container stop your-container-id
2018-11-12T09:50:14.892684432+02:00 container kill 7973ccec4849cf8759821d613e1b4870c68d76f396fefcfb1cd1bf1f7a3bd509 (com.docker.compose.config-hash=8d5184879de73d9f624319983822eafca40959cbe9f91929ff4648ace18acc6a, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=compose-tuts, com.docker.compose.service=alpine, com.docker.compose.version=1.22.0, image=alpine:3.8, name=compose-tuts_alpine_1, signal=15)2018-11-12T09:50:15.019039627+02:00 container die 7973ccec4849cf8759821d613e1b4870c68d76f396fefcfb1cd1bf1f7a3bd509 (com.docker.compose.config-hash=8d5184879de73d9f624319983822eafca40959cbe9f91929ff4648ace18acc6a, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=compose-tuts, com.docker.compose.service=alpine, com.docker.compose.version=1.22.0, exitCode=143, image=alpine:3.8, name=compose-tuts_alpine_1)2018-11-12T09:50:15.104203647+02:00 network disconnect 9802e2506ea80e7597f0b018450f86fa0c7dcc045a920702cc0e463aacfda84f (container=7973ccec4849cf8759821d613e1b4870c68d76f396fefcfb1cd1bf1f7a3bd509, name=compose-tuts_default, type=bridge)2018-11-12T09:50:15.149018229+02:00 container stop 7973ccec4849cf8759821d613e1b4870c68d76f396fefcfb1cd1bf1f7a3bd509 (com.docker.compose.config-hash=8d5184879de73d9f624319983822eafca40959cbe9f91929ff4648ace18acc6a, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=compose-tuts, com.docker.compose.service=alpine, com.docker.compose.version=1.22.0, image=alpine:3.8, name=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

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