Overview
If you specify the container dependency with depends_on
of docker-compose, you can control the container startup order, but
Is the launched container working properly? Because it does not guarantee until
There are many troublesome cases such as "Please do your best on the application side such as wait-for-it to check whether the middleware container is started properly".
https://docs.docker.jp/compose/startup-order.html
So, docker has a healthcheck function, https://dev.classmethod.jp/articles/docker-healthcheck/ The story is that using this will make you wait for the dependent container to start until it becomes healthy.
Try putting redis in alpine.
Dockerfile
FROM alpine:latest
RUN apk add make redis
Makefile
node1: #The side to be kept waiting
@ echo "$@ is running!"
redis-cli -h node2 ping
node2: #The side that makes you wait
@ echo "$@ started!"
sleep 3 #Wait 3 seconds
redis-server --protected-mode no # redis-start server
@ echo "$@ ended!"
docker-compose.yml
version: '3'
services:
node1:
image: hoge
build:
context: .
command: make -f /mnt/Makefile node1
depends_on:
node2: #Wait for startup until node2 healthcheck passes
condition: service_healthy
volumes:
- .:/mnt
node2:
image: hoge
build:
context: .
command: make -f /mnt/Makefile node2
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 1s
timeout: 30s
retries: 30
volumes:
- .:/mnt
with this
bash
$ docker-compose build
do it
bash
$ docker-compose up
Then, while sleeping for 3 seconds before starting redis on node2 side, healthcheck redis-cli ping
does not pass, so
You can see the behavior of waiting for node1 to start.
Like this
$ docker-compose up
Creating network "docker_default" with the default driver
Creating docker_node2_1 ... done
Creating docker_node1_1 ... done
Attaching to docker_node2_1, docker_node1_1
node2_1 | node2 is started!
node2_1 | sleep 3 #Wait 3 seconds
node1_1 | node1 is running!
node1_1 | redis-cli -h node2 ping
node2_1 | redis-server --protected-mode no # redis-start server
node2_1 | 22:C 22 Dec 2020 09:38:25.036 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
node2_1 | 22:C 22 Dec 2020 09:38:25.036 # Redis version=5.0.9, bits=64, commit=869dcbdc, modified=0, pid=22, just started
node2_1 | 22:C 22 Dec 2020 09:38:25.036 # Configuration loaded
node1_1 | PONG
node2_1 | 22:M 22 Dec 2020 09:38:25.039 * Running mode=standalone, port=6379.
node2_1 | 22:M 22 Dec 2020 09:38:25.039 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
node2_1 | 22:M 22 Dec 2020 09:38:25.039 # Server initialized
node2_1 | 22:M 22 Dec 2020 09:38:25.039 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
node2_1 | 22:M 22 Dec 2020 09:38:25.040 * Ready to accept connections
docker_node1_1 exited with code 0
The story.
cf.
https://til.codes/health-check-option-in-docker-to-wait-for-dependent-containers-to-be-healthy/
Recommended Posts