A variable is set for docker build or compose, but the running container has it empty
echo $MY_VAR prints an empty line inside the containerShort answer
Build arguments and runtime variables are different things. An ARG exists only while the image builds, an ENV is stored in the image, and a value you want at run time must come from ENV, docker run -e, or the environment and env_file keys in Compose. Check what the container really has first, then fix the one that is missing.
Fix it fast
1Find out which cause you have
Print what the running container actually has. This separates 'never passed' from 'passed but read wrongly'.
docker exec <container> env docker inspect --format='{{json .Config}}' <container>2Match what you see, then jump to its fix
- 1
The Dockerfile has ARG MY_VAR and you pass --build-arg, but env inside the container lacks MY_VAR.The value was given as ARG, which is gone at run timeFix - 2
ARG VERSION sits above FROM, and a RUN or ENV below it sees an empty value.An ARG declared before FROM is not visible in the stageFix - 3
docker compose up starts the service, and docker compose exec <service> env lacks the key.Compose does not pass the variable to the serviceFix - 4
The variable exists in env, but CMD ["echo", "$MY_VAR"] prints the text $MY_VAR.The exec form does not expand the variableFix
- 1
What does the error look like?
echo $MY_VAR prints an empty line inside the containerprocess.env.MY_VAR is undefined in the running app
Cause 1 of 4
The value was given as ARG, which is gone at run time
1Check
Build arguments are not present in containers unless the Dockerfile explicitly passes them into the image. ENV values, in contrast, persist in containers created from the image.
grep -n -E "^(ARG|ENV)" Dockerfile2Fix
Copy the argument into an ENV in the same stage, or pass the value when you run the container.
ARG MY_VAR=default
ENV MY_VAR=$MY_VAR
# or at run time
docker run -e MY_VAR=value my-image3Verify
Start a new container from the rebuilt image and print the variable.
docker run --rm my-image printenv MY_VARConfirmed by docs.docker.com
Cause 2 of 4
An ARG declared before FROM is not visible in the stage
1Check
Global ARGs declared before FROM are not automatically inherited by a build stage. To use one inside a stage you consume it there with ARG NAME.
grep -n -E "^(ARG|FROM)" Dockerfile2Fix
Redeclare the argument after FROM in every stage that needs it.
ARG NODE_VERSION=22
FROM node:${NODE_VERSION}
ARG NODE_VERSION
ENV NODE_VERSION=$NODE_VERSION3Verify
Rebuild and print the value inside the new container.
docker run --rm my-image printenv NODE_VERSIONConfirmed by docs.docker.com
Cause 3 of 4
Compose does not pass the variable to the service
1Check
Compose sets container variables with the environment attribute or an env_file. Listing only a name, such as - DEBUG, takes the value from your shell, and no warning is issued when it is not set there.
docker compose config2Fix
Give the variable a value in the file, or load it from an env file whose path is relative to compose.yaml.
services:
webapp:
environment:
DEBUG: "true"
env_file:
- webapp.env3Verify
Recreate the service and print the variable.
docker compose up -d --force-recreate webapp
docker compose exec webapp printenv DEBUGConfirmed by docs.docker.com
Cause 4 of 4
The exec form does not expand the variable
1Check
The exec form does not invoke a command shell, so variable substitution does not happen. The shell form runs through /bin/sh -c.
grep -n -E "^(CMD|ENTRYPOINT|RUN)" Dockerfile2Fix
Use the shell form, or call a shell yourself in the exec form.
CMD echo "$MY_VAR"
# or
CMD ["sh", "-c", "echo \"$MY_VAR\""]3Verify
Run the container and read the output.
docker run --rm my-imageConfirmed by docs.docker.com
How do I stop it coming back?
- Decide for each value whether it is needed at build time (ARG) or run time (ENV, -e, Compose), and name it that way in the file.
- Do not pass secrets as build arguments or ENV; the Docker docs say they are exposed in the final image. Use secret mounts.
- Add docker compose config to your checks, so you see the final values before you start anything.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
apk cannot find py-pip in an Alpine Docker image
py-pip (no such package):
On current Alpine releases there is no package called py-pip.
2 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
AH00558: Apache in Docker cannot reliably determine its fully qualified domain name
Set the 'ServerName' directive globally to suppress this message
AH00558 is a warning, not a crash.
2 causes, each with a fixServers
Apache in a Docker container logs AH01114 when it proxies to a backend
AH01114: HTTP: failed to make connection to backend: api
Apache's proxy worker could not open a connection to the address in ProxyPass.
5 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fix