问题描述
我不知道自己做错了什么,但是如果不首先从系统中完全删除旧容器,我就无法让 docker-compose up
使用我们注册表中的最新映像.尽管 docker-compose pull 获取了更新的图像,但看起来 compose 正在使用之前启动的图像.
I don't know what I'm doing wrong, but I simply cannot get docker-compose up
to use the latest image from our registry without first removing the old containers from the system completely. It looks like compose is using the previously started image even though docker-compose pull has fetched a newer image.
我看了如何获取docker-compose总是从新图像重新创建容器? 这似乎与我的问题相似,但是那里提供的解决方案都不适用于我,因为我正在寻找可以在生产服务器上使用的解决方案,并且在那里我不想在再次启动它们之前删除所有容器(可能会丢失数据?).我希望 compose 仅检测更改图像的新版本,提取它们,然后使用这些新图像重新启动服务.
I looked at How to get docker-compose to always re-create containers from fresh images? which seemed to be similar to my issue, but none of the provided solutions there work for me, since I'm looking for a solution I can use on the production server and there I don't want to be removing all containers before starting them again (possible data loss?). I would like for compose only to detect the new version of the changed images, pull them and then restart the services with those new images.
我为此创建了一个简单的测试项目,其中唯一的目标是在每个新构建中增加一个版本 nr.如果我浏览到创建的 nginx 服务器,则会显示版本 nr(这在本地按预期工作).
I created a simple test project for this in which the only goal is to get a version nr to increase on each new build. The version nr is displayed if I browse to the nginx server that is created (this works as expected locally).
docker 版本:1.11.2docker-compose 版本:1.7.1操作系统:使用 docker-toolbox 在 CentOS 7 和 OS X 10.10 上测试
docker version: 1.11.2docker-compose version: 1.7.1OS: tested on both CentOS 7 and OS X 10.10 using docker-toolbox
我的 docker-compose.yml:
My docker-compose.yml:
version: '2'
services:
application:
image: ourprivate.docker.reg:5000/ourcompany/buildchaintest:0.1.8-dev
volumes:
- /var/www/html
tty: true
nginx:
build: nginx
ports:
- "80:80"
volumes_from:
- application
volumes:
- ./logs/nginx/:/var/log/nginx
php:
container_name: buildchaintest_php_1
build: php-fpm
expose:
- "9000"
volumes_from:
- application
volumes:
- ./logs/php-fpm/:/var/www/logs
在我们的 jenkins 服务器上,我运行以下命令来构建和标记图像
on our jenkins server I run the following to build and tag the image
cd $WORKSPACE && PROJECT_VERSION=$(cat VERSION)-dev
/usr/local/bin/docker-compose rm -f
/usr/local/bin/docker-compose build
docker tag ourprivate.docker.reg:5000/ourcompany/buildchaintest ourprivate.docker.reg:5000/ourcompany/buildchaintest:$PROJECT_VERSION
docker push ourprivate.docker.reg:5000/ourcompany/buildchaintest
这似乎正在做它应该做的事情,因为每次构建完成并且版本 nr 被碰撞时,我都会在我们的存储库中获得一个新的版本标签.
this seems to be doing what it's supposed to be since I get a new version tag in our repository each time the build completes and the version nr has been bumped.
如果我现在跑
docker-compose pull && docker-compose -f docker-compose.yml up -d
在我电脑上的一个文件夹中,其中的内容只有 docker-compose.yml 和构建 nginx 和 php 服务所需的 Dockerfile,我得到的输出不是最新的版本号,因为已在注册表中标记或显示在 docker-compose.yml (0.1.8) 中,但之前的版本是 0.1.7.然而,pull 命令的输出表明已获取新版本的图像:
in a folder on my computer, where the contents is only the docker-compose.yml and the necessary Dockerfiles to build the nginx and php services, the output I get is not the latest version number as has been tagged in the registry or is shown in the docker-compose.yml (0.1.8), but the version before that, which is 0.1.7. However the output of the pull command would suggest that a new version of the image was fetched:
Pulling application (ourprivate.docker.reg:5000/ourcompany/buildchaintest:latest)...
latest: Pulling from ourcompany/buildchaintest
Digest: sha256:8f7a06203005ff932799fe89e7756cd21719cccb9099b7898af2399414bfe62a
Status: Downloaded newer image for docker.locotech.fi:5000/locotech/buildchaintest:0.1.8-dev
仅当我运行时
docker-compose stop && docker-compose rm -f
然后运行 docker-compose up
命令是否让新版本按预期显示在屏幕上.
and then run the docker-compose up
command do I get the new version to show up on screen as expected.
这是 docker-compose 的预期行为吗?即我是否应该在再次运行 up
之前执行 docker-compose rm -f
,即使是在生产服务器上?或者我在这里做了什么反对粮食的事情,这就是为什么它不起作用?
Is this intended behaviour of docker-compose? i.e. should I always do a docker-compose rm -f
before running up
again, even on production servers? Or am I doing something against the grain here, which is why it's not working?
目标是让我们的构建过程构建并创建 docker-compose.yml 中所需图像的标记版本,将它们推送到我们的私有注册表,然后在发布到生产步骤"中简单地复制 docker-compose.yml 到生产服务器并运行 docker-compose pull &&docker-compose -f docker-compose.yml up -d
使新镜像开始生产.如果有人对此有提示,或者可以指出此类设置的最佳实践教程,我们也将不胜感激.
The goal is to have our build process build and create tagged versions of the images needed in a docker-compose.yml, push those to our private registry and then for the "release to production-step" to simply copy the docker-compose.yml to the production server and run a docker-compose pull && docker-compose -f docker-compose.yml up -d
for the new image to start in production. If anyone has tips on this or can point to a best practices tutorial for this kind of setup that would be much appreciated also.
推荐答案
为了结束这个问题,似乎有效的方法确实在运行
To close this question, what seemed to have worked is indeed running
docker-compose stop
docker-compose rm -f
docker-compose -f docker-compose.yml up -d
即在再次运行 up
之前移除容器.
I.e. remove the containers before running up
again.
这样做时需要记住的是,如果您只运行 rm -f
,数据卷容器也会被删除.为了防止我明确指定要删除的每个容器:
What one needs to keep in mind when doing it like this is that data volume containers are removed as well if you just run rm -f
. In order to prevent that I specify explicitly each container to remove:
docker-compose rm -f application nginx php
正如我在问题中所说,我不知道这是否是正确的过程.但这似乎适用于我们的用例,因此在我们找到更好的解决方案之前,我们将继续使用该解决方案.
As I said in my question, I don't know if this is the correct process. But this seems to work for our use case, so until we find a better solution we'll roll with this one.
这篇关于如何让 docker-compose 使用存储库中的最新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!