'Update only one Container via docker-compose
Let's say I have a docker-compose.yml file containg five apps & I started them via docker-compose up -d
Now there is an update for one of these apps available which I want to use. So I pull this newer image and... how can I stop this one old container without stopping the four other container?
- Using
docker-compose down && docker-compose up -d
would stop and restart all of my container, so that's wrong. - Using
docker stop app_1 && docker run app_1
would work but seems "wrong" to me because it's nodocker-compose
command.
So what's the "docker-way" to do this?
Solution 1:[1]
Just run docker-compose up -d
again (without down
/ stop
/ kill
before).
Initial docker-compose.yml
:
version: "2"
services:
db1:
command: mongod
image: mongo:3.2.4
ports:
- "27020:27017"
db2:
command: mongod
image: mongo:3.2.4
ports:
- "27021:27017"
Update db2
:
version: "2"
services:
db1:
command: mongod
image: mongo:3.2.4
ports:
- "27020:27017"
db2:
command: mongod
image: mongo:3.2.6
ports:
- "27021:27017"
Run docker-compose up -d
again:
Pulling db2 (mongo:3.2.6)...
3.2.6: Pulling from library/mongo
47994b92ab73: Pull complete
a3ed95caeb02: Pull complete
71b6becd9768: Pull complete
7d5d40f9dc7b: Pull complete
9dc152e647de: Pull complete
3f1f69340f17: Pull complete
82a29b50f1d2: Pull complete
97869c61a050: Pull complete
50aa2bf3bccc: Pull complete
03913f2c5b05: Pull complete
Digest: sha256:29ee114c0ce96494553cd72f18d92935b36778b77bce167fc9962e442d8c7647
Status: Downloaded newer image for mongo:3.2.6
composetest_db1_1 is up-to-date
Recreating composetest_db2_1
The last two lines of the output show the expected behavior.
Solution 2:[2]
There's one use-case where the accepted answer doesn't work:
Say you have 4 containers, with 2 having a new version, and you only want to update/restart ONE of them.
If you simply do docker-compose up -d
again, this will update both containers.
If you want to target only one specific container, you can do it in two steps:
- Pull all updates:
docker-compose pull
(this will just download the images, not do anything else) - And then only update the container you want:
docker-compose up -d --no-deps name_of_your_container
--no-deps
is important to avoid restarting dependencies of your updated containers by mistake.
Solution 3:[3]
quite old question but the answer does not seem to work if the image has changed on docker hub and we don't change the docker file e.g. if I'm using the latest version of mongo (mongo:latest) in the example above and the "latest" tag is moved in Docker Hub from 3.2.4 to 3.2.6, it is not pulled by only running docker-compose up -d
as far as I can see...
Solution 4:[4]
To restart only one container you can simply do:
docker-compose up -d --build image-name
The output will look like this:
Recreating image-name ... done
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Antoine Jaussoin |
Solution 3 | Olivier |
Solution 4 | Emmanuel |