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.
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/<pid>/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.
Know What Each Inspection Source Tells You
There are three layers, and each answers a different question about the container.
- Recorded config:
docker inspectreturns JSON built at container creation:.Config.Env,.Config.Cmd,.Config.Entrypoint,.HostConfig(limits, restart policy),.Mountsand.NetworkSettings. - Live process state:
docker execand/procshow what is running now, including files and variables changed after start. - Runtime metrics:
docker stats,docker topanddocker logscover resource use, process tree and output. - Image vs container: use
--type containerwhen an image and a container share a name, so you do not read the wrong object.
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.Entrypointand.Config.Cmdto confirm what actually launches. - Limits and policy:
.HostConfig.Memory,.HostConfig.NanoCpusand.HostConfig.RestartPolicyexplain OOM kills and restart loops. - State:
.State.Status,.State.ExitCode,.State.OOMKilledand.State.Health.Status.
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 envshows 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/environis the environment PID 1 was started with; entries are NUL-separated, so translate them. - Check processes and ports:
docker top my-appanddocker port my-appconfirm what is running and published. - Filesystem drift:
docker diff my-applists files added (A), changed (C) or deleted (D) versus the image.
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/environinside the busybox container; this typically works when you run as the same user or root. - Stopped containers:
docker inspectstill works on exited containers, so you can read the exit code and config after a crash;docker execdoes not. - Compose projects:
docker compose configprints the resolved configuration after variable interpolation and overrides, which finds most "wrong env" bugs before the container even starts.
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
dockergroup can read every variable withdocker inspect; prefer file-based secrets (Docker/Swarm secrets, mounted files) for passwords and keys. - Redact before sharing: never paste raw
docker inspectoutput 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-appis not empty or root. - Log the evidence: capture
inspectoutput of a failing container as a pipeline artifact after scrubbing secrets.
- 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.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- 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.
- 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.
- Pasting full docker inspect output into chats or tickets It includes every environment variable, which often means database passwords and API tokens.
- 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.