用'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
现在我试图运行 docker-compose up
与 supervisord
(请参阅 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
推荐答案
如果容器中有任何状态,则需要将该状态放入卷中。容器应该是短暂的,你应该能够在任何时间摧毁它们并重新创建它们,而不会丢失任何东西。
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屋!