skip to Main Content

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:

enter image description here

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:

enter image description here

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


  1. 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:

    If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, docker compose up picks up the changes by stopping and recreating the containers (preserving mounted volumes). To prevent Compose from picking up changes, use the –no-recreate flag

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search