skip to Main Content

How can I list docker containers by application? For example, assume that I have app1 (that has c1, c2, c3) and app2 (that has c4, c5) deployed. I can see c1, c2 and c3 are under app1 and c4 and c5 are under app2 using docker dashboard. But, if I do "docker ps", it returns a flat list of containers, I have no way to know which containers belong to which app? Below is an example.

enter image description here

As can be seen in docker dashboard on the left side, there are two applications, nginx-gohttp-master and demo, deployed in the docker environment. The first app has two containers and the second app has four containers, and the two apps are independent of each other. However, I only see a flat list of containers on the right side as listed by "docker ps". I would like to know how many apps are deployed and which containers belong to which app. How can I do that using a docker command line tool?

Thanks

2

Answers


  1. By default, docker-compose adds some labels including com.docker.compose.project which contains the name of the compose app/project. You can get that output using docker ps with the --format option like so:

    $ docker ps --format 'table {{.Label "com.docker.compose.project"}} {{.ID}} {{.Names}} {{.Command}} {{.Status}}'
    project CONTAINER ID NAMES COMMAND STATUS
    app1 1fd93328e69b app1_nginx_1 "/docker-entrypoint.…" Up 3 minutes
    app2 9fe81391b961 app2_nginx_1 "/docker-entrypoint.…" Up 2 minutes
    app3 d28fca61a184 app3_nginx_1 "/docker-entrypoint.…" Up 1 minutes
    

    You can see some of the other labels that docker-compose applies here.

    Login or Signup to reply.
  2. Check out filters

    1. Filter running containers
    docker ps --filter "name=nginx-gohttp-master"
    
    1. Filter all containers (running + stopped)
    docker ps -a --filter "name=nginx-gohttp-master"
    

    Apparently there’s no need for a wildcard character *, because = is not a direct exact-match string equality but a sub-string search.

    For an exact match (which is not what you want in your question) you need to pipe the result into something else as discussed here.

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