简体   繁体   中英

How to update existing images with docker-compose?

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

Additional info: i am using gitlab-ci

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

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

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

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

输出

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.

我已经注意到上面的答案,但我仍然坚持使用以下命令来确保一切正确:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM