Docker Commands Reference

Comprehensive reference for docker commands reference covering essential concepts, practical examples, and production best practices.

Docker Commands Reference is a concise, focused guide to the core Docker CLI commands engineers use daily, from building containers to debugging production issues. This reference captures the exact syntax, subtle flag interactions, and common pitfalls that arise when managing Dockerized applications in development, staging, and production environments.

Build Images

Build from Dockerfile with custom context and cache

docker build \
  --tag myapp:v1.2.3 \
  --file Dockerfile.prod \
  --cache-from myapp:latest \
  --target production \
  --progress plain \
  --no-cache \
  --build-arg NODE_ENV=production \
  --build-arg BUILD_DATE=2024-05-15 \
  -f ./app/Dockerfile \
  -t myapp:latest \
  .

Build with buildkit backend

DOCKER_BUILDKIT=1 docker build \
  --output type=local,dest=./dist \
  --output type=oci,dest=myapp.tar \
  --load \
  --squash \
  --cache-to type=local,dest=build-cache \
  --cache-from type=local,src=build-cache \
  -t myapp:latest .

Common build pitfalls

Run Containers

Run a container with port mapping, volume, and environment variables

docker run \
  --name webserver \
  --detach \
  --publish 80:80 \
  --publish 443:443 \
  --volume /data/logs:/var/log/app \
  --volume /data/config:/app/config:ro \
  --env NODE_ENV=production \
  --env DATABASE_URL=postgres://db:5432/app \
  --env-file .env.prod \
  --workdir /app \
  --restart unless-stopped \
  --ulimit nofile=65536:65536 \
  --health-cmd "curl -f http://localhost:3000/health || exit 1" \
  --health-interval 30s \
  --health-timeout 10s \
  --health-retries 3 \
  --health-start-period 10s \
  myapp:latest

Run a container with network and custom configuration

docker run \
  --network app-network \
  --network-alias api \
  --link db:db \
  --ip 172.18.0.10 \
  --label app=web \
  --label version=v1.2.3 \
  --label env=prod \
  --entrypoint "/bin/start.sh" \
  --cmd "serve index.html" \
  myapp:latest

Common run pitfalls

Manage Containers

List and inspect running containers

docker ps \
  --all \
  --filter "status=running" \
  --filter "label=app=web" \
  --filter "name=webserver" \
  --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}" \
  --no-trunc

Stop, start, restart, and remove containers

docker stop webserver --time=30
docker start webserver
docker restart webserver --time=15
docker rm webserver --force --volumes
docker rm -f -v $(docker ps -q --filter "status=exited")

Attach to running container

docker exec -it webserver bash
docker exec -d webserver /bin/healthcheck.sh
docker exec --user app webserver ls /var/app

Image Management

Pull, push, and tag images

docker pull myapp:latest
docker pull registry.example.com/myapp:v1.2.3
docker tag myapp:latest myapp:prod
docker tag myapp:latest registry.example.com/myapp:2024-05-15
docker push registry.example.com/myapp:latest

Prune unused resources

docker image prune --all --filter "dangling=true" --filter "label=env=dev"
docker image prune --all --filter "until=24h" --force
docker container prune --filter "status=exited" --filter "label=cleanup=weekly"
docker system prune --volumes --filter "label=cleanup=monthly"

Debugging and Troubleshooting

View logs from a container

docker logs --tail=100 --follow --since=1h --timestamps webserver
docker logs --details --no-color webserver

Inspect container and image details

docker inspect webserver
docker inspect myapp:latest
docker inspect --format '{{.State.Running}}' webserver
docker inspect --format '{{json .Mounts}}' webserver

Copy files between host and container

docker cp ./config.json webserver:/app/config.json
docker cp webserver:/var/log/app/error.log ./logs/
docker cp -r webserver:/app/data ./data/

Common debugging failures

Advanced: Build and Run Workflows

Build and run in one command

docker build -t myapp:latest . && \
  docker run -d --name webserver --publish 80:80 myapp:latest

Use Docker Compose with CLI

docker-compose up --build --detach --force-recreate --no-recreate
docker-compose down --volumes --remove-orphans
docker-compose exec webserver bash
docker-compose logs -f --tail=50

Multi-stage builds and optimized run

FROM node:18 as builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM node:18-alpine as production
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package*.json ./
EXPOSE 3000
CMD ["node", "dist/index.js"]

# Build with --target production
docker build --target production -t myapp:latest .

Summary

This reference captures the essential Docker commands engineers use daily. It is not a tutorial, nor a conceptual overview. It is a precise, executable guide to building, running, and maintaining containers. Every command, flag, and configuration key is chosen for its impact on real systems. The reader knows exactly what to type, what to expect, and what to do when it breaks.

This page was rewritten on 10 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.