The commands I type 50 times a day. Muscle memory at this point, but still worth documenting.

Container lifecycle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Run container (basic)
docker run nginx

# Run with name and port mapping
docker run -d --name web -p 8080:80 nginx

# Run with environment variables
docker run -d -e DB_HOST=localhost -e DB_PORT=5432 myapp

# Run with volume mount
docker run -d -v /host/path:/container/path nginx

# Run interactive with TTY
docker run -it ubuntu bash

# Run and auto-remove on exit
docker run --rm -it alpine sh

Container management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Stop container
docker stop container-name

# Start stopped container
docker start container-name

# Restart container
docker restart container-name

# Remove container
docker rm container-name

# Force remove running container
docker rm -f container-name

# Remove all stopped containers
docker container prune

Logs and exec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# View logs
docker logs container-name

# Follow logs (tail -f style)
docker logs -f container-name

# Last 100 lines
docker logs --tail 100 container-name

# Exec into running container
docker exec -it container-name bash

# Run single command
docker exec container-name ls /app

Images

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# List images
docker images

# Pull image
docker pull nginx:latest

# Build image from Dockerfile
docker build -t myapp:v1 .

# Build with custom Dockerfile
docker build -t myapp -f Dockerfile.prod .

# Tag image
docker tag myapp:v1 myapp:latest

# Remove image
docker rmi image-name

# Remove unused images
docker image prune

# Remove all unused images
docker image prune -a

Inspect and stats

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Container details
docker inspect container-name

# Live resource usage
docker stats

# Stats for specific container
docker stats container-name

# Disk usage
docker system df

# Detailed disk usage
docker system df -v

Cleanup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Remove stopped containers
docker container prune

# Remove unused images
docker image prune

# Remove unused volumes
docker volume prune

# Remove unused networks
docker network prune

# Nuclear option - clean everything unused
docker system prune

# Really nuclear - include volumes
docker system prune -a --volumes

Quick combinations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Stop and remove container
docker stop container-name && docker rm container-name

# Remove all stopped containers
docker rm $(docker ps -a -q)

# Stop all running containers
docker stop $(docker ps -q)

# Remove all images
docker rmi $(docker images -q)

Debugging

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Check container processes
docker top container-name

# View port mappings
docker port container-name

# Copy files (see separate note for details)
docker cp container:/path/file.txt ./

# Show container resource limits
docker inspect container-name | grep -i memory

Pro tip: Alias these if you type them daily:

1
2
3
alias dps='docker ps'
alias dex='docker exec -it'
alias dlogs='docker logs -f'