如何让 docker-compose 始终从新图像重新创建容器? [英] How to get docker-compose to always re-create containers from fresh images?
问题描述
我的 docker 镜像构建在 Jenkins CI 服务器上,并被推送到我们的私有 Docker Registry.我的目标是使用 docker-compose 提供环境,该环境始终启动图像的原始构建状态.
My docker images are built on a Jenkins CI server and are pushed to our private Docker Registry. My goal is to provision environments with docker-compose which always start the originally built state of the images.
我目前在不同的机器上使用 docker-compose 1.3.2 和 1.4.0,但我们之前也使用过旧版本.
I am currently using docker-compose 1.3.2 as well as 1.4.0 on different machines but we also used older versions previously.
我总是使用 docker-compose pull &&docker-compose up -d
命令从注册表中获取新图像并启动它们.我相信我的首选行为在某个时间点按预期工作,但从那时起 docker-compose up
开始重新运行以前停止的容器,而不是每次都启动最初构建的图像.
I always used the docker-compose pull && docker-compose up -d
commands to fetch the fresh images from the registry and start them up. I believe my preferred behaviour was working as expected up to a certain point in time, but since then docker-compose up
started to re-run previously stopped containers instead of starting the originally built images every time.
有没有办法摆脱这种行为?这种方式是否可以连接到 docker-compose.yml 配置文件中,以便在每次调用时不依赖不要忘记"命令行上的某些内容?
Is there a way to get rid of this behaviour? Could that way be one which is wired in the docker-compose.yml configuration file to not depend "not forgetting" something on the command line upon every invocation?
ps.除了找到一种方法来实现我的目标,我也很想知道更多关于这种行为的背景.我认为 Docker 的基本思想是构建一个不可变的基础设施.docker-compose 的当前行为似乎与这种方法完全冲突.. 还是我在这里错过了一些要点?
ps. Besides finding a way to achieve my goal, I would also love to know a bit more about the background of this behaviour. I think the basic idea of Docker is to build an immutable infrastructure. The current behaviour of docker-compose just seem to plain clash with this approach.. or do I miss some points here?
推荐答案
docker-compose up --force-recreate
是一种选择,但如果你将它用于 CI,我会开始使用 docker-compose rm -f
停止和删除容器和卷的构建(然后用 pull 和 up 跟随它).
docker-compose up --force-recreate
is one option, but if you're using it for CI, I would start the build with docker-compose rm -f
to stop and remove the containers and volumes (then follow it with pull and up).
这是我使用的:
docker-compose rm -f
docker-compose pull
docker-compose up --build -d
# Run some tests
./tests
docker-compose stop -t 1
重新创建容器的原因是为了保留可能使用的任何数据量(并且它也恰好使 up
更快).
The reason containers are recreated is to preserve any data volumes that might be used (and it also happens to make up
a lot faster).
如果你在做 CI,你不想要那样,所以只要移除所有东西就可以得到你想要的.
If you're doing CI you don't want that, so just removing everything should get you want you want.
更新:使用在 docker-compose
1.7
Update: use up --build
which was added in docker-compose
1.7
这篇关于如何让 docker-compose 始终从新图像重新创建容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!