While learning Docker and Kubernetes I have looked for different cheat sheets and get tired of the advertisments, etc.. and so here is mine. These were run through AI for formatting and verification on commands. Please let me know if there are any errors.
There are extras in the downloads. For example finding the container’s IP and MAC address. The commands take considerable amount of effort to put into a webpage and were left out.
🐳 Docker Command Cheat Sheet
A clean and complete cheat sheet for working with Docker, covering everything from basic commands to advanced troubleshooting and networking.
📦 Images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # Search for images
docker search python
docker search --filter is-official=true python
# Pull an image
docker pull python
docker pull python:3.12-rc-bookworm
# List images
docker images
# Remove an image
docker rmi image_name
# Show image history
docker history image_name
|
🛠️ Build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # Build from Dockerfile in current directory
docker build .
# Specify a Dockerfile
docker build -f Dockerfile.custom .
# Disable cache
docker build --no-cache .
# Remove intermediate containers
docker build --rm=true .
# Force remove intermediate containers even if build fails
docker build --force-rm=true .
|
🧱 Containers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| # Run a container
docker run -it ubuntu /bin/bash
# Run with port mapping
docker run -d -p 8080:80 nginx
# Run with volume
docker run -v /host:/container ubuntu
# List all containers
docker ps -a
# Start/Stop/Restart container
docker start container_name
docker stop container_name
docker restart container_name
# Remove a container
docker rm container_name
# Remove all stopped containers
docker container prune
|
🕵️ Inspect / Logs / Stats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| # Inspect detailed info (JSON)
docker inspect container_name
# Show logs from container
docker logs container_name
# Show logs with timestamps and tail
docker logs --timestamps --tail 50 container_name
# Follow logs live
docker logs -f container_name
# Show resource usage
docker stats container_name
# View process tree inside container
docker top container_name
|
🌐 Networking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| # List Docker networks
docker network ls
# Inspect a network
docker network inspect bridge
# Create a new network
docker network create my-net
# Run container on a custom network
docker run --network=my-net alpine ping 8.8.8.8
# Connect/Disconnect container to/from network
docker network connect my-net container_name
docker network disconnect my-net container_name
# Get container IP address
docker inspect -f '' container_name
# Get container MAC address
docker inspect -f '' container_name
|
🧩 Volumes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # List volumes
docker volume ls
# Create a volume
docker volume create my-vol
# Inspect a volume
docker volume inspect my-vol
# Remove a volume
docker volume rm my-vol
# Prune all unused volumes
docker volume prune
|
🧪 Dockerfile Essentials
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| # Base image
FROM ubuntu:22.04
# Set working directory
WORKDIR /app
# Copy files
COPY . .
# Run command during build
RUN apt-get update && apt-get install -y curl
# Set environment variable
ENV APP_ENV=production
# Default command
CMD ["python3", "app.py"]
|
🧹 Clean Up
1
2
3
4
5
6
7
8
| # Remove all stopped containers
docker container prune
# Remove all unused images
docker image prune
# Remove unused volumes and networks
docker system prune --volumes
|
🔒 Security Tips
1
2
3
4
5
6
7
8
| # Scan an image for vulnerabilities (requires Docker Scan plugin)
docker scan image_name
# Run with limited capabilities
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE alpine
# Read-only filesystem
docker run --read-only nginx
|
🐳 Miscellaneous
1
2
3
4
5
6
7
8
9
10
11
12
| # Docker help
docker --help
docker run --help
# Get Docker version
docker version
# Get system-wide info
docker info
# Execute a command inside a running container
docker exec -it container_name /bin/bash
|