Docker: Environment variables & container config
12 Docker commands: Environment variables & container config. Click a command to see what each part does.
- Whole sheet120
- Basics: images & containers11
- Volumes & networks4
- Find & inspect containers7
- Environment variables & container config12
- Software versions inside containers & images9
- Logs & shells5
- Run containers3
- Images4
- Cleanup6
- Docker Compose v27
- Container lifecycle & live changes10
- Builds, multi-platform & moving images11
- Volumes, networks & remote hosts in depth12
- Compose: more commands9
- Swarm mode10
Environment variables & container config12
docker exec <container> envPrint all environment variables of a running containerWhat each part does
docker exec <container>run a command inside the running containerenvprint every environment variable the container's processes see, KEY=value per line
Tip Needs a running container that has env (most images do; distroless ones don't). For a stopped container use the inspect version below.
docker exec <container> printenv <VAR>Print one environment variableWhat each part does
docker exec <container>run inside the containerprintenv <VAR>print only that variable's value; exit code 1 if it isn't set
docker inspect <container> --format '{{range .Config.Env}}{{println .}}{{end}}'Show env vars from the container config (works when stopped)What each part does
docker inspect <container>read the container's stored configuration; no exec needed--format '...'Go template to pick fields{{range .Config.Env}}loop over the Env list (from the image ENV, -e and --env-file){{println .}}print each KEY=value on its own line
Tip Shows the configured env, not variables a process exported at runtime. Secrets passed with -e are visible here: anyone with docker access can read them.
docker image inspect <image>:<tag> --format '{{json .Config.Env}}'Show env vars baked into an imageWhat each part does
docker image inspect <image>:<tag>metadata of a local image{{json .Config.Env}}the ENV values from the Dockerfile (PATH, versions...) as a JSON list
docker run --rm <image>:<tag> envPrint the env a fresh container would getWhat each part does
docker run --rmstart a throwaway container, removed when it exits<image>:<tag>the image to testenvoverride the command: just print the environment
docker compose exec <service> envPrint the env of a Compose serviceWhat each part does
docker compose exec <service>run inside the running service containerenvprint its environment, after .env and environment: substitution
Tip To check the values before starting anything, use docker compose config.
docker inspect <container> --format 'Entrypoint={{json .Config.Entrypoint}} Cmd={{json .Config.Cmd}}'Show the entrypoint and command a container runsWhat each part does
docker inspect <container>container config{{json .Config.Entrypoint}}the ENTRYPOINT (the program that always runs){{json .Config.Cmd}}the CMD (default arguments, replaced by what you type after the image name)
docker inspect <container> --format '{{json .Mounts}}' | jqShow mounts (volumes and bind mounts)What each part does
{{json .Mounts}}every mount: type, source on the host, destination in the container, read-only flag| jqpretty-print the JSON
docker inspect <container> --format '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}'Show the container's IP address(es)What each part does
{{range .NetworkSettings.Networks}}loop over every network the container is attached to{{.IPAddress}}its IP on that network
docker inspect <container> --format 'restart={{.HostConfig.RestartPolicy.Name}} memory={{.HostConfig.Memory}} cpus={{.HostConfig.NanoCpus}}'Show restart policy and resource limitsWhat each part does
{{.HostConfig.RestartPolicy.Name}}no, always, on-failure or unless-stopped{{.HostConfig.Memory}}memory limit in bytes (0 = unlimited){{.HostConfig.NanoCpus}}CPU limit in billionths of a CPU (1000000000 = 1 CPU, 0 = unlimited)
docker inspect <container> --format '{{json .State.Health}}' | jqShow the health check status and last resultsWhat each part does
{{json .State.Health}}status (starting / healthy / unhealthy), failing streak, and the output of the last checks| jqpretty-print
docker inspect <container> --format '{{json .Config.Labels}}' | jqShow labels (Compose project, versions, maintainers)What each part does
{{json .Config.Labels}}all labels as JSON, e.g. com.docker.compose.service and org.opencontainers.image.version| jqpretty-print
Nothing matches.
My DevOps Playbook by Angelo John Calleja