Skip to content

Commit 9dba583

Browse files
committed
Added Docker Cheatsheets and Dockerfile Master Template
1 parent c3584c2 commit 9dba583

11 files changed

Lines changed: 555 additions & 0 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Check Docker is installed and running
3+
4+
```bash
5+
docker --version
6+
docker version
7+
docker info
8+
docker system info
9+
```
10+
11+
Docker daemon status (Linux)
12+
13+
```bash
14+
sudo systemctl status docker
15+
sudo systemctl start docker
16+
sudo systemctl restart docker
17+
sudo journalctl -u docker -f
18+
```
19+
20+
Add user to docker group (Linux)
21+
22+
```bash
23+
sudo usermod -aG docker $USER
24+
newgrp docker
25+
```
26+
27+
Test run
28+
29+
```bash
30+
docker run hello-world
31+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Run as non-root
2+
3+
Prefer images that support non-root
4+
5+
In Dockerfile:
6+
```dockerfile
7+
USER 1000:1000
8+
```
9+
10+
Reduce image size
11+
12+
Use smaller base images (alpine when appropriate)
13+
14+
Combine RUN layers:
15+
```dockerfile
16+
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
17+
```
18+
19+
Secrets
20+
21+
Don’t bake secrets into images
22+
23+
Use env files carefully:
24+
```dockerfile
25+
docker run --env-file .env myapp
26+
```
27+
28+
Health checks
29+
```dockerfile
30+
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD curl -f http://localhost:8080/health || exit 1
31+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Help + quick docs
2+
3+
```bash
4+
docker --help
5+
docker <command> --help
6+
docker help <command>
7+
```
8+
9+
Common “what’s running / what exists”
10+
11+
```bash
12+
docker ps
13+
docker ps -a
14+
15+
docker images
16+
docker image ls
17+
18+
docker container ls
19+
docker container ls -a
20+
21+
docker volume ls
22+
docker network ls
23+
```
24+
25+
Inspect / metadata
26+
27+
```bash
28+
docker inspect <container_or_image>
29+
docker image inspect <image>
30+
docker volume inspect <volume>
31+
docker network inspect <network>
32+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Pulling images
2+
3+
```bash
4+
docker pull nginx
5+
docker pull nginx:latest
6+
docker pull ubuntu:22.04
7+
```
8+
List images
9+
10+
```bash
11+
docker images
12+
docker image ls
13+
docker image ls -a
14+
```
15+
16+
Tagging images
17+
18+
```bash
19+
docker tag <source_image>:<tag> <new_image>:<tag>
20+
docker tag myapp:1.0 myapp:latest
21+
```
22+
23+
Build images
24+
25+
```bash
26+
docker build -t myapp:1.0 .
27+
docker build -t myapp:latest .
28+
docker build -f Dockerfile -t myapp:1.0 .
29+
docker build --no-cache -t myapp:nocache .
30+
```
31+
32+
Build with build args
33+
34+
```bash
35+
docker build --build-arg KEY=value -t myapp:1.0 .
36+
```
37+
38+
History + layers
39+
40+
```bash
41+
docker history <image>
42+
docker image history <image>
43+
```
44+
45+
Remove images
46+
47+
```bash
48+
docker rmi <image>
49+
docker image rm <image>
50+
docker rmi -f <image>
51+
```
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Run containers (core patterns)
2+
3+
Foreground interactive
4+
5+
```bash
6+
docker run -it ubuntu bash
7+
docker run -it alpine sh
8+
```
9+
10+
Detached (background)
11+
12+
```bash
13+
docker run -d nginx
14+
docker run -d --name web nginx
15+
```
16+
17+
Naming + hostname
18+
```bash
19+
docker run -d --name api --hostname api-host nginx
20+
```
21+
22+
Port mapping
23+
```bash
24+
docker run -d -p 8080:80 nginx
25+
docker run -d -p 127.0.0.1:8080:80 nginx
26+
```
27+
28+
Environment variables
29+
```bash
30+
docker run -d -e KEY=value nginx
31+
docker run -d --env KEY=value nginx
32+
docker run -d --env-file .env nginx
33+
```
34+
35+
Restart policies
36+
```bash
37+
docker run -d --restart=no nginx
38+
docker run -d --restart=always nginx
39+
docker run -d --restart=unless-stopped nginx
40+
docker run -d --restart=on-failure nginx
41+
```
42+
43+
Container lifecycle commands
44+
45+
```bash
46+
docker start <container>
47+
docker stop <container>
48+
docker restart <container>
49+
50+
docker kill <container>
51+
52+
docker pause <container>
53+
docker unpause <container>
54+
```
55+
56+
Remove containers
57+
```bash
58+
docker rm <container>
59+
docker rm -f <container>
60+
docker container rm <container>
61+
```
62+
63+
Remove all stopped containers
64+
```bash
65+
docker container prune
66+
```
67+
68+
Exec into a running container
69+
```bash
70+
docker exec -it <container> bash
71+
docker exec -it <container> sh
72+
```
73+
74+
Run a one-off command inside:
75+
```bash
76+
docker exec <container> ls -la
77+
docker exec <container> printenv
78+
```
79+
80+
Logs and debugging
81+
View logs
82+
```bash
83+
docker logs <container>
84+
docker logs -f <container>
85+
docker logs --tail 100 <container>
86+
docker logs --since 10m <container>
87+
```
88+
89+
Check processes
90+
```bash
91+
docker top <container>
92+
docker stats
93+
docker stats <container>
94+
```
95+
Port + config troubleshooting
96+
```bash
97+
docker port <container>
98+
docker inspect <container>
99+
```
100+
101+
Copy files in/out
102+
```bash
103+
docker cp <container>:/path/in/container ./localpath
104+
docker cp ./localfile <container>:/path/in/container
105+
```
106+
107+
Clean “run and delete after exit”
108+
```bash
109+
docker run --rm alpine echo "hi"
110+
docker run --rm -it ubuntu bash
111+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Named volumes (recommended for persistence)
2+
3+
Create volume
4+
```bash
5+
docker volume create mydata
6+
docker volume ls
7+
docker volume inspect mydata
8+
```
9+
10+
Use volume
11+
```bash
12+
docker run -d --name db -v mydata:/var/lib/mysql mysql
13+
docker run -d --name pg -v mydata:/var/lib/postgresql/data postgres
14+
```
15+
16+
Remove volume
17+
```bash
18+
docker volume rm mydata
19+
docker volume prune
20+
```
21+
22+
Bind mounts (map host path → container path)
23+
```bash
24+
docker run -it -v /host/path:/container/path ubuntu bash
25+
docker run -it --mount type=bind,source=/host/path,target=/container/path ubuntu bash
26+
```
27+
28+
Common dev example:
29+
```bash
30+
docker run -it -v $(pwd):/app -w /app node:20 bash
31+
```
32+
33+
Read-only mounts
34+
```bash
35+
docker run -it -v $(pwd):/app:ro ubuntu bash
36+
docker run -it --mount type=bind,source=$(pwd),target=/app,readonly ubuntu bash
37+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Basics
2+
3+
```bash
4+
docker network ls
5+
docker network inspect bridge
6+
```
7+
8+
Create custom network
9+
10+
```bash
11+
docker network create mynet
12+
docker network ls
13+
docker network inspect mynet
14+
```
15+
16+
Run container in network
17+
```bash
18+
docker run -d --name web --network mynet nginx
19+
```
20+
Connect/disconnect existing container
21+
```bash
22+
docker network connect mynet <container>
23+
docker network disconnect mynet <container>
24+
```
25+
26+
Remove network
27+
```bash
28+
docker network rm mynet
29+
docker network prune
30+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Common Dockerfile instructions
2+
3+
```dockerfile
4+
FROM ubuntu:22.04
5+
WORKDIR /app
6+
COPY . .
7+
RUN apt-get update && apt-get install -y curl
8+
ENV PORT=8080
9+
EXPOSE 8080
10+
CMD ["bash"]
11+
```
12+
13+
Key instructions
14+
15+
FROM — base image
16+
WORKDIR — sets working dir
17+
COPY / ADD — copy files
18+
RUN — execute build-time commands
19+
ENV — environment variables
20+
EXPOSE — document port
21+
CMD — default command
22+
ENTRYPOINT — fixed entry command
23+
ARG — build-time variables
24+
USER — run as non-root
25+
HEALTHCHECK — container health
26+
VOLUME — declare mount point
27+
28+
Build using Dockerfile
29+
```bash
30+
docker build -t myapp:1.0 .
31+
docker build -f Dockerfile -t myapp:1.0 .
32+
```
33+
34+
35+
Multi-stage build (pattern)
36+
37+
```dockerfile
38+
FROM node:20 AS build
39+
WORKDIR /app
40+
COPY package*.json ./
41+
RUN npm ci
42+
COPY . .
43+
RUN npm run build
44+
45+
FROM nginx:alpine
46+
COPY --from=build /app/dist /usr/share/nginx/html
47+
```

0 commit comments

Comments
 (0)