Quick reference guide for Docker commands and best practices. All your notes are saved locally in your browser.
Display Docker version information
docker --version
docker version # More detailed infoDisplay system-wide information
docker infoGet help for Docker commands
docker --help
docker <command> --helpShow all Docker images on your system
docker images
docker image lsDownload an image from Docker Hub
docker pull <image-name>
docker pull ubuntu:22.04
docker pull nginx:latestBuild an image from a Dockerfile
docker build -t <image-name>:<tag> .
docker build -t myapp:1.0 .
docker build -t myapp:latest --no-cache .Delete Docker images
docker rmi <image-id>
docker rmi <image-name>:<tag>
docker image prune # Remove unused images
docker image prune -a # Remove all unused imagesCreate a new tag for an existing image
docker tag <source-image> <target-image>:<tag>
docker tag myapp:latest myapp:1.0Upload an image to a registry
docker push <image-name>:<tag>
docker push username/myapp:latestCreate and start a new container
docker run <image-name>
docker run -d <image-name> # Detached mode
docker run -it <image-name> bash # Interactive mode
docker run -p 8080:80 <image-name> # Port mapping
docker run --name mycontainer <image-name> # Named container
docker run -v /host/path:/container/path <image-name> # Volume mountShow running or all containers
docker ps # Running containers
docker ps -a # All containers (including stopped)
docker container ls
docker container ls -aControl container lifecycle
docker stop <container-id>
docker start <container-id>
docker restart <container-id>
docker pause <container-id>
docker unpause <container-id>Delete stopped containers
docker rm <container-id>
docker rm -f <container-id> # Force remove running container
docker container prune # Remove all stopped containersView container output
docker logs <container-id>
docker logs -f <container-id> # Follow log output
docker logs --tail 100 <container-id> # Last 100 linesRun commands inside a running container
docker exec <container-id> <command>
docker exec -it <container-id> bash
docker exec -it <container-id> shDisplay detailed information about a container
docker inspect <container-id>
docker inspect --format='{{.NetworkSettings.IPAddress}}' <container-id>Display real-time resource usage statistics
docker stats
docker stats <container-id>Example of a simple Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Optimize image size with multi-stage builds
# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Production stage
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./
RUN npm install --production
EXPOSE 3000
CMD ["node", "dist/server.js"]Key Dockerfile commands
FROM <image> # Base image
WORKDIR /path # Set working directory
COPY <src> <dest> # Copy files
ADD <src> <dest> # Copy files (can extract archives)
RUN <command> # Execute command during build
ENV KEY=value # Set environment variable
EXPOSE <port> # Document exposed ports
CMD ["executable", "param1"] # Default container command
ENTRYPOINT ["executable"] # Configure container executable
VOLUME /data # Create mount point
USER username # Set user for subsequent commands
ARG VAR=default # Build-time variableCreate a named volume
docker volume create <volume-name>
docker volume create mydataShow all volumes
docker volume lsDisplay detailed volume information
docker volume inspect <volume-name>Delete volumes
docker volume rm <volume-name>
docker volume prune # Remove all unused volumesAttach a volume to a container
docker run -v <volume-name>:/container/path <image>
docker run -v mydata:/app/data nginx
docker run --mount source=mydata,target=/app/data nginxShow all Docker networks
docker network lsCreate a new network
docker network create <network-name>
docker network create mynetwork
docker network create --driver bridge mynetworkAdd a container to a network
docker network connect <network-name> <container-id>
docker run --network=<network-name> <image>Display detailed network information
docker network inspect <network-name>Delete a network
docker network rm <network-name>
docker network prune # Remove unused networksExample compose file
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: example
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:Common docker-compose operations
docker-compose up # Start services
docker-compose up -d # Start in detached mode
docker-compose down # Stop and remove containers
docker-compose ps # List containers
docker-compose logs # View logs
docker-compose logs -f # Follow logs
docker-compose exec <service> <command> # Execute command
docker-compose build # Build images
docker-compose restart # Restart servicesBuild custom images with compose
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
NODE_ENV: production
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_DB: myapp
POSTGRES_USER: user
POSTGRES_PASSWORD: passwordClean up unused Docker resources
docker system prune # Remove unused data
docker system prune -a # Remove all unused data
docker system prune --volumes # Include volumes
docker system df # Show disk usageClean up stopped containers
docker container prune
docker rm $(docker ps -a -q) # Remove all containersDelete dangling and unused images
docker image prune # Remove dangling images
docker image prune -a # Remove all unused images
docker rmi $(docker images -q) # Remove all images.dockerignore to exclude unnecessary files from your image