Skip to content

Commit 9d4ec06

Browse files
committed
addressed reviews
1 parent 2ba6061 commit 9d4ec06

File tree

1 file changed

+104
-27
lines changed

1 file changed

+104
-27
lines changed

src/assets/Debugging/debugging.md

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Advanced Docker Container Debugging - A comprehensive guide
2+
title: Advanced Docker Container Debugging - A Comprehensive Guide
33
authorName: Prajwol Amatya
44
authorAvatar: https://1.gravatar.com/avatar/de64e53c0e2cb393dd0d14ffdd53058ee9c607b35e366dd392425bd1b95a034c?size=256
55
authorLink: https://github.com/prajwolamatya
6-
createdAt: April 29, 2025
7-
tags: debugging, docker
6+
createdAt: May 07, 2025
7+
tags: debugging, docker, docker container
88
banner: https://blog.jankaritech.com/src/assets/Debugging/images/Debugging.png
99
---
1010

@@ -17,13 +17,13 @@ Our setup consists of:
1717

1818
## 1. Container Inspection
1919
### Viewing Running Containers
20-
First, let's check our running containers:
20+
First, let's check whether our containers are actually running:
2121

2222
```bash
23-
docker-compose ps
23+
docker compose ps
2424
```
2525

26-
**Expected Output:**
26+
**Example Output:**
2727
```console
2828
Name Command State Ports
2929
debug-docker_nginx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp
@@ -47,17 +47,39 @@ This returns a JSON with all container details including:
4747
- Environment variables
4848
- IP addresses
4949

50-
**Pro Tip:** Filter specific information:
50+
**Filter specific information:**
5151

5252
```bash
5353
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' debug-docker_nodejs-app_1
5454
```
5555

56+
Let's breakdown the command:
57+
- `-f` or `--format` activates Go template formatting
58+
- `{{range .NetworkSettings.Networks}}` iterates through networks
59+
- `{{.IPAddress}}` extracts the IP for each network
60+
- `{{end}}` closes the loop
61+
62+
**Example Output:**
63+
64+
(*The container's internal IP in Docker's network*)
65+
```console
66+
172.19.0.3
67+
```
68+
69+
Docker uses **Go Templates** for the `--format` filtering in `docker inspect`. You can get more details on how to construct the filter in Go's [text/template](https://pkg.go.dev/text/template) package.
70+
Also, you can learn more on formatting output in [Format command and log output](https://docs.docker.com/engine/cli/formatting/) section.
71+
5672
## 2. Log Analysis
5773
### Viewing Container Logs
5874

5975
```bash
60-
docker-compose logs
76+
docker compose logs
77+
```
78+
79+
This will return the logs of containers that are part of the docker compose setup. You can also get the container specific logs using the following command.
80+
81+
```bash
82+
docker compose logs nodejs-app
6183
```
6284

6385
**Example Output (when accessing the service):**
@@ -66,7 +88,7 @@ docker-compose logs
6688
debug-docker_nodejs-app_1 | Node.js server running on port 3000
6789
```
6890

69-
This confirms your Node.js application launched successfully
91+
This confirms your Node.js application launched successfully.
7092

7193
### Generating Request Logs
7294
Make a test request to generate logs:
@@ -75,7 +97,7 @@ Make a test request to generate logs:
7597
curl -v http://localhost:3000
7698
```
7799

78-
After the request, check logs again to see:
100+
After the request, check the logs again to see:
79101

80102
```console
81103
debug-docker_nodejs-app_1 | Node.js server running on port 3000
@@ -84,10 +106,10 @@ debug-docker_nodejs-app_1 | GET / 200 7.001 ms - 19
84106

85107
To view only GET requests from the last 5 minutes
86108
```bash
87-
docker logs --since 5m debug-docker_nodejs-app_1 | grep "GET"
109+
docker compose logs --since 5m nodejs-app | grep "GET"
88110
```
89111

90-
**Expected Output:**
112+
**Example Output:**
91113

92114
```console
93115
GET / 200 7.001 ms - 19
@@ -102,39 +124,89 @@ Test if Nginx can reach Node.js:
102124
docker exec debug-docker_nginx_1 ping nodejs-app
103125
```
104126

105-
**Expected Output:**
127+
- `docker exec`: Executes a command inside a running container (`debug-docker_nginx_1`)
128+
- `ping nodejs-app`: Calls the Linux `ping` utility to test network reachability to the hostname `nodejs-app`
129+
130+
**Example Output:**
131+
106132
```console
107133
PING nodejs-app (172.19.0.3): 56 data bytes
108134
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.060 ms
109135
```
136+
110137
- **Success:** <1ms response confirms network connectivity
111138
- **Failure:** Would show "unknown host" or timeout
112139

140+
Docker Compose automatically assigns hostnames to containers based on the service names defined in `docker-compose.yml`.
141+
142+
Example:
143+
144+
```yaml
145+
services:
146+
nodejs-app: # This becomes the hostname
147+
image: node:alpine
148+
```
149+
150+
Run `docker compose ps` to see the exact service/container names:
151+
152+
```bash
153+
docker compose ps --format "table {{.Name}}\t{{.Service}}"
154+
```
155+
Output:
156+
157+
```console
158+
NAME SERVICE
159+
debug-docker_nginx_1 nginx
160+
debug-docker_nodejs-app_1 nodejs-app
161+
```
162+
163+
Alternatively, you can use service names directly from `docker-compose.yml` instead of full container name.
164+
165+
```bash
166+
docker compose exec nginx ping nodejs-app
167+
```
168+
169+
**Example Output:**
170+
171+
```console
172+
PING nodejs-app (172.19.0.3): 56 data bytes
173+
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.042 ms
174+
```
175+
113176
### Examining Post Accessibility
114177
Check if Node.js is listening on port 3000 inside its container:
115178

116179
```bash
117-
docker exec debug-docker_nodejs-app_1 netstat -tuln
180+
docker exec nodejs-app netstat -tuln
118181
```
119182

120-
**Expected Output:**
183+
- `docker exec`: Runs a command inside a specific container (`nodejs-app`).
184+
- `netstat -tuln`: A Linux utility to list all listening network ports with the flags:
185+
- `-t`: Show TCP ports
186+
- `-u`: Show UDP ports
187+
- `-l`: Display only listening ports (services accepting connections)
188+
- `-n`: Show numeric addresses/ports
189+
190+
**Example Output:**
121191
```console
122192
Active Internet connections (only servers)
123193
Proto Recv-Q Send-Q Local Address Foreign Address State
124194
tcp 0 0 :::3000 :::* LISTEN
125195
```
126196

127197
- Shows Node.js listening on port 3000
128-
- No output means service isn't running properly
198+
- No output means the service isn't running properly
129199

130200
## 4. Interactive Debugging
131201
### Executing into Containers
132202

133203
For Node.js application debugging:
134204
```bash
135-
docker exec -it debug-docker_nodejs-app_1 sh
205+
docker exec -it nodejs-app sh
136206
```
137-
Now you can:
207+
208+
This gives you full shell access inside the container where you can run any Linux command (as long as the tool exists in the container). Following are few things that you can do.
209+
138210
1. Check running processes: `ps aux`
139211
```bash
140212
ps aux
@@ -190,13 +262,14 @@ PONG
190262
docker exec -it debug-docker_nginx_1 nginx -t
191263
```
192264

193-
**Expected Output:**
265+
**Example Output:**
194266
```console
195267
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
196268
nginx: configuration file /etc/nginx/nginx.conf test is successful
197269
```
198270

199271
## 5. Health Checks and Readiness Probes
272+
Docker health checks are automated tests that periodically verify if a container is functioning properly. Health checks transform your containers from static processes into self aware services that can catch issues like application crashes, frozen processes, dependency failures, etc.
200273
Let's enhance our `docker-compose.yml` with health checks:
201274

202275
```yaml
@@ -218,12 +291,16 @@ services:
218291
retries: 3
219292
```
220293

294+
- Docker runs the `test` command at your specified `interval` (e.g., every 30s)
295+
- The service is marked `healthy` only if the command succeeds (exit code 0)
296+
- After `retries` consecutive failures, it's marked `unhealthy`
297+
221298
Now check the container health:
222299
```bash
223300
docker ps --format "table {{.Names}}\t{{.Status}}"
224301
```
225302

226-
**Expected Output:**
303+
**Example Output:**
227304
```console
228305
NAMES STATUS
229306
debug-docker_nginx_1 Up 5 minutes
@@ -232,7 +309,7 @@ debug-docker_redis_1 Up 5 minutes (healthy)
232309
```
233310

234311
## 6. Temporary Debug Containers
235-
Sometimes you need additional tools. Create a temporary debug container in the same network:
312+
Sometimes you need additional tools that are not part of the containers you are using, for that create a temporary debug container with the needed tools in the same network:
236313
```bash
237314
docker run -it --rm --network debug-docker_default alpine sh
238315
```
@@ -243,7 +320,7 @@ Now from this container you can:
243320
nslookup nodejs-app
244321
```
245322

246-
**Expected Output:**
323+
**Example Output:**
247324
```console
248325
Server: 127.0.0.11
249326
Address: 127.0.0.11:53
@@ -261,7 +338,7 @@ Address: 172.19.0.3
261338
wget -qO- http://nodejs-app:3000
262339
```
263340

264-
**Expected Output:**
341+
**Example Output:**
265342
```console
266343
Hello from Node.js!
267344
```
@@ -291,10 +368,10 @@ docker stats
291368

292369
**Example Output:**
293370
```console
294-
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
295-
5111f46d640b debug-docker_nginx_1 0.00% 9.633MiB / 31.06GiB 0.03% 41.9kB / 126B 0B / 4.1kB 13
296-
e701e4d02bb0 debug-docker_nodejs-app_1 0.00% 13.31MiB / 31.06GiB 0.04% 45.3kB / 3.49kB 0B / 0B 7
297-
3e0399cc7510 debug-docker_redis_1 0.93% 4.691MiB / 31.06GiB 0.01% 42.3kB / 126B 1.43MB / 0B 6
371+
CONTAINER ID NAME CPU % MEM USAGE/LIMIT MEM % NET I/O BLOCK I/O PIDS
372+
5111f46d640b debug-docker_nginx_1 0.00% 9.633MiB/31.06GiB 0.03% 41.9kB/126B 0B/4.1kB 13
373+
e701e4d02bb0 debug-docker_nodejs-app_1 0.00% 13.31MiB/31.06GiB 0.04% 45.3kB/3.49kB 0B/0B 7
374+
3e0399cc7510 debug-docker_redis_1 0.93% 4.691MiB/31.06GiB 0.01% 42.3kB/126B 1.43MB/0B 6
298375
```
299376

300377
## Conclusion

0 commit comments

Comments
 (0)