DevOps & Cloud ReferenceUpdated: 2026-11-06

Docker CLI, Container Lifecycle & Dockerfile Directives Cheat Sheet

Essential reference for container run, build, compose, network, volume, and image optimization commands.

Docker automates the deployment of applications inside lightweight, portable containers. Whether you are building production microservices or spinning up local development databases, this cheat sheet provides tested Docker CLI commands and Dockerfile syntax.

Container Lifecycle & Execution

Commands for starting, inspecting, and managing live container instances.

CommandDescriptionCommon Example
docker run -d --name <name> -p <host>:<cont> <img>Run container in background with port mappingdocker run -d -p 8080:80 --name web nginx:alpine
docker exec -it <container> <cmd>Execute interactive command inside running containerdocker exec -it web sh
docker logs -f --tail 100 <container>Stream real-time container log outputdocker logs -f --tail 50 web
docker stop / start / restart <container>Control container execution statedocker restart redis-cache
docker rm -f <container>Forcefully stop and remove a containerdocker rm -f web

Image Building & Registry

Commands for compiling Dockerfiles and pushing images.

CommandDescriptionCommon Example
docker build -t <tag> .Build image from Dockerfile in current directorydocker build -t myapp:v1.2 .
docker imagesList all locally cached Docker imagesdocker images -a
docker rmi <image_id>Delete local image by ID or namedocker rmi myapp:v1.0
docker tag <src> <target>Tag image for registry pushdocker tag app:v1 registry.hub.docker.com/u/app:v1

Disk Space & Resource Cleanup

Reclaim gigabytes of disk space occupied by dangling build caches and stopped containers.

CommandDescriptionRisk Level
docker system pruneRemove stopped containers, dangling images, and unused networksSafe
docker system prune -a --volumesRemove ALL unused images and anonymous volumesDestructive
docker builder prune -aClear BuildKit build cacheSafe

Interactive Client-Side Tools for this Guide

Test and run these patterns directly inside your browser with zero server uploads:

Frequently Asked Questions

What is the difference between CMD and ENTRYPOINT in Dockerfile?

ENTRYPOINT sets the default executable for the container that should always run, while CMD defines default arguments that can be easily overridden by arguments passed to docker run.

How do I reduce Docker image size?

Use Alpine Linux base images (e.g. node:alpine), leverage multi-stage builds to discard build tools from the final layer, and combine RUN commands with && to minimize layer count.