Skip to content

Commands: Docker & Podman

Docker and Podman commands: containers, images, pods, logs, cleanup.

Index
  • #docker
  • #podman
Updated

Also:containers · docker compose · podman

26 items

Docker20

Docker: Follow logs of a container

Safe

Stream a container's stdout/stderr starting from the last 200 lines.

docker logs -f --tail 200 --timestamps <container>
What each part does
  1. docker logsshow a container's stdout/stderr
  2. -ffollow: keep streaming new lines
  3. --tail 200start with only the last 200 lines
  4. --timestampsprefix each line with its timestamp
  5. <container>container name or ID

Also known as: docker logs -f · container logs

Docker: Show container logs for a time window

Safe

Logs from the last N minutes/hours, e.g. --since 15m or --since 2h.

docker logs --since <duration> <container>
What each part does
  1. docker logsshow a container's output
  2. --since <duration>only lines newer than this: relative (30m, 2h) or a timestamp; pair with --until
  3. <container>container name or ID
Example
$ docker logs --since 15m api

Also known as: logs since

Docker: Follow logs of all compose services

Safe

Interleaved logs from every service in the compose project. Add a service name to narrow it.

docker compose logs -f --tail 100
What each part does
  1. docker compose logslogs of every service in the compose project, prefixed by service name
  2. -ffollow new lines
  3. --tail 100last 100 lines per service to start with

Also known as: docker-compose logs · compose logs

Docker: List containers with status and ports

Safe

All containers (including exited) in a compact table.

docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
What each part does
  1. docker pslist containers
  2. -ainclude stopped ones
  3. --format "table ..."Go template; table adds a header row and aligns columns
  4. {{.Names}}\t{{.Status}}\t{{.Ports}}the columns: name, status (Up / Exited (code)), port mappings, tab separated

Also known as: docker ps · exited containers

Docker: Open a shell inside a container

Safe

Interactive shell in a running container. Use bash if the image has it.

docker exec -it <container> sh
What each part does
  1. docker execrun a command in a running container
  2. -itinteractive + allocate a terminal
  3. <container>container name or ID
  4. shthe shell to start; use bash if the image has it

Also known as: docker exec · shell into container

Docker: Check exit code and OOM kill

Safe

Why did it stop? 137 + OOMKilled=true means the memory limit was hit.

docker inspect <container> --format '{{.State.ExitCode}} OOMKilled={{.State.OOMKilled}}'
What each part does
  1. docker inspect <container>full JSON details of a container (works on stopped ones too)
  2. --format '...'Go template: print only the fields you ask for
  3. {{.State.ExitCode}}the exit code of the last run (137 = SIGKILL, 143 = SIGTERM)
  4. {{.State.OOMKilled}}true if the kernel killed it for exceeding its memory limit
Example
137 OOMKilled=true

Also known as: exit code 137 · OOMKilled · container keeps restarting

Docker: Show live container resource usage

Safe

One-shot CPU, memory, network and block I/O per container.

docker stats --no-stream
What each part does
  1. docker statsCPU, memory, network and disk I/O per running container
  2. --no-streamprint one snapshot and exit instead of refreshing

Also known as: docker stats · container memory

Docker: Copy a file out of a container

Safe

Grab a config, dump or log file from a container to the current directory.

docker cp <container>:<path> ./
What each part does
  1. docker cpcopy files between a container and the host (works on stopped containers)
  2. <container>:<path>source: a path inside the container
  3. ./destination: the current directory on the host

Also known as: docker cp

Docker: Show disk used by Docker

Safe

Space used by images, containers, local volumes and build cache, and how much is reclaimable.

docker system df
What each part does
  1. docker system dfdisk used by images, containers, volumes and build cache, with how much is reclaimable; add -v for per-item detail

Also known as: docker disk usage · /var/lib/docker full

Docker: Remove dangling images

Caution

Delete untagged <none> images left over from rebuilds.

docker image prune -f
What each part does
  1. docker image pruneremove dangling images (<none>:<none>, left over from rebuilds)
  2. -fdon't ask for confirmation

Also known as: dangling images · <none> images

Docker: Prune everything unused including volumes

Destructive

Removes stopped containers, unused networks, all unused images, build cache AND unused volumes.

docker system prune -a --volumes -f
What each part does
  1. docker system pruneremove stopped containers, unused networks, dangling images and build cache
  2. -aalso remove every image not used by a container, not just dangling ones
  3. --volumesalso remove volumes not used by a container (data loss)
  4. -fdon't ask for confirmation

My note --volumes deletes data in unused named volumes. Drop it on hosts with local DBs.

Also known as: docker prune · no space left on device · free docker space

Docker: Build and tag an image

Safe

Build from the Dockerfile in the current directory and tag for a registry.

docker build -t <registry>/<image>:<tag> .
What each part does
  1. docker buildbuild an image from a Dockerfile
  2. -t <registry>/<image>:<tag>name and tag the result, ready to docker push
  3. .build context: the current directory (sent to the daemon; .dockerignore applies)

Also known as: docker build

Docker: Log in to a registry without leaking the password

Safe

Pipe the token in so it never shows up in shell history or ps.

echo "$REGISTRY_TOKEN" | docker login <registry> -u <username> --password-stdin
What each part does
  1. echo "$REGISTRY_TOKEN"print the token from an env var (not typed on the command line)
  2. |pipe it into the next command's stdin
  3. docker login <registry>authenticate to the registry
  4. -u <username>the user name
  5. --password-stdinread the password from stdin, so it stays out of shell history and ps

Also known as: docker login · unauthorized: authentication required

Docker: Redeploy a compose service with a new image

Safe

Pull the latest tag and recreate only that service.

docker compose pull <service> && docker compose up -d <service>
What each part does
  1. docker compose pull <service>download the newest image for the service's tag
  2. &&only continue if the pull worked
  3. docker compose up -d <service>recreate the service if its image or config changed, detached

Also known as: compose update · restart service

Docker: Check the base OS of an image

Safe

Tells you which package manager to patch with in the Dockerfile (apt, apk, dnf, microdnf).

docker run --rm --entrypoint cat <image>:<tag> /etc/os-release
What each part does
  1. docker run --rmthrowaway container
  2. --entrypoint catskip the image entrypoint and just run cat
  3. /etc/os-releasedistro name and version

Also known as: image os · base image distro · which distro image

Docker: Patch OS packages in a Debian or Ubuntu Dockerfile

Safe

Dockerfile line: pulls in all security fixes for the base image at build time. Put it right after FROM.

RUN apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/*
What each part does
  1. apt-get updaterefresh package lists
  2. apt-get upgrade -yupgrade every installed package
  3. rm -rf /var/lib/apt/lists/*keep the image small

My note Only build fixes when the build is not cached: docker build --pull --no-cache

  • #docker
  • #patching
  • #build
  • #vulnerabilities

Also known as: dockerfile apt upgrade · patch container image · fix image cve

Docker: Patch OS packages in an Alpine Dockerfile

Safe

Dockerfile line: upgrade every Alpine package to the latest fixed version at build time.

RUN apk upgrade --no-cache
What each part does
  1. apk upgradeupgrade all installed packages
  2. --no-cachedo not keep the package index in the image
  • #docker
  • #patching
  • #build
  • #vulnerabilities

Also known as: dockerfile apk upgrade · alpine cve · patch alpine image

Docker: Rebuild an image with the latest base image

Safe

Most image CVEs are fixed by rebuilding: a fresh base image plus re-run package installs.

docker build --pull --no-cache -t <image>:<tag> .
What each part does
  1. --pullalways download the newest version of the FROM image
  2. --no-cachere-run every step so apt/apk get today's fixes
  3. -t <image>:<tag>name the result

My note Scan before pushing: trivy image --severity HIGH,CRITICAL --ignore-unfixed <image>:<tag>

  • #docker
  • #patching
  • #build
  • #vulnerabilities

Also known as: rebuild image · refresh base image · fix image vulnerabilities

Docker: Print environment variables of a container

Safe

Every env var the running container sees. Stopped container: docker inspect <container> --format '{{range .Config.Env}}{{println .}}{{end}}'.

docker exec <container> env
What each part does
  1. docker exec <container>run a command inside the running container (name or ID)
  2. envprint every environment variable, KEY=value per line; use printenv <VAR> for just one
Example
$ docker exec api env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=4f2a9c1e7b3d
NODE_ENV=production
DATABASE_HOST=db

My note Values passed with -e or --env-file (including secrets) are readable by anyone with docker access. Distroless images have no env binary: use the inspect form.

Also known as: docker print env · docker env vars · show container environment · printenv

Podman6

Podman: List all containers

Safe

Running and exited containers, rootless by default for the current user.

podman ps -a
What each part does
  1. podman pslist containers
  2. -ainclude stopped ones

Also known as: podman ps

Podman: Follow logs of a container

Safe

Stream a container's output starting from the last 200 lines.

podman logs -f --tail 200 <container>
What each part does
  1. podman logsshow a container's output
  2. -ffollow new lines
  3. --tail 200start with the last 200 lines
  4. <container>container name or ID

Also known as: podman logs

Podman: Create a pod with a published port

Safe

A pod shares one network namespace between containers, like a Kubernetes pod. Add containers with --pod <pod>.

podman pod create --name <pod> -p <host-port>:<container-port>
What each part does
  1. podman pod createcreate a pod: containers in it share network and localhost
  2. --name <pod>the pod's name
  3. -p <host-port>:<container-port>publish a port; must be set on the pod, not on its containers

Also known as: podman pod

Podman: Generate a systemd unit for a container

Safe

Writes container-<name>.service so systemd can manage the container.

podman generate systemd --new --files --name <container>
What each part does
  1. podman generate systemdwrite a systemd unit for a container (newer Podman prefers Quadlet files)
  2. --newthe unit creates a fresh container on start and removes it on stop
  3. --fileswrite .service files to the current directory instead of stdout
  4. --name <container>use the container name (not ID) in the unit

My note Deprecated in favour of Quadlet (.container files in ~/.config/containers/systemd/) on Podman 4.4+.

Also known as: podman systemd · quadlet

Podman: Log in to a registry

Safe

Store registry credentials for pulls and pushes (auth.json).

podman login <registry> -u <username>
What each part does
  1. podman login <registry>authenticate to a registry, e.g. quay.io
  2. -u <username>the user name; prompts for the password (or use --password-stdin)

Also known as: podman login · unauthorized

Podman: Prune everything unused

Destructive

Remove stopped containers, unused pods, images and build cache.

podman system prune -a -f
What each part does
  1. podman system pruneremove stopped containers, unused pods, networks and dangling images
  2. -aalso remove all images not used by a container
  3. -fdon't ask for confirmation

My note Add --volumes to delete unused volumes too.

Also known as: podman prune · no space left on device

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…