如何获取docker-compose以使用存储库中的最新映像 [英] how to get docker-compose to use the latest image from repository

查看:171
本文介绍了如何获取docker-compose以使用存储库中的最新映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道自己在做错什么,但是我不能让 docker-compose up 从注册表中使用最新的映像,而无需先删除旧的系统中的所有容器。即使docker-compose pull获取了新的映像,它似乎仍在使用先前启动的映像。

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总是从新鲜映像中重新创建容器?类似于我的问题,但是那里提供的解决方案都不适合我,因为我正在寻找可以在生产服务器上使用的解决方案,因此我不想在再次启动之前删除所有容器(可能会丢失数据?)。我只想检测更改图像的新版本,将它们拉出,然后使用这些新图像重新启动服务。

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.2
docker-撰写版本:1.7.1
操作系统:使用docker-toolbox在CentOS 7和OS X 10.10上进行了测试

docker version: 1.11.2 docker-compose version: 1.7.1 OS: 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

在我们的詹金斯服务器上,我执行以下操作机翼来构建和标记图像

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服务所需的Dockerfiles,我得到的输出不是注册表中标记的最新版本号,或者不是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 命令是否使新版本按预期显示在屏幕上。

and then run the docker-compose up command do I get the new version to show up on screen as expected.

这是docker-compose的预期行为吗?即我是否应该总是再次执行 docker-compose rm -f 甚至在生产服务器上再次运行 up ?还是我在对这里的谷物做些什么,这就是为什么它不起作用的原因?

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以使用存储库中的最新映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆