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
source share