docker psList containers and their state
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.
docker ps [OPTIONS] (same as docker container ls)You've logged in to a docker host and want to know what's running, since when, and on which ports.
docker psList containers. Short for docker container ls; by default only running ones.
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.
-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 charactersRunning docker ps after a crash and concluding the container is goneWithout -a, stopped containers aren't listed. The exited container, its exit code and its logs are still there.
docker ps -a --filter name=apiParsing 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 itAdding everyone to the docker group so they don't need sudoAccess 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 developersTry these on a Linux machine of your own. Each task has a hint and a solution.
List only the names of all containers (any state) created from the image shop-api:1.4.0.
Write a one-liner that removes every container that exited with code 0.
A teammate's docker ps shows: 'Exited (137) 3 minutes ago'. Write the two commands you would run next and what you're checking.
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.
docker psList containers and their state
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.
docker ps [OPTIONS] (same as docker container ls)You've logged in to a docker host and want to know what's running, since when, and on which ports.
docker psList containers. Short for docker container ls; by default only running ones.
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.
-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 charactersRunning docker ps after a crash and concluding the container is goneWithout -a, stopped containers aren't listed. The exited container, its exit code and its logs are still there.
docker ps -a --filter name=apiParsing 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 itAdding everyone to the docker group so they don't need sudoAccess 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 developersTry these on a Linux machine of your own. Each task has a hint and a solution.
List only the names of all containers (any state) created from the image shop-api:1.4.0.
Write a one-liner that removes every container that exited with code 0.
A teammate's docker ps shows: 'Exited (137) 3 minutes ago'. Write the two commands you would run next and what you're checking.
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.