⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 19 of 159 in Docker & Containers
Senior DevOps / SRE [L2] Docker Production Scenario [L2]
🎯 Target Role / Context: Docker Scenario #19 (L2) · Docker

Q: How do you inspect a running container's environment variables and configuration?

To inspect a running container, use docker inspect for the configuration Docker recorded (env, mounts, ports, limits) and docker exec or /proc/[pid]/environ for what the process actually sees.

#Docker #L2 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
"To <strong>inspect a running container</strong>, use <code>docker inspect</code> for the configuration Docker recorded (env, mounts, ports, limits) and <code>docker exec</code> or <code>/proc/&lt;pid&gt;/environ</code> for what the process actually sees. These are different sources of truth, and interviewers ask this question to see whether you know the difference. Config drift, a missing variable or a wrong entrypoint is usually found in minutes if you read the right layer: image metadata, container creation settings, or live process state. A senior engineer also knows that environment variables are readable by anyone with Docker socket access."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Direct Answer: To <strong>inspect a running container</strong>, use <code>docker inspect</code> for the configuration Docker recorded (env, mounts, ports, limits) and <code>docker exec</code> or <code>/proc/&lt;pid&gt;/environ</code> for what the process actually sees. These are different sources of truth, and interviewers ask this question to see whether you know the difference. Config drift, a missing variable or a wrong entrypoint is usually found in minutes if you read the right layer: image metadata, container creation settings, or live process state. A senior engineer also knows that environment variables are readable by anyone with Docker socket access.

1️⃣

Know What Each Inspection Source Tells You

There are three layers, and each answers a different question about the container.

  • Recorded config: docker inspect returns JSON built at container creation: .Config.Env, .Config.Cmd, .Config.Entrypoint, .HostConfig (limits, restart policy), .Mounts and .NetworkSettings.
  • Live process state: docker exec and /proc show what is running now, including files and variables changed after start.
  • Runtime metrics: docker stats, docker top and docker logs cover resource use, process tree and output.
  • Image vs container: use --type container when an image and a container share a name, so you do not read the wrong object.
Pro Tip: <code>.Config.Env</code> is the environment Docker <em>set</em>, not necessarily what the app is using now. If the app reads a config file or mutates its own environment, only live inspection will show it.
2️⃣

Query Config With docker inspect and Go Templates

Filter the JSON instead of scrolling a 200-line dump.

# Environment variables, one per line
docker inspect --format '{{range .Config.Env}}{{println .}}{{end}}' my-app

# Same with jq
docker inspect my-app | jq -r '.[0].Config.Env[]'

# Entrypoint, command, mounts, network
docker inspect -f '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' my-app
docker inspect -f '{{json .Mounts}}' my-app | jq .
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-app

# Why did it die or restart?
docker inspect -f 'status={{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}}' my-app
  • Environment variables: print one per line with a range template, or pipe to jq.
  • Startup command: compare .Config.Entrypoint and .Config.Cmd to confirm what actually launches.
  • Limits and policy: .HostConfig.Memory, .HostConfig.NanoCpus and .HostConfig.RestartPolicy explain OOM kills and restart loops.
  • State: .State.Status, .State.ExitCode, .State.OOMKilled and .State.Health.Status.
Advertisement
3️⃣

Verify the Live Process Environment

Confirm what the running process actually received, not only what was configured.

docker exec my-app env | sort
docker exec my-app sh -c "tr '\0' '\n' < /proc/1/environ"
docker top my-app
docker port my-app
docker diff my-app | head
docker stats --no-stream my-app
  • Exec a shell command: docker exec my-app env shows the environment of a new process in the container, which matches the configured env but not variables the app changed internally.
  • Read PID 1 directly: /proc/1/environ is the environment PID 1 was started with; entries are NUL-separated, so translate them.
  • Check processes and ports: docker top my-app and docker port my-app confirm what is running and published.
  • Filesystem drift: docker diff my-app lists files added (A), changed (C) or deleted (D) versus the image.
4️⃣

Handle Images With No Shell and Other Edge Cases

Minimal images and stopped containers need a different approach.

  • No shell in the image: distroless and scratch images fail on docker exec ... sh. Attach a debug container that shares the target's namespaces: docker run --rm -it --pid=container:my-app --net=container:my-app busybox sh.
  • Process env from the sidecar: with a shared PID namespace, run cat /proc/1/environ inside the busybox container; this typically works when you run as the same user or root.
  • Stopped containers: docker inspect still works on exited containers, so you can read the exit code and config after a crash; docker exec does not.
  • Compose projects: docker compose config prints the resolved configuration after variable interpolation and overrides, which finds most "wrong env" bugs before the container even starts.
Pro Tip: Kubernetes equivalent: <code>kubectl exec</code> for env and <code>kubectl debug -it &lt;pod&gt; --image=busybox --target=&lt;container&gt;</code> for shell-less images.
5️⃣

Treat Inspect Output as Sensitive and Automate Checks

Environment variables are plaintext to anyone who can reach the Docker daemon.

  • Secret exposure: any user in the docker group can read every variable with docker inspect; prefer file-based secrets (Docker/Swarm secrets, mounted files) for passwords and keys.
  • Redact before sharing: never paste raw docker inspect output into tickets; filter to the fields you need.
  • Drift checks in CI: a smoke-test step can assert expected values, for example docker inspect -f '{{.Config.User}}' my-app is not empty or root.
  • Log the evidence: capture inspect output of a failing container as a pipeline artifact after scrubbing secrets.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Read <code>docker inspect</code> for what Docker configured and <code>/proc/1/environ</code> for what the process started with; when they disagree, the bug is usually an entrypoint script rewriting the environment."
⚡ 60-Second Elevator Pitch Talking Points
  • I use docker inspect with Go-template filters to read the recorded env, entrypoint, mounts and limits, and docker exec or /proc/1/environ to see the live process.
  • For shell-less images I attach a busybox container sharing the target's PID and network namespaces, and docker diff shows filesystem drift.
  • I treat inspect output as sensitive, keep secrets out of env vars, and add small inspect-based assertions to CI smoke tests.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Assuming .Config.Env equals the running process environment An entrypoint script can export, unset or rewrite variables, so the process may see different values than Docker recorded.
  2. Running docker exec sh on a distroless or scratch image There is no shell binary, so it fails; use a namespace-sharing debug container instead.
  3. Pasting full docker inspect output into chats or tickets It includes every environment variable, which often means database passwords and API tokens.
  4. Inspecting the image when you meant the container Image metadata shows build-time defaults and misses run-time overrides such as -e flags and --env-file.
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →