By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
run
exec
logs
inspect
top
stats
A hyper-practical, zero-fluff guide for engineers in production
You’re debugging a containerized app in production. The service is crashing, logs are scattered, and you don’t know if the container is even running. This guide teaches you the six Docker CLI commands that turn chaos into clarity.
docker run
docker exec
docker logs
docker inspect
docker top
htop
docker stats
Why this matters in production:- Debugging: When a container crashes, you need logs now, not after filing a Jira ticket.- Security: exec lets you audit running containers (e.g., check for exposed secrets).- Performance: stats helps you spot memory leaks before they take down your cluster.- Automation: inspect outputs JSON, which you can parse in scripts to auto-scale or auto-heal.
Real-world scenario:You inherit a legacy app running in Docker. The original dev left no docs. The app is misbehaving—high CPU, random crashes. You have 30 minutes to diagnose it before the CEO notices. These six commands are your lifeline.
--restart unless-stopped
-it
exec -it <container> sh
--tail 100 -f
jq
docker logs <container> | jq '.'
--format
docker inspect --format='{{.NetworkSettings.IPAddress}}' <container>
ps aux
--no-stream
docker stats --no-stream > stats.txt
docker --version
nginx
You’re given a container that’s supposed to serve a static site, but it’s returning 502 errors. Let’s diagnose it.
docker run -d --name my-nginx -p 8080:80 nginx
-d
--name
my-nginx
-p 8080:80
Verify it’s running:
docker ps
Expected output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abc123def456 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp my-nginx
docker logs my-nginx
Expected output (truncated):
2023/10/01 12:00:00 [notice] 1#1: using the "epoll" event method 2023/10/01 12:00:00 [notice] 1#1: nginx/1.25.1 2023/10/01 12:00:00 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
If you see errors (e.g., Permission denied), you’ve found the issue.
Permission denied
docker inspect my-nginx
This dumps a huge JSON blob. Let’s extract the IP address:
docker inspect --format='{{.NetworkSettings.IPAddress}}' my-nginx
172.17.0.2
Why this matters: If the container can’t bind to port 80, inspect will show Ports: {}.
Ports: {}
docker exec -it my-nginx sh
Now you’re inside the container. Run:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 10628 5248 ? Ss 12:00 0:00 nginx: master process nginx -g daemon off; nginx 6 0.0 0.0 11100 2624 ? S 12:00 0:00 nginx: worker process
Check for issues: - Is nginx running? If not, the container might have crashed.- Are there zombie processes? (Look for <defunct> in ps aux.)
<defunct>
Exit the shell:
exit
docker stats my-nginx
Expected output (live updating):
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS abc123def456 my-nginx 0.00% 2.5MiB / 1.944GiB 0.13% 656B / 0B 0B / 0B 2
What to look for: - High CPU (>80% sustained) → Possible infinite loop.- High memory (>90% of limit) → Memory leak.- High disk I/O → Log spam or storage issues.
>80%
>90%
docker top my-nginx
UID PID PPID C STIME TTY TIME CMD root 12345 12340 0 12:00 ? 00:00:00 nginx: master process nginx -g daemon off; 101 12346 12345 0 12:00 ? 00:00:00 nginx: worker process
Why this matters: If exec fails (e.g., the container is crashing), top still works.
--user
--user 1000:1000
--memory
--cpus
bash docker run -d --memory="512m" --cpus="1" nginx
--read-only
bash docker run -d --read-only nginx
docker system prune -a
--name api-prod
--log-opt max-size=10m --log-opt max-file=3
bash docker run -d --log-opt max-size=10m --log-opt max-file=3 nginx
--health-cmd
bash docker run -d --health-cmd="curl -f http://localhost || exit 1" nginx
--since
--until
bash docker logs --since 2023-10-01T00:00:00 --until 2023-10-01T12:00:00 my-nginx
docker stats --no-stream
bash docker inspect --format='{{.NetworkSettings.Networks}}' my-nginx
Error: No such container: <name>
docker ps -a
docker start <container>
--log-opt max-size
--log-opt max-file
USER
--format='{{.NetworkSettings}}'
docker run -d --name my-container nginx
"How do you limit a container to 512MB of memory?" Answer: docker run --memory="512m" nginx
docker run --memory="512m" nginx
docker exec vs docker run:
"What’s the difference between docker exec and docker run?" Answer: run creates a new container; exec runs a command in an existing one.
docker logs:
"How do you follow the last 100 lines of a container’s logs?" Answer: docker logs --tail 100 -f <container>
docker logs --tail 100 -f <container>
docker inspect:
"How do you get a container’s IP address?" Answer: docker inspect --format='{{.NetworkSettings.IPAddress}}' <container>
docker stats:
docker stats --no-stream <container>
docker start
start
docker attach
attach
docker events
events
A container named web-app is running, but the app is unresponsive. You suspect a memory leak. Diagnose the issue using only docker CLI commands.
web-app
docker
bash docker stats web-app
If memory usage is near the limit, it’s likely a leak.
Inspect the container’s memory limit: bash docker inspect --format='{{.HostConfig.Memory}}' web-app
bash docker inspect --format='{{.HostConfig.Memory}}' web-app
If no limit is set, the container can consume all host memory.
Jump inside the container to check processes: bash docker exec -it web-app sh
bash docker exec -it web-app sh
Run top or ps aux to spot runaway processes.
Restart the container with a memory limit: bash docker stop web-app docker rm web-app docker run -d --name web-app --memory="512m" your-image
bash docker stop web-app docker rm web-app docker run -d --name web-app --memory="512m" your-image
Why this works: - stats identifies the leak.- inspect confirms if a limit was set.- exec lets you debug interactively.- Restarting with --memory prevents future leaks.
docker run -d --name my-app -p 8080:80 nginx
docker exec -it my-app sh
docker logs --tail 100 -f my-app
docker inspect --format='{{.NetworkSettings.IPAddress}}' my-app
docker top my-app
docker stats --no-stream my-app
docker run -it
json-file
--log-opt max-size=10m
kubectl
Final Tip: Bookmark this guide. When production breaks at 3 AM, you’ll thank yourself. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.