Docker — Build, Ship, and Run Containers
Practical guide to Docker — container lifecycle, building images, volumes, networks, Compose, and cleanup on the command line, with examples.
Docker packages applications together with their dependencies into containers – lightweight, isolated units that run the same way on every system. Instead of "works on my machine" you get reproducible environments from laptop to production. From the command line you build images, start containers, map ports, mount volumes, and orchestrate whole stacks with Compose. This guide gathers the most important docker commands for everyday work – from your first docker run to targeted cleanup with prune.
Container Lifecycle
docker run <image> — Create and start a new container from an image.
docker run nginxdocker run -d <image> — Run a container in detached (background) mode.
docker run -d nginxdocker run -it <image> <command> — Run a container interactively with a TTY. Useful for shells.
docker run -it ubuntu bashdocker run --name <name> <image> — Run a container with a custom name for easier reference.
docker run --name my-web -d nginxdocker run --rm <image> — Automatically remove the container when it exits.
docker run --rm alpine echo "Hello"docker start <container> — Start a stopped container.
docker start my-webdocker stop <container> — Gracefully stop a running container (sends SIGTERM, then SIGKILL after timeout).
docker stop my-webdocker restart <container> — Stop and then start a container again.
docker restart my-webdocker kill <container> — Immediately stop a container by sending SIGKILL.
docker kill my-webdocker rm <container> — Remove a stopped container.
docker rm my-webdocker rm -f <container> — Force remove a running container (stop + remove).
docker rm -f my-webPorts & Environment
docker run -p <host_port>:<container_port> <image> — Map a host port to a container port.
docker run -d -p 8080:80 nginxdocker run -p <host_port>:<container_port>/udp <image> — Map a UDP port from host to container.
docker run -d -p 5353:53/udp dns-serverdocker run -P <image> — Map all exposed ports to random host ports.
docker run -d -P nginxdocker run -e <KEY>=<value> <image> — Set an environment variable inside the container.
docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql:8docker run --env-file <file> <image> — Load environment variables from a file.
docker run -d --env-file .env my-appdocker run --restart=<policy> <image> — Set restart policy: no, on-failure, always, unless-stopped.
docker run -d --restart=unless-stopped nginxVolumes & Mounts
docker run -v <host_path>:<container_path> <image> — Bind mount a host directory into the container.
docker run -d -v ./html:/usr/share/nginx/html nginxdocker run -v <host_path>:<container_path>:ro <image> — Bind mount as read-only inside the container.
docker run -d -v ./config:/etc/app/config:ro my-appdocker run -v <volume_name>:<container_path> <image> — Mount a named Docker volume into the container.
docker run -d -v db-data:/var/lib/mysql mysql:8docker volume create <name> — Create a named volume for persistent data storage.
docker volume create db-datadocker volume ls — List all Docker volumes.
docker volume lsdocker volume inspect <name> — Show detailed information about a volume.
docker volume inspect db-datadocker volume rm <name> — Remove a volume. Fails if the volume is in use.
docker volume rm db-datadocker volume prune — Remove all unused volumes to free disk space.
docker volume pruneListing & Inspecting
docker ps — List all running containers.
docker psdocker ps -a — List all containers including stopped ones.
docker ps -adocker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" — List containers with custom output format.
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"docker inspect <container> — Show detailed JSON information about a container.
docker inspect my-webdocker inspect --format '{{.NetworkSettings.IPAddress}}' <container> — Extract a specific field from container metadata using Go templates.
docker inspect --format '{{.NetworkSettings.IPAddress}}' my-webdocker port <container> — Show port mappings for a container.
docker port my-webdocker stats — Show live CPU, memory, network, and disk I/O usage for all running containers.
docker statsdocker top <container> — Show running processes inside a container.
docker top my-webLogs & Exec
docker logs <container> — Show the logs (stdout/stderr) of a container.
docker logs my-webdocker logs -f <container> — Follow (tail) the log output in real-time.
docker logs -f my-webdocker logs --tail <n> <container> — Show only the last N lines of logs.
docker logs --tail 50 my-webdocker logs --since <time> <container> — Show logs since a timestamp or relative time.
docker logs --since 30m my-webdocker exec -it <container> <command> — Run a command interactively inside a running container.
docker exec -it my-web bashdocker exec <container> <command> — Run a command inside a running container (non-interactive).
docker exec my-web cat /etc/nginx/nginx.confdocker exec -u <user> <container> <command> — Run a command as a specific user inside the container.
docker exec -u root my-web apt-get updateCopy & Diff
docker cp <container>:<path> <host_path> — Copy files from a container to the host.
docker cp my-web:/etc/nginx/nginx.conf ./nginx.confdocker cp <host_path> <container>:<path> — Copy files from the host into a container.
docker cp ./index.html my-web:/usr/share/nginx/html/docker diff <container> — Show filesystem changes made inside a container (A=added, C=changed, D=deleted).
docker diff my-webImages
docker images — List all locally available images.
docker imagesdocker pull <image> — Download an image from a registry.
docker pull nginx:alpinedocker pull <image>:<tag> — Pull a specific tagged version of an image.
docker pull node:22-slimdocker push <image> — Upload an image to a registry.
docker push myuser/my-app:latestdocker tag <source_image> <target_image> — Create a new tag (alias) for an existing image.
docker tag my-app:latest myuser/my-app:v1.0docker rmi <image> — Remove a local image.
docker rmi nginx:alpinedocker image prune — Remove all dangling (untagged) images.
docker image prunedocker image prune -a — Remove all images not used by any container.
docker image prune -adocker history <image> — Show the layer history of an image.
docker history nginx:alpinedocker save -o <file> <image> — Export an image to a tar archive.
docker save -o my-app.tar my-app:latestdocker load -i <file> — Import an image from a tar archive.
docker load -i my-app.tarBuilding Images
docker build -t <name>:<tag> . — Build an image from a Dockerfile in the current directory.
docker build -t my-app:latest .docker build -t <name> -f <dockerfile> . — Build using a specific Dockerfile.
docker build -t my-app -f Dockerfile.prod .docker build --no-cache -t <name> . — Build without using the layer cache. Forces a fresh build.
docker build --no-cache -t my-app:latest .docker build --build-arg <KEY>=<value> -t <name> . — Pass a build-time variable to the Dockerfile.
docker build --build-arg NODE_ENV=production -t my-app .docker build --target <stage> -t <name> . — Build only up to a specific stage in a multi-stage Dockerfile.
docker build --target builder -t my-app:build .docker build --platform <platform> -t <name> . — Build for a specific platform architecture.
docker build --platform linux/amd64 -t my-app .Networks
docker network ls — List all Docker networks.
docker network lsdocker network create <name> — Create a new bridge network.
docker network create my-networkdocker network inspect <name> — Show detailed information about a network including connected containers.
docker network inspect my-networkdocker network connect <network> <container> — Connect a running container to a network.
docker network connect my-network my-webdocker network disconnect <network> <container> — Disconnect a container from a network.
docker network disconnect my-network my-webdocker run --network <name> <image> — Run a container attached to a specific network.
docker run -d --network my-network --name api my-apidocker network rm <name> — Remove a network. Fails if containers are connected.
docker network rm my-networkdocker network prune — Remove all unused networks.
docker network pruneDocker Compose
docker compose up — Create and start all services defined in docker-compose.yml.
docker compose updocker compose up -d — Start all services in detached (background) mode.
docker compose up -ddocker compose up --build — Rebuild images before starting services.
docker compose up --build -ddocker compose down — Stop and remove all containers, networks created by up.
docker compose downdocker compose down -v — Stop and remove containers, networks, and volumes.
docker compose down -vdocker compose ps — List containers managed by the current Compose project.
docker compose psdocker compose logs <service> — Show logs for a specific service.
docker compose logs webdocker compose logs -f — Follow logs for all services in real-time.
docker compose logs -fdocker compose exec <service> <command> — Execute a command inside a running Compose service container.
docker compose exec web bashdocker compose run <service> <command> — Run a one-off command in a new container for a service.
docker compose run web npm testdocker compose pull — Pull the latest images for all services.
docker compose pulldocker compose build — Build or rebuild all service images.
docker compose builddocker compose restart <service> — Restart a specific service.
docker compose restart webdocker compose config — Validate and display the resolved Compose configuration.
docker compose configCleanup & System
docker system df — Show Docker disk usage: images, containers, volumes, and build cache.
docker system dfdocker system df -v — Show detailed disk usage with individual item sizes.
docker system df -vdocker system prune — Remove all stopped containers, unused networks, dangling images, and build cache.
docker system prunedocker system prune -a — Remove everything unused: all images, containers, networks, and cache.
docker system prune -adocker system prune -a --volumes — Full cleanup including unused volumes. Frees maximum disk space.
docker system prune -a --volumesdocker container prune — Remove all stopped containers.
docker container prunedocker system info — Show system-wide Docker information (version, storage driver, OS, etc.).
docker system infodocker version — Show Docker client and server version details.
docker versionResource Limits
docker run -m <memory> <image> — Set a memory limit for the container.
docker run -d -m 512m nginxdocker run --cpus=<number> <image> — Limit the number of CPU cores the container can use.
docker run -d --cpus=1.5 my-appdocker run --memory-swap=<limit> <image> — Set memory + swap limit. Use -1 for unlimited swap.
docker run -d -m 512m --memory-swap=1g my-appdocker run --pids-limit=<number> <image> — Limit the number of processes inside the container.
docker run -d --pids-limit=100 my-appdocker update --memory <limit> <container> — Update resource limits on a running container.
docker update --memory 1g my-web Conclusion
Docker has become indispensable in modern development and DevOps: what starts as a single container quickly grows into multi-stage builds, Compose stacks, and CI/CD pipelines. Once the commands in this guide feel natural, look next at lean multi-stage images, named volumes for persistent data, and regular prune runs so disk usage stays under control.
Further Reading
- Docker documentation – official reference and guides
- docker CLI reference – every command and option
- Docker (software) – Wikipedia – background and architecture
Related Commands
- ddev – local Docker-based development environments for PHP projects
- docker-compose – define multi-container applications declaratively
- helm – package manager for Kubernetes applications