I have multiple microservices and I am using docker-compose for development deployments. When there are some changes in the microservices code base, I am triggering ci job to re-deploy them. I have below script to do this. But each time I have to build all images from scratch, then run them. After all of this operation, I have anonymous images. So I am using the last script to remove them. What you would suggest making this process more practical? Is there any way to update an existing image without removing it with new changes?

- docker-compose build
- docker-compose down
- docker-compose up -d --force-recreate
- docker rmi $(docker images -f "dangling=true" -q) -f

附加信息:我正在使用gitlab-ci


当前回答

docker-compose pull

then

docker-compose up -d

你不需要“down”“docker-compose up -d”命令只会重新创建修改后的docker

其他回答

你可以使用:

docker-compose pull

现在您的映像已经更新。如果你有以前版本的容器运行,你应该重新启动它来使用更新后的镜像:

docker-compose up --detach

Up命令自动在映像或配置更改上重新创建容器。

我更倾向于确保在用新图像更新容器之前下载所有图像,以最大限度地减少处于中间状态的时间,或者在图像下载失败的情况下处于中间状态的时间。

我调出了最新的图片: docker-compose拉 然后重新启动容器: Docker-compose up -d——remove-orphans 可选地,我删除过时的图像: Docker映像修剪

我已经注意到了上面的答案,但是我仍然坚持使用下面的顺序来确保一切正确:

docker-compose拉 docker-compose下来 Docker-compose up -d

Docker容器的设计是短暂的。要更新现有容器,需要删除旧容器并启动一个新容器。 因此,您所遵循的过程是正确的。

您可以将命令简化为以下命令:

docker-compose up --force-recreate --build -d
docker image prune -f

还有一个脚本,使用它可以一次更新许多docker-compose堆栈。

它被称为合成更新,可以在以下链接中找到:

https://github.com/FrederikRogalski/compose-update

合成-更新docker-合成-图像-更新器

这个python脚本自动更新一个或多个docker-compose堆栈的映像。

如果提供了多个docker-compose目录,脚本将并行更新它们。

Demo

使用

Usage: compose-update [OPTIONS] [UPDATE_DIRS]...

  Update docker-compose images automatically.

  Takes one or more directorys as input and searches for a
  compose file in one of the following forms:
  "compose.yaml", "compose.yml", "docker-compose.yaml",
  "docker-compose.yml"

Options:
  --prune / --no-prune  Prune docker images after update
                        process if set
  --help                Show this message and exit.

安装

git clone https://github.com/FrederikRogalski/compose-update.git
cd compose-updater
chmod +x update-compose

然后将文件“update-compose”添加到您的路径。