CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Docker / docker ps

docker ps

List containers and their state

Beginnercontainersessentialtroubleshooting

docker ps lists running containers: ID, image, command, age, status (with health and exit codes), published ports and names. Add -a to include stopped ones, which is where crashed containers hide. Filters and --format make it the starting point of most docker scripts.

Syntaxdocker ps [OPTIONS] (same as docker container ls)

Watch it run

You've logged in to a docker host and want to know what's running, since when, and on which ports.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
docker ps

The docker CLI talks to dockerd through /var/run/docker.sock, which is owned by root:docker with mode 660. Your user isn't in the docker group yet.

Anatomy of a command

$
commanddocker ps

List containers. Short for docker container ls; by default only running ones.

How it works

docker ps sends GET /containers/json to dockerd (adding all=1 for -a and a JSON filters parameter). dockerd answers from its in-memory view of containers, which is persisted in /var/lib/docker/containers/<id>/config.v2.json, so listing never touches the containers themselves and is cheap. STATUS is computed from the state and timestamps: Up 2 hours, Exited (137) 5 minutes ago, Restarting (1), and (healthy) or (unhealthy) from the last HEALTHCHECK results. PORTS comes from the network settings. The one expensive option is -s: dockerd has to walk each container's writable overlay layer to add up its size.

Options you'll actually use

-a, --allShow all containers, not just running ones
-q, --quietPrint only container IDs (for $(...) and xargs)
-f, --filter KEY=VALstatus=, name=, label=, ancestor=, health=, publish=, network=, exited=
--format TEMPLATEPick columns: 'table {{.Names}}\t{{.Ports}}', or json for one JSON object per line
-s, --sizeAdd writable-layer size (and the image size as 'virtual'); slower
-l, --latestOnly the most recently created container
-n NThe N most recently created containers, any state
--no-truncFull IDs and full commands instead of 12 and 20 characters

Common mistakes

Running docker ps after a crash and concluding the container is gone

Without -a, stopped containers aren't listed. The exited container, its exit code and its logs are still there.

docker ps -a --filter name=api
Parsing docker ps output with awk '{print $7}'

Columns contain spaces ("Up 2 hours", the command) and widths change, so field numbers shift.

docker ps --format '{{.Names}}\t{{.Status}}'
Reading 5432/tcp in PORTS as 'Postgres is reachable on the host'

Without an arrow the port is only exposed (EXPOSE in the image), not published. Only containers on the same network can reach it.

Publish it with -p 127.0.0.1:5432:5432 if the host really needs it
Adding everyone to the docker group so they don't need sudo

Access to the docker socket is root on the host: docker run -v /:/host gives full control.

Limit the docker group to admins, or use rootless Docker for developers

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List only the names of all containers (any state) created from the image shop-api:1.4.0.

  2. 2

    Write a one-liner that removes every container that exited with code 0.

  3. 3

    A teammate's docker ps shows: 'Exited (137) 3 minutes ago'. Write the two commands you would run next and what you're checking.

  4. 4

    In the sandbox, practise the same columns-not-awk idea on a real file: print the 1st and 3rd field of /etc/passwd separated by a tab.

Related commands

docker runCreate and start a container from an imagedocker logsRead a container's stdout and stderrdocker inspectShow the full configuration and state of Docker objectsdocker rmRemove containersdocker statsLive CPU, memory, network and I/O per container
Previousdocker buildNextdocker images
Linux / Docker / docker ps

docker ps

List containers and their state

Beginnercontainersessentialtroubleshooting

docker ps lists running containers: ID, image, command, age, status (with health and exit codes), published ports and names. Add -a to include stopped ones, which is where crashed containers hide. Filters and --format make it the starting point of most docker scripts.

Syntaxdocker ps [OPTIONS] (same as docker container ls)

Watch it run

You've logged in to a docker host and want to know what's running, since when, and on which ports.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
docker ps

The docker CLI talks to dockerd through /var/run/docker.sock, which is owned by root:docker with mode 660. Your user isn't in the docker group yet.

Anatomy of a command

$
commanddocker ps

List containers. Short for docker container ls; by default only running ones.

How it works

docker ps sends GET /containers/json to dockerd (adding all=1 for -a and a JSON filters parameter). dockerd answers from its in-memory view of containers, which is persisted in /var/lib/docker/containers/<id>/config.v2.json, so listing never touches the containers themselves and is cheap. STATUS is computed from the state and timestamps: Up 2 hours, Exited (137) 5 minutes ago, Restarting (1), and (healthy) or (unhealthy) from the last HEALTHCHECK results. PORTS comes from the network settings. The one expensive option is -s: dockerd has to walk each container's writable overlay layer to add up its size.

Options you'll actually use

-a, --allShow all containers, not just running ones
-q, --quietPrint only container IDs (for $(...) and xargs)
-f, --filter KEY=VALstatus=, name=, label=, ancestor=, health=, publish=, network=, exited=
--format TEMPLATEPick columns: 'table {{.Names}}\t{{.Ports}}', or json for one JSON object per line
-s, --sizeAdd writable-layer size (and the image size as 'virtual'); slower
-l, --latestOnly the most recently created container
-n NThe N most recently created containers, any state
--no-truncFull IDs and full commands instead of 12 and 20 characters

Common mistakes

Running docker ps after a crash and concluding the container is gone

Without -a, stopped containers aren't listed. The exited container, its exit code and its logs are still there.

docker ps -a --filter name=api
Parsing docker ps output with awk '{print $7}'

Columns contain spaces ("Up 2 hours", the command) and widths change, so field numbers shift.

docker ps --format '{{.Names}}\t{{.Status}}'
Reading 5432/tcp in PORTS as 'Postgres is reachable on the host'

Without an arrow the port is only exposed (EXPOSE in the image), not published. Only containers on the same network can reach it.

Publish it with -p 127.0.0.1:5432:5432 if the host really needs it
Adding everyone to the docker group so they don't need sudo

Access to the docker socket is root on the host: docker run -v /:/host gives full control.

Limit the docker group to admins, or use rootless Docker for developers

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List only the names of all containers (any state) created from the image shop-api:1.4.0.

  2. 2

    Write a one-liner that removes every container that exited with code 0.

  3. 3

    A teammate's docker ps shows: 'Exited (137) 3 minutes ago'. Write the two commands you would run next and what you're checking.

  4. 4

    In the sandbox, practise the same columns-not-awk idea on a real file: print the 1st and 3rd field of /etc/passwd separated by a tab.

Related commands

docker runCreate and start a container from an imagedocker logsRead a container's stdout and stderrdocker inspectShow the full configuration and state of Docker objectsdocker rmRemove containersdocker statsLive CPU, memory, network and I/O per container
Previousdocker buildNextdocker images