撰写器文件中的Docker Healthcheck [英] Docker healthcheck in composer file
问题描述
我尝试将新的healthcheck集成到我的docker系统中,但我真的不知道如何以正确的方式进行操作:/
I try to integrate the new healthcheck into my docker system, but I don't really know how to do it in the right way :/
问题是,我的数据库容器比启动我的主应用程序的容器需要更多的时间来启动和初始化数据库。
结果:主容器将无法正确启动,这是缺少数据库连接的原因。
我编写了healthcheck.sh脚本来检查数据库容器的连接性,因此在连接可用后主容器开始启动。但是我不知道如何将其正确集成到Dockerfile和我的docker-compose.yml
The problem is, my database container needs more time to start up and initialize the database then the container who starts my main application. As a result: the main container wont start correct, cause of the missing database connection. I wrote an healthcheck.sh script to check the database container for connectivity, so the main container starts booting after the connectivity is available. But I dont know how to integrate it correctly in the Dockerfile and my docker-compose.yml
healthcheck.sh中,就像这样:
healthcheck.sh is like:
#!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's sleep again"
else
COUNTER=1
echo "OK, lets go!"
fi
done
mysql容器Dockerfile:
mysql container Dockerfile:
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,例如:
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
要将此运行状况检查集成到我的docker-compose.yml文件中,我该怎么做?
还是有其他机会延迟我的主容器的容器启动?
What do I have to do to integrate this healthcheck into my docker-compose.yml file to work? Or is there any other chance to delay the container startup of my main container?
Thx Markus
Thx Markus
推荐答案
我认为这类似于 Docker Compose在开始Y之前等待容器X
您的db_image需要支持curl。
为此,请创建您自己的db_image:
Your db_image needs to support curl.
To do that, create your own db_image as:
FROM base_image:latest
RUN apt-get update
RUN apt-get install -y curl
EXPOSE 3306
然后您需要做的是如下所示的docker-compose.yml:
Then all you should need is a docker-compose.yml that 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
这篇关于撰写器文件中的Docker Healthcheck的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!