Wait for the container service to start with docker healthcheck

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 out

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

Wait for the container service to start with docker healthcheck
Wait for PostgreSQL to start with Docker and then start the WEB service
[Docker] How to see the contents of Volumes. Start a container with root privileges.
[Linux] Start Apache container with Docker
How to start Camunda with Docker
[Docker] Start the container as soon as possible
How to start a Docker container with a volume mounted in a batch file
How to check the logs in the Docker container
Setting to exit from Docker container with VScode
Docker Container Operations with Docker-Client API for Java
How to use nginx-ingress-controller with Docker for Mac
How to mount the batch file location via WSL2 and start the Docker container
How to display the amount of disk used by Docker container for each container
How to start with Hyper-V instead of WSL2 on Docker Desktop for Windows
Automatically start the container
How to build docker environment with Gradle for intelliJ
`Failed to create the host-only adapter` with docker-machine start`
Try connecting to AzureCosmosDB Emulator for Docker with Java
How to make Laravel faster with Docker for Mac
Command to try using Docker for the time being
Docker for Windows: MySQL container does not start when migrating from Hyper-v to WSL2
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
Solution for the error "no basic auth credentials" when pushing a Docker container to Heroku
[Docker] Delete only the volume associated with a specific container
Time is wrong with the application launched on the Docker container
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to build an environment with Docker, which is the minimum required to start a Rails application
Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
When requested access to the resource is denied when pushing with Docker
If the DB container does not start with Initializing database files
Summary of steps for developing in Docker container with VS Code
I tried to build the environment of PlantUML Server with Docker
Super beginner builds Rails6 + Postgresql environment with Docker to the end
Check the contents by docker pull from ECR and start the container
Setting to start multiple units at the same time with Vagrant
Until you build the docker environment and start / stop the Ubuntu container
"Wait for the process to finish." And kill the process because it remains.
Minimal Workflow to push Docker image to Github Container Registry with Github Actions
[Note] How to restart the Windows container set up with docker-compose
[GCP] Until you push the local Docker image to the Container Registry
[For those who create portfolios] How to use binding.pry with Docker
Introduction to Linux Container / Docker (Part 1)
Environment construction with Docker for beginners
Articles referred to for incorporating Docker
Introduction to Linux Container / Docker (Part 2)
[SRE / Docker] Start control with Dockerize
mysql doesn't start up with docker.
Pass environment variables to docker container
Java, arrays to start with beginners
[Docker] Operation up to container creation # 2
docker single container restart for myself
Container does not start with docker-compose
Update MySQL from 5.7 to 8.0 with Docker
Build WebRTC Janus with Docker container
[Almost free] How to get a domain and SSL certificate for 0 yen using Freenom and Let's Encrypt, put docker in AWS EC2, start an nginx container, and launch an HTTPS WEB service that connects with the domain
Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image
Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
I tried running the Angular sample in Auth0 Quick Start with Docker
For the time being, run the war file delivered in Java with Docker
Problems I was addicted to when building the digdag environment with docker