skip to Main Content

I have several docker-compose.yml files, running ok with docker-compose up, individually.

Each docker-compose runs several containers.

After they are up, I can’t see what containers are up with docker ps.
I can see something with docker-compose ps, but only for a specific docker-compose.yml.

I want access to global polling of the containers state.

How can I list all running containers, no matter their origin?

2

Answers


  1. Here is kind of a guess based on the clues of your question.

    When you do run containers via docker-compose up, please do mind that hitting CTRL+C will actually stop your containers.

    Here is an example of doing this:

    $ docker-compose up
    [+] Running 1/0
     ⠿ Container docker-test-1  Created                                  0.0s
    Attaching to docker-test-1
    ^CGracefully stopping... (press Ctrl+C again to force)
    [+] Running 1/1
     ⠿ Container docker-test-1  Stopped                                 10.2s
    canceled
    

    The important part here being: ^CGracefully stopping...

    If you want to keep them running, you will have to run docker-compose up in a detached mode, with the help of the -d option.

    -d, --detach       Detached mode: Run containers in the background,
                       print new container names. Incompatible with
                       --abort-on-container-exit.
    

    Source: https://docs.docker.com/compose/reference/up/


    So doing,

    docker-compose up -d && docker ps
    

    Should actually give you what you expected.
    E.g.:

    $ docker-compose up -d && docker ps
    [+] Running 2/2
     ⠿ Network docker_default   Created                                                                                   0.1s
     ⠿ Container docker-test-1  Started                                                                                   0.5s
    CONTAINER ID   IMAGE     COMMAND               CREATED        STATUS                  PORTS     NAMES
    ab9f422247c9   alpine    "tail -f /dev/null"   1 second ago   Up Less than a second             docker-test-1
    

    And if you want to see all container, regardless of their state, you can use the -a option of docker ps.

    --all , -a        Show all containers (default shows just running)
    

    Source: https://docs.docker.com/engine/reference/commandline/ps/

    E.g.:

    $ docker-compose up  
    [+] Running 2/2
     ⠿ Network docker_default   Created                                                                                   0.1s
     ⠿ Container docker-test-1  Created                                                                                   0.1s
    Attaching to docker-test-1
    ^CGracefully stopping... (press Ctrl+C again to force)
    [+] Running 1/1
     ⠿ Container docker-test-1  Stopped                                                                                  10.2s
    canceled
    $ docker ps -a
    CONTAINER ID   IMAGE                 COMMAND               CREATED          STATUS                       PORTS     NAMES
    615bcc6f1e50   alpine                "tail -f /dev/null"   18 seconds ago   Exited (137) 3 seconds ago             docker-test-1
    
    Login or Signup to reply.
  2. Docker compose adds labels to each container that it creates. If you want to get all containers created by compose, you can perform a container ls and apply a filter.

    docker container ls --filter label=com.docker.compose.project
    

    This will show all running container created by compose, regardless of the project name.

    For example, I created some containers from different compose projects. With the filter, I get only those, but no other container that have not been created by compose and therefore don’t have a project label.

    $ base='{{.Status}}t{{.ID}}t{{.Names}}t{{.Image}}t{{.Ports}}t{{.Networks}}t{{.Mounts}}'
    $ compose='{{.Label "com.docker.compose.project"}}t{{.Label "com.docker.compose.service"}}'
    
    $ docker container ls --all 
      --filter label=com.docker.compose.project 
      --format "table $composet$base"
    
    project        service     STATUS                      CONTAINER ID   NAMES                IMAGE                   PORTS                                                                     NETWORKS               MOUNTS
    kafka          kafka       Up 5 minutes                3f97a460266e   kafka_kafka_1        bitnami/kafka:3         0.0.0.0:9092->9092/tcp, :::9092->9092/tcp                                 kafka_default          kafka_kafka_da…,kafka_kafa_con…
    kafka          zookeeper   Up 5 minutes                0b6f32ccd196   kafka_zookeeper_1    bitnami/zookeeper:3.7   2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, :::2181->2181/tcp, 8080/tcp   kafka_default          kafka_zookeepe…
    manager        db          Up 22 minutes               4f0e799b4fd7   manager_db_1         da2cb49d7a8d            5432/tcp                                                                  manager_default        0d667a0e48a280…
    foo            db          Exited (0) 37 minutes ago   e106c5cdbf5e   foo_db_1             da2cb49d7a8d                                                                                      foo_default            5a87e93627b8f6…
    foo            backend     Up 10 minutes               08a0873c0587   foo_backend_2        c316d5a335a5            80/tcp                                                                    foo_default            
    foo            frontend    Up 10 minutes               be723bf41aeb   foo_frontend_1       c316d5a335a5            80/tcp                                                                    foo_default            
    foo            backend     Up 10 minutes               5d91d4bcfcb3   foo_backend_1        c316d5a335a5            80/tcp                                                                    foo_default            
    manager        app         Up 22 minutes               2ca4c0920807   manager_app_1        c316d5a335a5            80/tcp                                                                    manager_default        
    manager        app         Up 22 minutes               b2fa2b9724b0   manager_app_2        c316d5a335a5            80/tcp                                                                    manager_default        
    loadbalancer   app         Exited (0) 37 minutes ago   791f4059b4af   loadbalancer_app_1   c316d5a335a5                                                                                      loadbalancer_default   
    

    If you want to see all container regardless of their state, you can add the --all or short -a flag to the ls command, like I did in my example. Otherwise, only running containers are shown.

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