Skip to content

Docker: Environment variables & container config

12 Docker commands: Environment variables & container config. Click a command to see what each part does.

Index
12 items
Click a command to see what each part does.

Environment variables & container config12

  • docker exec <container> envPrint all environment variables of a running container

    What each part does

    1. docker exec <container>run a command inside the running container
    2. envprint 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 variable

    What each part does

    1. docker exec <container>run inside the container
    2. printenv <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

    1. docker inspect <container>read the container's stored configuration; no exec needed
    2. --format '...'Go template to pick fields
    3. {{range .Config.Env}}loop over the Env list (from the image ENV, -e and --env-file)
    4. {{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 image

    What each part does

    1. docker image inspect <image>:<tag>metadata of a local image
    2. {{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 get

    What each part does

    1. docker run --rmstart a throwaway container, removed when it exits
    2. <image>:<tag>the image to test
    3. envoverride the command: just print the environment
  • docker compose exec <service> envPrint the env of a Compose service

    What each part does

    1. docker compose exec <service>run inside the running service container
    2. envprint 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 runs

    What each part does

    1. docker inspect <container>container config
    2. {{json .Config.Entrypoint}}the ENTRYPOINT (the program that always runs)
    3. {{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

    1. {{json .Mounts}}every mount: type, source on the host, destination in the container, read-only flag
    2. | jqpretty-print the JSON
  • docker inspect <container> --format '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}'Show the container's IP address(es)

    What each part does

    1. {{range .NetworkSettings.Networks}}loop over every network the container is attached to
    2. {{.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 limits

    What each part does

    1. {{.HostConfig.RestartPolicy.Name}}no, always, on-failure or unless-stopped
    2. {{.HostConfig.Memory}}memory limit in bytes (0 = unlimited)
    3. {{.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 results

    What each part does

    1. {{json .State.Health}}status (starting / healthy / unhealthy), failing streak, and the output of the last checks
    2. | jqpretty-print
  • docker inspect <container> --format '{{json .Config.Labels}}' | jqShow labels (Compose project, versions, maintainers)

    What each part does

    1. {{json .Config.Labels}}all labels as JSON, e.g. com.docker.compose.service and org.opencontainers.image.version
    2. | jqpretty-print

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…