I am running two docker container using docker compose.
Here is my docker-compose.yml
file
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- "4001:8081"
And after running the command docker-compose up
, docker is creating two container redis-server and node-app as we can see in the image below:
I added restart policy for node-app service in docker-compose.yml
as shown below:
version: '3'
services:
redis-server:
image: 'redis'
node-app:
restart: on-failure
build: .
ports:
- "4001:8081"
And when I run the docker-compose up
again, the containers are created again. Here is the output docker ps --all
:
I see that container id for node-app is different between two executions which is due to the fact that docker is creating a new container. But my question is why can’t I see the old container for node-app that was created during the first execution (container id: 96b19ffca388) when I type docker ps --all
command?
2
Answers
Docker-Compose is smart and will automatiaclly stop your old containers and re-create them with your updates when you run
Docker-Compose up
. From the official documentation page:Because they are no longer there. When
docker-compose
restarted it cleaned up after itself. This can be verified quite easily. If you run in the machine that runs the compose:ps -ef | grep <container id>
for a one of the newer containers you will see it is there, whereas running the same command for one of the old ones will find nothing. Those containers aren’t paused, they’re deleted.