Docker healthcheck in composer file

I am trying to integrate a new health check into my docker system, but I really do not know how to do it correctly: /

The problem is that my database container needs more time to start and initialize the database, and then the container that launches my main application. As a result: the main container does not start correctly, the reason for the lack of a connection to the database. I wrote a healthcheck.sh script file to check the database container for the connection, so the main container starts loading after the connection. But I do not know how to integrate it correctly in Dockerfile and my docker-compose.yml

healthcheck.sh:

#!bin/bash COUNTER=0 while [[ $COUNTER = 0 ]]; do mysql --host=HOST --user="user" --password="password" --database="databasename" --execute="SELECT 1"; if [[ $? == 1 ]]; then sleep 1 echo "Let sleep again" else COUNTER=1 echo "OK, lets go!" fi done 

mysql Dockerfile container:

 FROM repository/mysql-5.6:latest MAINTAINER Me ... some copies, chmod and so on VOLUME ["/..."] EXPOSE 3306 CMD [".../run.sh"] HEALTHCHECK --interval=1s --timeout=3s CMD ./healthcheck.sh 

docker-compose.yml like:

 version: '2' services: db: image: db image restart: always dns: - 10. ports: - "${MYSQL_EXTERNAL_PORT}:${MYSQL_INTERNAL_PORT}" environment: TZ: Europe/Berlin data: image: data image main application: image: application image restart: always dns: - 10. ports: - "${..._EXTERNAL_PORT}:${..._INTERNAL_PORT}" environment: TZ: Europe/Berlin volumes: - ${HOST_BACKUP_DIR}:/... volumes_from: - data - db 

What do I need to do to integrate this health check into the docker-compose.yml file? Or is there another chance to delay the launch of the container of my main container?

thanks Markus

+5
source share
3 answers

Since docker-compose 1.10.0 you can specify healthchecks in your file to create: https://github.com/docker/docker.imtqy.com/blob/master/compose/compose-file.md#healthcheck

It uses https://docs.docker.com/engine/reference/builder/#/healthcheck , which was introduced using Docker 1.12

+3
source

In general, your application should be able to cope with inaccessible resources, but there are also some cases at startup where it’s quite convenient to have one container that expects the other to be “fully accessible”. Docker itself does not handle this for you, but there are ways to handle the launch in a container using a resource, delaying the actual command with some script.

There is a good example of postgresql startup check, which can be used in any container that needs to wait until the database is fully started. See sample code in docker docs: https://docs.docker.com/compose/startup-order/

+2
source

I find it looks like Docker Compose is waiting for container X to run Y

Your db_image should support curl.
To do this, create your own db_image as:

 FROM base_image:latest RUN apt-get update RUN apt-get install -y curl EXPOSE 3306 

Then you only need docker-compose.yml, which looks like this:

 version: '2' services: db: image: db_image restart: always dns: - 10. ports: - "${MYSQL_EXTERNAL_PORT}:${MYSQL_INTERNAL_PORT}" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:${MYSQL_INTERNAL_PORT}"] interval: 30s timeout: 10s retries: 5 environment: TZ: Europe/Berlin main_application: image: application_image restart: always depends_on: db: condition: service_healthy links: - db dns: - 10. ports: - "${..._EXTERNAL_PORT}:${..._INTERNAL_PORT}" environment: TZ: Europe/Berlin volumes: - ${HOST_BACKUP_DIR}:/... volumes_from: - data - db 
+1
source

All Articles