In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how compose controls the start-up sequence of services in docker. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Summary
Docker-compose can easily compose multiple docker container services, but when there is a dependency between container services, docker-compose can not guarantee the startup order of the services.
The depends_on configuration in docker-compose is the startup order of the container, not the startup order of the service in the container.
Problem recurrence
First, we construct an example to demonstrate the problems caused by docker-compose. The docker-compose.yml file is as follows:
Version: '2'services: web: image: ubuntu:14.04 depends_on:-web command: nc-z database 3306 database: image: ubuntu:14.04 command: > / bin/bash-c' sleep 5; echo "sleep over"; nc-lk 0.0.0.0 3306;'
After startup, you can find that it is true to start database first, and then start web, but the service in database is completed after about 5 seconds, so the startup of web fails.
$docker-compose upCreating tmp_database_1... DoneCreating tmp_database_1... Creating tmp_web_1... DoneAttaching to tmp_database_1, tmp_web_1tmp_web_1 exited with code 1database_1 | sleep over
Problem Resolution 1.0
Modify the startup script of web and wait for the port of database to open before starting the service
Version: '2'services: web: image: ubuntu:14.04 depends_on:-database command: > / bin/bash-c' while! Nc-z database 3306; do echo "wait for database"; sleep 1; done; echo "database is ready!"; echo "start web service here"; 'database: image: ubuntu:14.04 command: > / bin/bash-c' sleep 5; echo "sleep over"; nc-lk 0.0.0.0 3306;'
Start again
$docker-compose upCreating tmp_database_1... DoneCreating tmp_database_1... Creating tmp_web_1... DoneAttaching to tmp_database_1, tmp_web_1web_1 | wait for databaseweb_1 | wait for databasedatabase_1 | sleep overweb_1 | database is readywebsite 1 | start web service heretmp_web_1 exited with code 0
Web will not start until the database boot is complete and the port is open.
Problem solving 2.0
Although the above solution can solve the problem, inserting scripts directly into yaml is difficult to maintain and error-prone. If there are multiple dependencies, or multiple layers of dependencies, the complexity will rise in a straight line.
So, to encapsulate an entrypoint.sh script, you can accept startup commands, as well as the services and ports you need to wait. The script reads as follows:
#! / bin/bash#set-x please contact me # @ file: entrypoint.sh# @ author: wangyubin# @ date: 2018-08-1 10:18:43 # # @ brief: entry point for manage service start order# history: init#****: ${SLEEP_SECOND:=2} wait_for () {echo Waiting for $1 to listen on $2. While! Nc-z $1 $2; do echo waiting...; sleep $SLEEP_SECOND; done} declare DEPENDSdeclare CMDwhile getopts "dvc:" argdo case $arg in d) DEPENDS=$OPTARG;; c) CMD=$OPTARG;;?) Echo "unkonw argument" exit 1;; esacdonefor var in ${DEPENDS//,/} do host=$ {var%:*} port=$ {var#*:} wait_for $host $portdoneeval $CMD
This script has two parameters,-d needs to wait for the service and port, and-c waits for the service and port to start after its own startup command
Modify the docker-compose.yml and use the entrypoint.sh script to control the startup sequence.
Version: '2'services: web: image: ubuntu:14.04 depends_on:-database volumes: -. / entrypoint.sh:/entrypoint.sh "entrypoint: / entrypoint.sh-d database:3306-c' echo" start web service here "'; database: image: ubuntu:14.04 command: > / bin/bash-c 'sleep 5; echo" sleep over "; nc-lk 0.0.0.0 3306;'
In practice, entrypoint.sh can also be packaged into a published image without having to load entrypoint.sh scripts through volumes configuration.
The test results are as follows:
$docker-compose upStarting tmp_database_1... DoneStarting tmp_web_1... DoneAttaching to tmp_database_1, tmp_web_1web_1 | Waiting for database to listen on 3306...web_1 | waiting...database_1 | sleep overweb_1 | start web service heretmp_web_1 exited with code 0
Supplement
Rely on multiple services and ports
Using the entrypoint.sh script above, you can also rely on multiple services and ports, which are separated by commas (,) after the-d parameter.
Version: '2'services: web: image: ubuntu:14.04 depends_on:-mysql-postgresql volumes: -. / entrypoint.sh:/entrypoint.sh "entrypoint: / entrypoint.sh-d mysql:3306,postgresql:5432-c' echo" start web service here "'; mysql: image: ubuntu:14.04 command: > / bin/bash-c 'sleep 4; echo" sleep over "; nc-lk 0.0.0.0 3306 'postgresql: image: ubuntu:14.04 command: > / bin/bash-c' sleep 8; echo "sleep over"; nc-lk 0.0.0.0 5432;'
The effect of execution can be tried on your own.
Try the configuration of the interval
The wait time for each connection attempt can be configured by the environment variable SLEEP_SECOND. If the configuration wait time below 2 seconds is set to 4 seconds by default, you can connect when you try the mysql service every 4 seconds.
Version: '2'services: web: image: ubuntu:14.04 environment: SLEEP_SECOND: 4 depends_on:-mysql volumes: -. / entrypoint.sh:/entrypoint.sh "entrypoint: / entrypoint.sh-d mysql:3306' echo" start web service here "; mysql: image: ubuntu:14.04 command: > / bin/bash-c 'sleep 4; echo" sleep over "; nc-lk 0.0.0.0 3306 This is the end of the article on "how to control the startup order of services in compose in docker". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.