Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

How to Debug a Running Docker Container (logs, exec, inspect)

Debug containers with ps, logs, inspect, and exec before restart. Fix the cause, then recreate and verify.

Friends, never restart first. Chala mitrano — logs, inspect, then exec.

Quick answer

Hold this vertical picture:

  1. docker ps -a for exit code.
  2. Read docker logs.
  3. inspect status and env keys.
  4. exec a shell if running.
  5. Fix, recreate, verify.

Language: English · मराठी · हिंदी

docker ps -aSee exit code docker logsRead the error docker inspectEnv mounts image docker execShell inside Fix and recreateVerify health
Container debug card

Why a debug card

A container that exits at 2 a.m. on an EC2 box needs a calm order, not random restarts.

Why: Find the error before changing state. What: ps, logs, inspect, exec. How: Practice the card on a lab container.

Example 1: List all containers

Type this:

command -v docker >/dev/null && docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Image}}" || printf "%s\n" "NAMES STATUS IMAGE\napi Exited (1) myapi:1"

You will see:

NAMES STATUS IMAGE
api Exited (1) myapi:1

What it means: Exit code 1 means the process failed — start with logs.

Example 2: Read recent logs

Type this:

printf "%s\n" "docker logs --tail 100 api"

You will see:

docker logs --tail 100 api

What it means: Tail keeps noise down; add -f to follow live.

Example 12: Debug card

Type this:

printf "%s\n" "1 ps -a 2 logs 3 inspect 4 exec 5 fix 6 verify"

You will see:

1 ps -a 2 logs 3 inspect 4 exec 5 fix 6 verify

What it means: Say this card in TCS Docker rounds.

Concept 1: Logs first

Most answers are in the last log lines.

Why: See the stack trace or missing env. What: docker logs --tail / -f / --since How: Then inspect exit code.

Example 2: Read recent logs

Type this:

printf "%s\n" "docker logs --tail 100 api"

You will see:

docker logs --tail 100 api

What it means: Tail keeps noise down; add -f to follow live.

Example 3: Follow logs mindset

Type this:

printf "%s\n" "docker logs -f --since 10m api"

You will see:

docker logs -f --since 10m api

What it means: since 10m focuses on the latest incident window.

Example 4: Inspect exit and mounts

Type this:

printf "%s\n" "docker inspect api --format '{{.State.Status}} {{.State.ExitCode}}'"

You will see:

docker inspect api --format '{{.State.Status}} {{.State.ExitCode}}'

What it means: inspect exposes status, exit code, env, and mounts.

Example 11: OOM hint

Type this:

printf "%s\n" "Exit 137 often means OOM kill" "Check memory limits next"

You will see:

Exit 137 often means OOM kill
Check memory limits next

What it means: 137 is a classic interview signal.

Concept 2: Inspect and exec

Config and a shell confirm what the process sees.

Why: Env, mounts, user, command. What: inspect formats; exec sh or one command How: Never paste secret values into tickets.

Example 5: Inspect env keys (no secret values in demos)

Type this:

printf "%s\n" "docker inspect api --format '{{range .Config.Env}}{{println .}}{{end}}' | cut -d= -f1"

You will see:

docker inspect api --format '{{range .Config.Env}}{{println .}}{{end}}' | cut -d= -f1

What it means: Print keys only in shared screens — never paste secret values into chats.

Example 6: Exec a shell

Type this:

printf "%s\n" "docker exec -it api sh"

You will see:

docker exec -it api sh

What it means: Works only while the container is running; use ps to find a live one.

Example 7: Exec one command

Type this:

printf "%s\n" "docker exec api ls /app"

You will see:

docker exec api ls /app

What it means: Non-interactive exec is great for scripts and CI debug steps.

Concept 3: Ports fix verify

Wrong publish looks like a dead app.

Why: Map host and container ports. What: docker port; ss on host How: Fix then recreate; restart last.

Example 8: Check published ports

Type this:

printf "%s\n" "docker port api" "ss -lntp | head"

You will see:

docker port api
ss -lntp | head

What it means: Mismatch between publish and listen is a common Flipkart lab bug.

Example 9: Copy files out

Type this:

printf "%s\n" "docker cp api:/app/app.log /tmp/app.log"

You will see:

docker cp api:/app/app.log /tmp/app.log

What it means: Pull logs or configs when exec is enough but you need a host file.

Example 10: Restart is last

Type this:

printf "%s\n" "Fix config or image first" "Then docker restart api or recreate"

You will see:

Fix config or image first
Then docker restart api or recreate

What it means: Blind restart loops the same crash.

Debug card

  1. docker ps -a
  2. docker logs --tail 100 NAME
  3. docker inspect (status, exit, env keys)
  4. docker exec -it NAME sh
  5. Fix image/config
  6. Recreate and verify

Suppose: Exit 1 after deploy

Logs show missing PORT env. Add env, recreate.

Suppose: Exit 137

Memory limit too low. Raise limit, verify.

Ravindra Bagale's Tip

On Amazon ECS and plain Docker EC2 at Infosys, the first three commands are logs, inspect, and exec.

Check yourself (quiz)

Q1. First command?

docker ps -a then docker logs

Q2. Exit 137?

Often OOM kill

Q3. When does exec work?

While the container is running

Q4. How to see env keys?

docker inspect format on Config.Env

Q5. Restart first?

No — logs and inspect first

Q6. Copy a file out?

docker cp container:path hostpath

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

ps → logs → inspect → exec → fix → verify. Did you understand? Let us try it once more.

Frequently asked questions

First debug step?

docker ps -a then logs.

Exit 137?

Often out-of-memory kill.

exec when stopped?

No — container must be running.

See env safely?

inspect keys; do not paste secret values.

Copy files?

docker cp

Restart first?

No — logs and inspect first.