How to update existing images with docker-compose?

DockerDocker Compose

Docker Problem Overview


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 Solutions


Solution 1 - Docker

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

Solution 2 - Docker

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.

Solution 3 - Docker

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

Solution 4 - Docker

docker-compose pull

then

docker-compose up -d

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

Solution 5 - Docker

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

Solution 6 - Docker

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.

Solution 7 - Docker

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEfeView Question on Stackoverflow
Solution 1 - DockeryamenkView Answer on Stackoverflow
Solution 2 - DockerEvgen BodunovView Answer on Stackoverflow
Solution 3 - DockerChristophe LallementView Answer on Stackoverflow
Solution 4 - DockersonertbncView Answer on Stackoverflow
Solution 5 - DockerAlexGeraView Answer on Stackoverflow
Solution 6 - DockerFrederik RogalskiView Answer on Stackoverflow
Solution 7 - DockerZhang BuzzView Answer on Stackoverflow