Skip to content
AkOps Labs
Playgrounds

Docker

Package any app into a container and run it anywhere.

15 levels · 84 lessons · about 3 hours

Docker commands cheat sheet

Every command in the Docker Playground, grouped by level. Practise each one above in the interactive terminal.

Show all 84 commands

Level 1: Run Your First Container (Beginner)

Pull an image, run it and see it working.

Check Docker works
docker version

docker version prints the client and server (engine) versions - a quick health check.

Download an image
docker pull nginx

An image is a ready-made template for a container. docker pull downloads one from Docker Hub.

List images
docker images

docker images shows the images stored on your machine.

Run a container
docker run -d -p 8080:80 nginx

docker run starts a container from an image. -d runs it in the background, -p HOST:CONTAINER opens a port.

See running containers
docker ps

docker ps lists running containers. Add -a to include stopped ones.

Level 2: Manage Containers (Beginner)

Name, stop, start, remove and debug containers.

Give it a name
docker run -d --name web -p 8080:80 nginx

--name sets a friendly name so you don't have to copy long container IDs.

Read the logs
docker logs -f web

docker logs shows what the app printed. -f follows new lines live.

Get a shell inside
docker exec -it web sh

docker exec runs a command in a running container. -it gives you an interactive terminal.

Stop a container
docker stop web

docker stop shuts a container down gracefully. docker start brings it back.

Remove a container
docker rm web

Stopped containers still take space. docker rm deletes them (add -f to force-remove a running one).

Level 3: Build Your Own Image (Intermediate)

Turn your app into an image with a Dockerfile.

Build from a Dockerfile
docker build -t myapp:1.0 .

A Dockerfile is the recipe for an image. docker build -t NAME:TAG . builds it from the current folder.

Run your image
docker run -d -p 3000:3000 myapp:1.0

Run your own image exactly like any other. Use -e to pass environment variables.

Tag for a registry
docker tag myapp:1.0 akops/myapp:1.0

To push to Docker Hub the image name must start with your username: USER/NAME:TAG.

Log in to a registry
docker login

docker login stores credentials for Docker Hub (or another registry) so you can push.

Push the image
docker push akops/myapp:1.0

docker push uploads your image so servers and CI pipelines can pull it.

Level 4: Multi-Container Apps (Intermediate)

Connect containers, keep data and run whole stacks with Compose.

Create a network
docker network create appnet

Containers on the same user-defined network can reach each other by name.

Persist data with a volume
docker volume create dbdata

Container files vanish when the container is removed. A volume keeps data safe outside it.

Start a stack with Compose
docker compose up -d

docker compose reads compose.yaml and starts every service together. -d runs in the background.

Check the stack
docker compose ps

docker compose ps lists only the containers that belong to this Compose project.

Tear it down
docker compose down

docker compose down stops and removes the stack's containers and network (volumes stay unless you add -v).

Clean up disk space
docker system prune

docker system prune removes stopped containers, unused networks and dangling images.

Level 5: Production-Ready Containers (Intermediate)

Inspect, limit, harden and scan containers before they reach production.

Inspect a container
docker inspect web

docker inspect prints every detail of a container as JSON: IP address, mounts, env vars, restart policy.

Live resource usage
docker stats

docker stats shows live CPU, memory and network usage for each running container.

Limit memory
docker run -d --memory=512m nginx

Without limits, one container can eat the whole server's memory. --memory sets a hard cap.

Auto-restart
docker update --restart unless-stopped web

A restart policy brings a container back after a crash or a server reboot. unless-stopped is the common choice.

See image layers
docker history myapp:1.0

Each Dockerfile instruction adds a layer. history shows them with sizes, so you can find what makes an image big.

Scan for vulnerabilities
docker scout cves myapp:1.0

Docker Scout checks the packages in an image against known security issues (CVEs).

Level 6: Smaller, Faster Images (Advanced)

Build small images with multi-stage Dockerfiles and keep the machine clean.

Build from a different Dockerfile
docker build -t myapp:slim -f Dockerfile.slim .

-f picks which Dockerfile to use. Teams often keep a slim, multi-stage Dockerfile for production.

Compare image sizes
docker images myapp

A multi-stage build copies only the final output into a small base image, so the result is much smaller and safer.

Build only one stage
docker build --target build -t myapp:build .

--target stops at a named stage. Handy to run tests inside the build stage in CI.

Build without the cache
docker build --no-cache -t myapp:1.0 .

Docker reuses cached layers. When something looks stale, --no-cache rebuilds every layer from scratch.

Free up disk space
docker image prune -a

Old images fill the disk on build servers. docker image prune -a removes every image no container is using.

Level 7: Debug Like a Pro (Advanced)

Find out why a container crashed or misbehaves.

Show stopped containers too
docker ps -a

docker ps only shows running containers. -a shows stopped ones with their exit status, which is where crashes show up.

Read the last log lines
docker logs --tail 20 api

--tail shows only the last lines, which is where the error usually is.

Read one detail with --format
docker inspect --format '{{.State.ExitCode}}' api

docker inspect prints a lot of JSON. --format picks one value, like the exit code or the IP address.

Copy a file out of a container
docker cp web:/etc/nginx/nginx.conf .

docker cp copies files between a container and your machine, even from a stopped container.

Explore an image with a shell
docker run --rm -it --entrypoint sh myapp:1.0

When a container crashes at start, open a shell in the same image instead. --entrypoint replaces the start command and --rm deletes the container afterwards.

Follow one Compose service
docker compose logs -f api

With Compose, read the logs of just one service. -f keeps following new lines.

Level 8: Security & Registries (Advanced)

Run containers safely and publish images to a private registry.

Do not run as root
docker run -d --user 1000 myapp:1.0

By default many containers run as root. --user runs the process as a normal user, which limits the damage if the app is hacked.

Read-only filesystem
docker run -d --read-only nginx

--read-only stops anything from writing into the container's filesystem, so an attacker cannot drop files. Use a volume for folders that must be writable.

Drop Linux capabilities
docker run -d --cap-drop ALL nginx

Capabilities are root's special powers. --cap-drop ALL removes them, and you add back only what the app needs.

Log in to a private registry
docker login ghcr.io

Companies keep images in private registries like GitHub Container Registry (ghcr.io), AWS ECR or Harbor.

Tag for the registry
docker tag myapp:1.0 ghcr.io/akops/myapp:1.0

The tag decides where the image is pushed: registry/owner/name:version.

Push to the private registry
docker push ghcr.io/akops/myapp:1.0

Now servers and pipelines with access can pull this exact image.

Level 9: Write a Dockerfile (Advanced)

Write a production Dockerfile for a Node.js app, line by line.

Pick a base image
FROM node:22-alpine

FROM is always the first line. Alpine images are small, which means faster pulls and fewer vulnerabilities.

Set the work folder
WORKDIR /app

WORKDIR creates the folder and makes it the current folder for the next lines.

Copy the package files first
COPY package*.json ./

Copy only package.json and the lock file first. Docker caches this layer, so npm install only reruns when dependencies change.

Install dependencies
RUN npm ci --omit=dev

npm ci installs exactly what's in the lock file. --omit=dev leaves test tools out of the image.

Copy the code
COPY . .

Now copy the rest of the app. Add a .dockerignore so node_modules and .git stay out.

Say which port
EXPOSE 3000

EXPOSE documents the port the app listens on.

Start the app
CMD ["node", "server.js"]

CMD is the command the container runs. The JSON form lets the app receive stop signals properly.

Level 10: Volumes & Data (Advanced)

Keep data safe when containers are replaced, and back it up.

List volumes
docker volume ls

Volumes store data outside the container, so it survives when the container is deleted.

Run a database with a volume
docker run -d --name db -v dbdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret postgres:16

-v dbdata:/var/lib/postgresql/data keeps the database files in the dbdata volume. -e sets an environment variable.

Where is the data?
docker volume inspect dbdata

inspect shows where Docker keeps the volume on the host.

Mount a folder read-only
docker run -d -v ./html:/usr/share/nginx/html:ro -p 8080:80 nginx

A bind mount shares a host folder with the container. :ro makes it read-only, so the container can't change your files.

Back up a volume
docker run --rm -v dbdata:/data -v ./:/backup alpine tar czf /backup/db.tgz /data

Run a throwaway container that mounts the volume and your folder, and tars the data. --rm deletes it afterwards.

Level 11: Container Networking (Expert)

Connect containers by name and debug network problems between them.

List networks
docker network ls

Every Docker host has bridge, host and none networks, plus any you create.

Run on a network
docker run -d --name api --network appnet myapp:1.0

Containers on the same user-made network reach each other by name. Docker runs a small DNS server for this.

Who is on the network?
docker network inspect appnet

inspect shows each container on the network and its IP.

Can api reach db?
docker exec api ping -c 1 db

Test from inside the container. If the name doesn't resolve, they're on different networks.

Join another network
docker network connect appnet web

A running container can be connected to more networks without restarting.

Which ports are published?
docker port web

docker port shows which host ports map to the container.

Level 12: Compose Like a Pro (Expert)

Run, scale and override multi-container apps with Docker Compose.

Check the final config
docker compose config

config merges your files, fills in variables and prints the result. It also catches YAML mistakes.

Rebuild and start
docker compose up -d --build

--build rebuilds images before starting, so your latest code runs.

Run 3 copies of the API
docker compose up -d --scale api=3

--scale runs several containers of one service. Don't publish a fixed host port for a scaled service.

Open a database shell
docker compose exec db psql -U postgres

exec runs a command inside a running service.

Production overrides
docker compose -f compose.yaml -f compose.prod.yaml up -d

Several -f files are merged in order. Keep shared settings in compose.yaml and prod changes in compose.prod.yaml.

Remove everything, data too
docker compose down -v

-v also deletes the volumes. Use it to reset a dev environment, never on production data.

Level 13: Health Checks & Restarts (Expert)

Make containers report their health and recover by themselves.

Add a health check
docker run -d --name web --health-cmd="curl -f http://localhost/ || exit 1" --health-interval=30s nginx

A health check is a command Docker runs regularly. If it fails, the container is marked unhealthy.

Find unhealthy containers
docker ps --filter health=unhealthy

Filter ps by health status.

Read the health status
docker inspect --format '{{.State.Health.Status}}' web

A Go template pulls out one value from inspect.

Restart on failure
docker run -d --restart=on-failure:3 myapp:1.0

on-failure:3 restarts a crashed container up to 3 times, then stops, so a broken app doesn't restart forever.

What just happened?
docker events --since 10m --until 0s

events streams what Docker did: starts, deaths, health changes and OOM kills.

Level 14: BuildKit & Multi-Platform (Expert)

Build one image for Intel and ARM servers, with build secrets and cache control.

List builders
docker buildx ls

buildx is Docker's BuildKit builder. It can build for other CPU types.

Make a multi-platform builder
docker buildx create --use --name multi

The docker-container driver can build for many platforms using emulation.

Build for amd64 and arm64
docker buildx build --platform linux/amd64,linux/arm64 -t akops/myapp:1.0 --push .

One tag, two images inside. Servers pull the right one automatically. Multi-platform builds must be pushed.

Check the platforms
docker manifest inspect akops/myapp:1.0

manifest inspect shows each platform inside the tag.

Use a secret while building
docker build --secret id=npmrc,src=.npmrc -t myapp:1.0 .

--secret passes a file to one RUN step without saving it in any layer. Never COPY tokens into an image.

Free the build cache
docker builder prune -f

The build cache can grow to many gigabytes.

Level 15: Image Security Scanning (Expert)

Scan images for vulnerabilities, block bad images in CI and sign the good ones.

Scan an image
trivy image myapp:1.0

Trivy checks every package in the image against known vulnerabilities (CVEs).

Only what matters
trivy image --severity HIGH,CRITICAL myapp:1.0

Filter to HIGH and CRITICAL so the team fixes real risks first.

Fail the build
trivy image --exit-code 1 --severity CRITICAL myapp:1.0

--exit-code 1 makes Trivy fail when it finds something. In CI this stops a vulnerable image from being deployed.

Make an SBOM
trivy image --format cyclonedx -o sbom.json myapp:1.0

An SBOM (software bill of materials) lists every component in the image. Many companies now require one.

Sign the image
cosign sign akops/myapp:1.0

cosign signs an image so your cluster can check it came from your pipeline and wasn't changed.