如何使用docker-compose更新现有图像?

我有多个微服务,我正在使用Docker-Compose进行开发部署。当微服务代码库发生一些变化时,我会触发CI作业来重新部署它们。我有下面的脚本这样做。但每次我都必须从头开始构建所有映像,然后运行它们。在所有这些操作之后,我有了anonymous图像。所以我使用最后一个脚本来删除它们。你有什么建议使这个过程更实际?有没有办法在不删除现有映像的情况下使用新的更改来更新现有映像?

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

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

259406 次浏览

Docker containers are designed to be ephemeral. To update an existing container, you remove the old one and start a new one. Thus the process that you are following is the correct one.

You can simplify the commands to the following ones:

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

With docker-compose version 3 you can add tags to your images and clean up by them depends on your logic:

build: ./dir
image: yourapp:tag

It could help you to avoid anonymous images to clean up

You can update it using:

docker-compose pull

Now your image is updated. If you have the previous version of container running you should restart it to use the updated image:

docker-compose up --detach

up command automatically recreates container on image or configuration change.

I prefer to ensure all the images are downloaded before updating the containers with the new images to minimize the time in an intermediate state or worse being in the middle in case the download of an image fails.

  1. I pull latest images:

    docker-compose pull

  2. Then I restart containers:

    docker-compose up -d --remove-orphans

  3. Optionally, I remove obsolete images:

    docker image prune

docker-compose pull

then

docker-compose up -d

you don't need "down" "docker-compose up -d" command will only recreate changed one

There is also a script, with which one can update many docker-compose stacks at once.

It is called compose-update and can be found at the following link:

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

compose-update a docker-compose-image-updater

This python script updates the images of one or many docker-compose stacks automatically.

If multiple docker-compose directories are supplied, the script updates them in parallel.

Demo

output

Usage

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.

Installation

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

Then add the file 'update-compose' to your path.

I've noticed above answers, but I still insist to use the following orders to make sure everything is correct:

  1. docker-compose pull
  2. docker-compose down
  3. docker-compose up -d