英文:
How to restart docker containers in custom order after server restarts?
问题
如何在服务器重启时按自定义顺序重新启动 Docker 容器?
嗨,大家好,所以我正在尝试在服务器重新启动时启动所有我的 Docker 容器,我大约有 30 个容器,我已经了解到可以使用以下命令来实现:
docker update --restart unless-stopped <container>
但问题是,当编译时,其中 7 个容器使用了大量的 RAM,所以如果服务器重新启动并且所有容器同时启动,我的服务器将因为 RAM 不足而崩溃,因此我需要启动一个容器,等待直到它完全就绪(因为这时 RAM 又可用了),然后再启动另一个容器...
我已经了解了 dockerize 但不确定如何使用它。有什么想法吗?
提前感谢您的帮助,对我的英语不好,还请见谅。
英文:
How can I restart docker containers in custom order when the server restarts?
Hi guys, so I'm trying to start all my docker containers if my server restarts, I have around 30 containers, I've read about using
docker update --restart unless-stopped <container>
but the problem is that 7 containers uses a lot of RAM when compiling, so if the server restarts and all container start at the same time my server is going to crash because it is out of RAM, so I need to start one container, wait until it is completely ready (because here RAM is free again) and start the other container...
I've read about dockerize but I'm not sure how to use it. Any ideas?
Thanks in advance and sorry for my english.
答案1
得分: 0
你可以使用 Docker 的 depend_on
功能,使下一个容器依赖于前一个容器。以下是示例的 Docker Compose 文件:
version: '3'
services:
container1:
image: image1
restart: always
container2:
image: image2
restart: always
depends_on:
- container1
container3:
image: image3
restart: always
depends_on:
- container2
...
container30:
image: image30
restart: always
depends_on:
- container29
这是官方文档和其他可能性:Docker 文档
或者第二种解决方案是编写一个脚本,在系统重新启动后逐个启动容器。为此,您需要删除 restart: unless-stopped
:
#!/bin/bash
# 定义容器的数组
containers=("container1" "container2" "container3")
# 定义要在每个容器日志中等待的关键词的数组
log_words=("word1" "word2" "word3")
for i in "${!containers[@]}"
do
container=${containers[$i]}
log_word=${log_words[$i]}
echo "Starting $container..."
# 启动容器
docker start $container
echo "Waiting for $log_word in $container logs..."
# 跟踪容器的日志并等待指定的关键词
while true
do
log=$(docker logs --tail 1 $container)
if [[ $log == *"$log_word"* ]]; then
echo "$log_word found in $container logs. Continuing to the next container..."
break
else
# 在再次检查日志之前等待一秒钟
sleep 1
fi
done
done
英文:
You could do this with docker depend_on
and make next container dependent on the previous container. Here is sample docker compose file:
version: '3'
services:
container1:
image: image1
restart: always
container2:
image: image2
restart: always
depends_on:
- container1
container3:
image: image3
restart: always
depends_on:
- container2
...
container30:
image: image30
restart: always
depends_on:
- container29
Here is official documentation and other possibilities: Docker Documentation
or second solution would be to write a script which starts the container one by one after system reboot. for that you have to remove restart: unless-stopped
#!/bin/bash
# Define an array of your containers
containers=("container1" "container2" "container3")
# Define an array of the words you want to wait for in each container's logs
log_words=("word1" "word2" "word3")
for i in "${!containers[@]}"
do
container=${containers[$i]}
log_word=${log_words[$i]}
echo "Starting $container..."
# Start the container
docker start $container
echo "Waiting for $log_word in $container logs..."
# Follow the container's logs and wait for the specified word
while true
do
log=$(docker logs --tail 1 $container)
if [[ $log == *"$log_word"* ]]; then
echo "$log_word found in $container logs. Continuing to the next container..."
break
else
# Wait for a second before checking the logs again
sleep 1
fi
done
done
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论