Skip to content

Commit 796f4b8

Browse files
committed
add blog post on debugging docker container
1 parent 0c1007c commit 796f4b8

File tree

2 files changed

+300
-0
lines changed

2 files changed

+300
-0
lines changed

src/assets/Debugging/debugging.md

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
---
2+
title: Advanced Docker Container Debugging - A comprensive guide
3+
authorName: Prajwol Amatya
4+
authorAvatar: https://1.gravatar.com/avatar/de64e53c0e2cb393dd0d14ffdd53058ee9c607b35e366dd392425bd1b95a034c?size=256
5+
authorLink: https://github.com/prajwolamatya
6+
createdAt: April 29, 2025
7+
tags: debugging, docker
8+
banner: https://blog.jankaritech.com/src/assets/Debugging/images/Debugging.png
9+
---
10+
11+
Docker has revolutionized modern software development by enabling lightweight, portable, and scalable containerized applications. However, as deployments grow in complexity, so do the challenges in debugging and optimizing containers. In this blog, we'll explore powerful debugging techniques using a sample project with Node.js, Nginx, and Redis. You can get the sample project [here](https://github.com/prajwolamatya/debug-docker).
12+
13+
Our setup consists of:
14+
- A Node.js application (port 3000)
15+
- Nginx as a reverse proxy (port 80)
16+
- Redis for caching (port 6379)
17+
18+
## 1. Container Inspection
19+
### Viewing Running Containers
20+
First, let's check our running containers:
21+
22+
```bash
23+
docker-compose ps
24+
```
25+
26+
**Expected Output:**
27+
| Name | Command | State | Ports |
28+
|---------------------------|--------------------------------|---------|---------------------------|
29+
| debug-docker_nginx_1 | /docker-entrypoint.sh ngin ... | Up | 0.0.0.0:80->80/tcp |
30+
| debug-docker_nodejs-app_1 | docker-entrypoint.sh node ... | Up | 0.0.0.0:3000->3000/tcp |
31+
| debug-docker_redis_1 | docker-entrypoint.sh redis ... | Up | 0.0.0.0:6379->6379/tcp |
32+
33+
- **State:** Shows if container is running/stopped
34+
- **Ports:** Revesls port mappings (host:container)
35+
36+
### Inspecting Container Details
37+
For deeper inspection of a specific container:
38+
39+
```bash
40+
docker inspect debug-docker_nodejs-app_1
41+
```
42+
43+
This returns a JSON with all container details including:
44+
- Network settings
45+
- Mounts
46+
- Environment variables
47+
- IP addresses
48+
49+
**Pro Tip:** Filter specific information:
50+
51+
```bash
52+
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' debug-docker_nodejs-app_1
53+
```
54+
55+
## 2. Log Analysis
56+
### Viewing Container Logs
57+
58+
```bash
59+
docker-compose logs
60+
```
61+
62+
**Example Output (when accessing the service):**
63+
64+
```console
65+
debug-docker_nodejs-app_1 | Node.js server running on port 3000
66+
```
67+
68+
This confirms your Node.js application launched successfully
69+
70+
### Generating Request Logs
71+
Make a test request to generate logs:
72+
73+
```bash
74+
curl -v http://localhost:3000
75+
```
76+
77+
After the request, check logs again to see:
78+
79+
```console
80+
debug-docker_nodejs-app_1 | Node.js server running on port 3000
81+
debug-docker_nodejs-app_1 | GET / 200 7.001 ms - 19
82+
```
83+
84+
To view only GET requests from the last 5 minutes
85+
```bash
86+
docker logs --since 5m debug-docker_nodejs-app_1 | grep "GET"
87+
```
88+
89+
**Expected Output:**
90+
91+
```console
92+
GET / 200 7.001 ms - 19
93+
```
94+
95+
## 3. Network Troubleshooting
96+
### Checking Container Connectivity
97+
98+
Test if Nginx can reach Node.js:
99+
100+
```bash
101+
docker exec debug-docker_nginx_1 ping nodejs-app
102+
```
103+
104+
**Expected Output:**
105+
```console
106+
PING nodejs-app (172.19.0.3): 56 data bytes
107+
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.060 ms
108+
```
109+
- **Success:** <1ms response confirms network connectivity
110+
- **Failure:** Would show "unknown host" or timeout
111+
112+
### Examining Post Accessibility
113+
Check if Node.js is listening on port 3000 inside its container:
114+
115+
```bash
116+
docker exec debug-docker_nodejs-app_1 netstat -tuln
117+
```
118+
119+
**Expected Output:**
120+
```console
121+
Active Internet connections (only servers)
122+
Proto Recv-Q Send-Q Local Address Foreign Address State
123+
tcp 0 0 :::3000 :::* LISTEN
124+
```
125+
126+
- Shows Node.js listening on port 3000
127+
- No output means service isn't running properly
128+
129+
## 4. Interactive Debugging
130+
### Executing into Containers
131+
132+
For Node.js application debugging:
133+
```bash
134+
docker exec -it debug-docker_nodejs-app_1 sh
135+
```
136+
Now you can:
137+
1. Check running processes: `ps aux`
138+
```bash
139+
ps aux
140+
```
141+
142+
**Output:**
143+
144+
```console
145+
PID USER TIME COMMAND
146+
1 root 0:00 {MainThread} node app.js
147+
28 root 0:00 sh
148+
45 root 0:00 ps aux
149+
```
150+
- Shows all running processess in the container
151+
- `PID1`: Your Node.js application (node app.js)
152+
- `PID28`: The shell session you just started
153+
- `PID 45`: The `ps aux` command itself
154+
- Confirms your application is running as the main process
155+
156+
2. Examine environment variables: `printenv`
157+
```bash
158+
printenv
159+
```
160+
161+
**Output:**
162+
```console
163+
NODE_VERSION=23.11.0
164+
HOSTNAME=ada88201c429
165+
YARN_VERSION=1.22.22
166+
SHLVL=1
167+
HOME=/root
168+
TERM=xterm
169+
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
170+
PWD=/app
171+
```
172+
- Displays all environment variables to your Node.js app
173+
174+
3. Test Redis connectivity: `redis-cli -h redis ping`
175+
```bash
176+
redis-cli -h redis ping
177+
```
178+
179+
**Output:**
180+
```console
181+
PONG
182+
```
183+
184+
- Tests connectivity to your Redis container
185+
- `PONG` response confims network connectivity
186+
187+
### Debugging Nginx Configuration
188+
```bash
189+
docker exec -it debug-docker_nginx_1 nginx -t
190+
```
191+
192+
**Expected Output:**
193+
```console
194+
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
195+
nginx: configuration file /etc/nginx/nginx.conf test is successful
196+
```
197+
198+
## 5. Health Checks and Readiness Probes
199+
Let's enhance our `docker-compose.yml` with health checks:
200+
201+
```yaml
202+
services:
203+
nodejs-app:
204+
# ... existing config ...
205+
healthcheck:
206+
test: ["CMD", "curl", "-f", "http://localhost:3000"]
207+
interval: 30s
208+
timeout: 10s
209+
retries: 3
210+
211+
redis:
212+
# ... existing config ...
213+
healthcheck:
214+
test: ["CMD", "redis-cli", "ping"]
215+
interval: 30s
216+
timeout: 5s
217+
retries: 3
218+
```
219+
220+
Now check the container health:
221+
``bash
222+
docker ps --format "table {{.Names}}\t{{.Status}}"
223+
```
224+
225+
**Expected Output:**
226+
```console
227+
NAMES STATUS
228+
debug-docker_nginx_1 Up 5 minutes
229+
debug-docker_nodejs-app_1 Up 5 minutes (healthy)
230+
debug-docker_redis_1 Up 5 minutes (healthy)
231+
```
232+
233+
## 6. Temporary Debug Containers
234+
Sometimes you need additioinal tools. Create a temporary debug container in the same network:
235+
```bash
236+
docker run -it --rm --network debug-docker_default alpine sh
237+
```
238+
239+
Now from this container you can:
240+
1. Test DNS resolution:
241+
```bash
242+
nslookup nodejs-app
243+
```
244+
245+
**Expected Output:**
246+
```console
247+
Server: 127.0.0.11
248+
Address: 127.0.0.11:53
249+
250+
Non-authoritative answer:
251+
Name: nodejs-app
252+
Address: 172.19.0.3
253+
```
254+
255+
- Confirms Docker's internal DNS is working
256+
- Shows the service name resolves to the correct container IP (172.19.03)
257+
258+
2. Check connectivity:
259+
```bash
260+
wget -qO- http://nodejs-app:3000
261+
```
262+
263+
**Expected Output:**
264+
```console
265+
Hello from Node.js!
266+
```
267+
268+
- Shows successful TCP connection to port 3000
269+
- Returns the actual HTTP response from your Node.js app
270+
271+
## 7. Docker System Diagnostics
272+
When facing resource issues:
273+
```bash
274+
docker system df
275+
```
276+
277+
**Example Output:**
278+
```console
279+
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
280+
Images 17 3 2.374GB 2.295GB (96%)
281+
Containers 3 3 2B 0B (0%)
282+
Local Volumes 2 1 88B 88B (100%)
283+
Build Cache 108 0 37.82MB 37.82MB
284+
```
285+
286+
Check detailed resource usage:
287+
```bash
288+
docker stats
289+
```
290+
291+
**Example Output:**
292+
```console
293+
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
294+
5111f46d640b debug-docker_nginx_1 0.00% 9.633MiB / 31.06GiB 0.03% 41.9kB / 126B 0B / 4.1kB 13
295+
e701e4d02bb0 debug-docker_nodejs-app_1 0.00% 13.31MiB / 31.06GiB 0.04% 45.3kB / 3.49kB 0B / 0B 7
296+
3e0399cc7510 debug-docker_redis_1 0.93% 4.691MiB / 31.06GiB 0.01% 42.3kB / 126B 1.43MB / 0B 6
297+
```
298+
299+
## Conclusion
300+
Effective Docker debugging requires a systematic approach. By mastering these techniques, you'll be able to diagnose and resolve even the most complex Docker issues in production environments.
327 KB
Loading

0 commit comments

Comments
 (0)