简体   繁体   English

使用 `docker-compose` 重用容器

[英]Reuse containers with `docker-compose`

I've an app running on multiple Docker containers defined by docker-compose .我有一个应用程序在 docker docker-compose定义的多个 Docker 容器上运行。 Everything works fine from my user and the docker-compose ps output looks like:我的用户一切正常, docker-compose ps输出如下所示:

       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

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.现在我正在尝试使用supervisord运行docker-compose up (请参阅下面的supervisord.conf相关部分),问题是容器现在被命名为myapp_app_1myapp_data_1myapp_db_1 ,也就是说它们是从头开始创建的并且所有自定义在以前的容器上丢失了。

I tried renaming the containers, but it gives an error saying that there's already a container with that name.我尝试重命名容器,但它给出了一个错误,指出已经有一个具有该名称的容器。

Q: Is there some way to force docker-compose reuse existing containers instead of creating new ones based in their respective images?问:有什么方法可以强制docker-compose重用现有容器,而不是根据各自的图像创建新容器?

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 will always reuse containers as long as their config hasn't changed . 只要配置没有改变, 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. 容器应该是短暂的,你应该能够销毁它们并在任何时候重新创建它们而不会丢失任何东西。

If you need to initialize something I would do it in the Dockerfile, so that it's preserved in the image. 如果你需要初始化一些东西,我会在Dockerfile中做,所以它保存在图像中。

仅供参考:-g标志自v17.05.0起不推荐使用,并被--data-root标志取代...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM