🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

docker Command

Intermediate Containers man(1)

Container runtime and management tool

📅 Updated: Mar 16, 2026
SYNTAX
docker [OPTIONS] COMMAND [ARG...]

What Does docker Do?

The docker command is the primary CLI for Docker — the industry-standard platform for building, shipping, and running containerized applications. Docker enables you to package applications with all their dependencies into lightweight, portable containers that run consistently across any environment.

Docker containers share the host kernel but isolate application processes, filesystems, and networking through Linux namespaces and cgroups. This makes containers dramatically lighter than virtual machines — starting in milliseconds instead of minutes, using megabytes instead of gigabytes.

The docker CLI interacts with the Docker daemon (dockerd) to manage the complete container lifecycle: pulling images from registries, building images from Dockerfiles, running containers, managing networks and volumes, and pushing images to registries. Since Docker Compose V2, multi-container orchestration is also available as docker compose (integrated subcommand).

Docker is essential for modern DevOps workflows, CI/CD pipelines, microservices architecture, and consistent development environments. Most cloud platforms (AWS, Azure, GCP) and orchestration tools (Kubernetes, Swarm) use Docker-compatible container images as their standard deployment unit.

Options & Flags

OptionDescriptionExample
run Create and start a container from an image docker run -d --name web -p 8080:80 nginx:alpine
ps List running containers (-a for all) docker ps -a
build Build an image from a Dockerfile docker build -t myapp:latest .
pull Download an image from a registry docker pull postgres:16-alpine
push Upload an image to a registry docker push myregistry/myapp:v1
exec Execute a command inside a running container docker exec -it web bash
logs View container stdout/stderr logs docker logs -f --tail 100 web
stop/start/restart Manage container lifecycle docker stop web && docker start web
compose up -d Start multi-container applications from compose.yaml docker compose up -d --build
system prune Remove unused containers, images, networks, and volumes docker system prune -a --volumes

Practical Examples

#1 Run a container

Run an Nginx container: detached (-d), named, port-mapped, with a bind mount for serving local HTML files.
$ docker run -d --name webserver -p 8080:80 -v ./html:/usr/share/nginx/html nginx:alpine

#2 Build an image from Dockerfile

Build a Docker image from the current directory Dockerfile, targeting the production stage in multi-stage build.
$ docker build -t myapp:v1.0 --target production .

#3 Execute shell in running container

Open an interactive shell inside a running container for debugging.
$ docker exec -it webserver sh

#4 View and follow logs

Follow container logs from the last 5 minutes. Useful for debugging application issues.
$ docker logs -f --since 5m webserver

#5 Copy files between host and container

Copy the Nginx config file from inside the container to the host filesystem.
$ docker cp webserver:/etc/nginx/nginx.conf ./nginx.conf

#6 Inspect container details

Get detailed JSON metadata about a container. Pipe through jq to extract specific fields.
$ docker inspect webserver | jq '.[0].NetworkSettings.IPAddress'

#7 Clean up everything

Remove all stopped containers, unused images, unused networks, and unused volumes. Frees disk space.
$ docker system prune -a --volumes -f

#8 Run a one-off command

Run npm install using a Node container without installing Node locally. Container is removed after execution (--rm).
$ docker run --rm -v $(pwd):/app -w /app node:20-alpine npm install

Tips & Best Practices

Always pin image versions: Never use :latest in production. Pin versions like nginx:1.25-alpine or postgres:16.3-alpine to ensure reproducible builds.
docker system prune is destructive: docker system prune -a --volumes removes ALL unused images, containers, and volumes. This includes stopped containers and their data. Always check with docker ps -a first.
Use --rm for temporary containers: For one-off commands or testing, use docker run --rm to automatically remove the container when it exits. Prevents accumulation of stopped containers.
Multi-stage builds: Use multi-stage Dockerfiles to keep production images small. Build in one stage, copy only the binary/output to a minimal runtime stage (alpine or distroless).
Docker vs Podman: Podman is a daemonless, rootless Docker alternative. Most docker commands work with podman by aliasing docker=podman. Podman is default on RHEL/Fedora.

Frequently Asked Questions

How do I run a Docker container in the background?
Use the -d (detach) flag: docker run -d --name myapp nginx. The container runs in the background. View logs with docker logs myapp.
How do I stop and remove all Docker containers?
Stop all: docker stop $(docker ps -q). Remove all: docker rm $(docker ps -aq). Or use docker container prune to remove only stopped containers.
What is the difference between docker run and docker exec?
docker run creates a NEW container from an image. docker exec runs a command in an EXISTING running container. Use exec for debugging, run for starting new containers.
How do I reduce Docker image size?
Use multi-stage builds, Alpine or distroless base images, .dockerignore file to exclude unnecessary files, and combine RUN commands to reduce layers.
How do I persist data in Docker containers?
Use volumes: docker run -v mydata:/app/data myapp. Named volumes persist across container restarts and removals. Bind mounts (-v ./local:/container) map host directories.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →