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 commandsHide commands
Level 1: Run Your First Container (Beginner)
Pull an image, run it and see it working.
- Check Docker works
docker versiondocker version prints the client and server (engine) versions - a quick health check.
- Download an image
docker pull nginxAn image is a ready-made template for a container. docker pull downloads one from Docker Hub.
- List images
docker imagesdocker images shows the images stored on your machine.
- Run a container
docker run -d -p 8080:80 nginxdocker run starts a container from an image. -d runs it in the background, -p HOST:CONTAINER opens a port.
- See running containers
docker psdocker 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 webdocker logs shows what the app printed. -f follows new lines live.
- Get a shell inside
docker exec -it web shdocker exec runs a command in a running container. -it gives you an interactive terminal.
- Stop a container
docker stop webdocker stop shuts a container down gracefully. docker start brings it back.
- Remove a container
docker rm webStopped 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.0Run 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.0To push to Docker Hub the image name must start with your username: USER/NAME:TAG.
- Log in to a registry
docker logindocker login stores credentials for Docker Hub (or another registry) so you can push.
- Push the image
docker push akops/myapp:1.0docker 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 appnetContainers on the same user-defined network can reach each other by name.
- Persist data with a volume
docker volume create dbdataContainer files vanish when the container is removed. A volume keeps data safe outside it.
- Start a stack with Compose
docker compose up -ddocker compose reads compose.yaml and starts every service together. -d runs in the background.
- Check the stack
docker compose psdocker compose ps lists only the containers that belong to this Compose project.
- Tear it down
docker compose downdocker compose down stops and removes the stack's containers and network (volumes stay unless you add -v).
- Clean up disk space
docker system prunedocker 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 webdocker inspect prints every detail of a container as JSON: IP address, mounts, env vars, restart policy.
- Live resource usage
docker statsdocker stats shows live CPU, memory and network usage for each running container.
- Limit memory
docker run -d --memory=512m nginxWithout limits, one container can eat the whole server's memory. --memory sets a hard cap.
- Auto-restart
docker update --restart unless-stopped webA 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.0Each 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.0Docker 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 myappA 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 -aOld 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 -adocker 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}}' apidocker 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.0When 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 apiWith 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.0By 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 nginxCapabilities 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.ioCompanies 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.0The tag decides where the image is pushed: registry/owner/name:version.
- Push to the private registry
docker push ghcr.io/akops/myapp:1.0Now 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-alpineFROM is always the first line. Alpine images are small, which means faster pulls and fewer vulnerabilities.
- Set the work folder
WORKDIR /appWORKDIR 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=devnpm 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 3000EXPOSE 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 lsVolumes 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 dbdatainspect 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 nginxA 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 /dataRun 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 lsEvery 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.0Containers 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 appnetinspect shows each container on the network and its IP.
- Can api reach db?
docker exec api ping -c 1 dbTest from inside the container. If the name doesn't resolve, they're on different networks.
- Join another network
docker network connect appnet webA running container can be connected to more networks without restarting.
- Which ports are published?
docker port webdocker 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 configconfig 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 postgresexec runs a command inside a running service.
- Production overrides
docker compose -f compose.yaml -f compose.prod.yaml up -dSeveral -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 nginxA health check is a command Docker runs regularly. If it fails, the container is marked unhealthy.
- Find unhealthy containers
docker ps --filter health=unhealthyFilter ps by health status.
- Read the health status
docker inspect --format '{{.State.Health.Status}}' webA Go template pulls out one value from inspect.
- Restart on failure
docker run -d --restart=on-failure:3 myapp:1.0on-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 0sevents 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 lsbuildx is Docker's BuildKit builder. It can build for other CPU types.
- Make a multi-platform builder
docker buildx create --use --name multiThe 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.0manifest 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 -fThe 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.0Trivy checks every package in the image against known vulnerabilities (CVEs).
- Only what matters
trivy image --severity HIGH,CRITICAL myapp:1.0Filter 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.0An SBOM (software bill of materials) lists every component in the image. Many companies now require one.
- Sign the image
cosign sign akops/myapp:1.0cosign signs an image so your cluster can check it came from your pipeline and wasn't changed.