使用 `docker-compose` 重用容器 [英] Reuse containers with `docker-compose`
问题描述
我有一个应用程序在 docker-compose
定义的多个 Docker 容器上运行.我的用户一切正常,docker-compose ps
输出如下所示:
I've an app running on multiple Docker containers defined by docker-compose
. Everything works fine from my user and the docker-compose ps
output looks like:
Name Command State Ports
------------------------------------------------------------
myuser_app_1 /config/bootstrap.sh Exit 137
myuser_data_1 sh Exit 0
myuser_db_1 /run.sh Exit 143
现在我正在尝试使用 supervisord
运行 docker-compose up
(请参阅下面 supervisord.conf
的相关部分)和问题是容器现在被命名为 myapp_app_1
、myapp_data_1
和 myapp_db_1
,也就是说它们是从头开始创建的,对以前容器的所有自定义都是丢了.
Now I'm trying to run docker-compose up
with supervisord
(see relevant part of supervisord.conf
below) and the issue is that the containers are now named myapp_app_1
, myapp_data_1
and myapp_db_1
, that is they're created from scratch and all customizations on the former containers is lost.
我尝试重命名容器,但它给出了一个错误,指出已经有一个具有该名称的容器.
I tried renaming the containers, but it gives an error saying that there's already a container with that name.
问:有什么方法可以强制 docker-compose
重用现有容器,而不是根据各自的图像创建新容器?
Q: Is there some way to force docker-compose
reuse existing containers instead of creating new ones based in their respective images?
supervisord.conf
supervisord.conf
...
[program:myapp]
command=/usr/local/bin/docker-compose
-f /usr/local/app/docker-compose.yml up
redirect_stderr=true
stdout_logfile=/var/log/myapp_container.log
stopasgroup=true
user=myuser
推荐答案
Compose 将始终重用容器,只要它们的配置没有改变.
如果容器中有任何状态,则需要将该状态放入卷中.容器应该是短暂的,您应该能够随时销毁它们并重新创建它们而不会丢失任何东西.
If you have any state in a container, you need to put that state into a volume. Containers should be ephemeral, you should be able to destroy them and recreate them at any time without losing anything.
如果您需要初始化某些内容,我会在 Dockerfile 中进行,以便将其保留在映像中.
If you need to initialize something I would do it in the Dockerfile, so that it's preserved in the image.
这篇关于使用 `docker-compose` 重用容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!