Jen*_*gar 48 build-process docker docker-compose
我不知道我做错了什么,但我根本无法docker-compose up使用我们注册表中的最新图像,而无需先完全从系统中删除旧容器.看起来compose正在使用之前启动的图像,即使docker-compose pull已经获取了一个较新的图像.
我看了如何让docker-compose始终从新图像重新创建容器?这似乎与我的问题类似,但是所提供的解决方案都没有对我有用,因为我正在寻找可以在生产服务器上使用的解决方案,我不想在启动它们之前删除所有容器再次(可能的数据丢失?).我想仅仅为了检测已更改图像的新版本,拉出它们然后用这些新图像重新启动服务.
我为此创建了一个简单的测试项目,其中唯一的目标是在每个新构建上增加一个版本nr.如果我浏览到创建的nginx服务器(这在本地按预期工作),则显示版本nr.
docker版本:1.11.2 docker-compose版本:1.7.1 OS:使用docker-toolbox在CentOS 7和OS X 10.10上测试
我的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
Run Code Online (Sandbox Code Playgroud)
在我们的jenkins服务器上,我运行以下命令来构建和标记图像
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
Run Code Online (Sandbox Code Playgroud)
这似乎正在做它应该做的事情,因为每次构建完成并且版本nr被碰撞时我在我们的存储库中得到一个新的版本标记.
如果我现在跑
docker-compose pull && docker-compose -f docker-compose.yml up -d
Run Code Online (Sandbox Code Playgroud)
在我的计算机上的文件夹中,其中的内容只是docker-compose.yml和构建nginx和php服务的必要Dockerfiles,我得到的输出不是已在注册表中标记的最新版本号或显示在docker-compose.yml(0.1.8)中,但之前的版本是0.1.7.但是,pull命令的输出表明已获取新版本的图像:
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
Run Code Online (Sandbox Code Playgroud)
只有我跑
docker-compose stop && docker-compose rm -f
Run Code Online (Sandbox Code Playgroud)
然后运行docker-compose up命令我是否按预期在屏幕上显示新版本.
这是docker-compose的预期行为吗?也就是说我应该docker-compose rm -f在up再次运行之前做一次,即使在生产服务器上?或者我在这里做点什么,这就是为什么它不起作用?
我们的目标是让我们的构建过程构建并创建docker-compose.yml中所需图像的标记版本,将它们推送到我们的私有注册表,然后将"发布到生产步骤"简单地复制docker-compose. yml到生产服务器并运行一个docker-compose pull && docker-compose -f docker-compose.yml up -d新映像以在生产中启动.如果有人有这方面的提示或可以指向这种设置的最佳实践教程,也将非常感激.
ste*_*uer 36
为了确保您:latest从注册表(例如docker hub)使用最新版本的标签,您还需要再次提取最新标签.如果它改变了,差异将被下载并docker-compose up再次启动.
所以这将是要走的路:
docker-compose stop
docker-compose rm -f
docker-compose pull
docker-compose up -d
Run Code Online (Sandbox Code Playgroud)
我将其粘贴到一张图片中,我开始使用这个图片来确保图片保持最新状态:https://hub.docker.com/r/stephanlindauer/docker-compose-updater/
Abh*_*oda 24
要获取最新图像,请使用docker-compose build --pull
我使用下面的命令,这真的是3合1
"docker-compose down && docker-compose build --pull && docker-compose up -d"
Run Code Online (Sandbox Code Playgroud)
此命令将停止服务,提取最新映像,然后启动服务.
Jen*_*gar 19
要结束这个问题,似乎有效的方法确实在运行
docker-compose stop
docker-compose rm -f
docker-compose -f docker-compose.yml up -d
Run Code Online (Sandbox Code Playgroud)
即在up再次运行之前移除容器.
这样做时需要记住的是,如果你刚刚运行,数据量容器也会被删除rm -f.为了防止我明确指定要删除的每个容器:
docker-compose rm -f application nginx php
Run Code Online (Sandbox Code Playgroud)
正如我在问题中所说,我不知道这是否是正确的过程.但这似乎适用于我们的用例,所以在我们找到更好的解决方案之前,我们将推出这个解决方案.
小智 19
自 2020 年 5 月 7 日起,docker-compose 规范还为服务定义了“pull_policy”属性:
version: '3.7'
services:
my-service:
image: someimage/somewhere
pull_policy: always
Run Code Online (Sandbox Code Playgroud)
docker-compose规范说:
pull_policy defines the decisions Compose implementations will make when it starts to pull images.
Run Code Online (Sandbox Code Playgroud)
可能的值是(tl;dr,查看规范以获取更多详细信息):
小智 13
即使我的容器正在运行并且它更新得很好,我也会使用以下内容。
docker-compose pull
docker-compose up -d
Run Code Online (Sandbox Code Playgroud)
小智 13
拉取新图像:docker-compose pull
使用新镜像重建 docker 容器:docker-compose up --force-recreate --build -d
删除未使用的图像:docker image prune -f
有点晚了,但至少有:
Docker 版本 20.10.17
Docker Compose 版本 2.9.0
您可以使用docker-compose up --pull=always -d
来自 CLI 帮助:
--pull string Pull image before running ("always"|"missing"|"never") (default "missing")
选项down解决此问题
我运行我的撰写文件:
docker-compose -f docker/docker-compose.yml up -d
然后我删除所有down --rmi all
docker-compose -f docker/docker-compose.yml down --rmi all
Stops containers and removes containers, networks, volumes, and images
created by `up`.
By default, the only things removed are:
- Containers for services defined in the Compose file
- Networks defined in the `networks` section of the Compose file
- The default network, if one is used
Networks and volumes defined as `external` are never removed.
Usage: down [options]
Options:
--rmi type Remove images. Type must be one of:
'all': Remove all images used by any service.
'local': Remove only images that don't have a custom tag
set by the `image` field.
-v, --volumes Remove named volumes declared in the `volumes` section
of the Compose file and anonymous volumes
attached to containers.
--remove-orphans Remove containers for services not defined in the
Compose file
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46046 次 |
| 最近记录: |