Skip to content

Commit 6a8c17f

Browse files
committed
addressed reviews
1 parent 2ba6061 commit 6a8c17f

File tree

1 file changed

+92
-15
lines changed

1 file changed

+92
-15
lines changed

src/assets/Debugging/debugging.md

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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
6+
createdAt: May 07, 2025
77
tags: debugging, docker
88
banner: https://blog.jankaritech.com/src/assets/Debugging/images/Debugging.png
99
---
@@ -20,7 +20,7 @@ Our setup consists of:
2020
First, let's check our running containers:
2121

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

2626
**Expected Output:**
@@ -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 all the running continers. 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:
@@ -84,7 +106,7 @@ 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

90112
**Expected Output:**
@@ -102,21 +124,69 @@ 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

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+
120190
**Expected Output:**
121191
```console
122192
Active Internet connections (only servers)
@@ -132,9 +202,11 @@ tcp 0 0 :::3000 :::* LISTEN
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
@@ -197,6 +269,7 @@ 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 vefity 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,6 +291,10 @@ 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}}"
@@ -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)