Update only one Container via docker-compose
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.
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.
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