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