The source command does nothing in a Dockerfile RUN step
RUN source setup.sh fails during docker buildShort answer
RUN source fails because the shell form of RUN uses /bin/sh -c on Linux by default, and source is a bash feature. Switch the shell with SHELL, or call bash explicitly. Variables set by sourcing a file do not carry to later instructions, so run source and the command in one RUN, or use ENV.
Fix it fast
1Find out which cause you have
Check which shell your build uses and whether the base image even has bash.
docker run --rm my-base-image sh -c 'ls -l /bin/bash /bin/sh'2Match what you see, then jump to its fix
- 1
The build step with source exits with an error right away, on an image where bash is installedThe default RUN shell is /bin/sh, not bashFix - 2
The source step succeeds but the value is empty in the next RUN, or when the container runsVariables set in one RUN are expected in a later instructionFix - 3
The Dockerfile has a JSON-array RUN such as RUN ["source", "env.sh"]RUN is written in exec form, which uses no shellFix
- 1
What does the error look like?
RUN source setup.sh fails during docker buildEnvironment variables loaded with source are missing in the next RUN or in the running containerUsing the RUN instruction in a Dockerfile with 'source' does not work
Cause 1 of 3
The default RUN shell is /bin/sh, not bash
1Check
On Linux the default shell is ["/bin/sh", "-c"]. The Dockerfile reference says the SHELL instruction changes the shell used by the shell form of RUN, CMD and ENTRYPOINT.
grep -n -E '^(SHELL|RUN|CMD|ENTRYPOINT)' Dockerfile2Fix
Put SHELL with bash before the step. Make sure the image really contains /bin/bash (the first-step command shows it); if not, install bash in the image first.
FROM ubuntu:24.04
SHELL ["/bin/bash", "-c"]
RUN source /opt/app/env.sh && echo "$APP_MODE"3Verify
Rebuild without cache and read the step output.
docker build --no-cache --progress=plain .Confirmed by docs.docker.com
Cause 2 of 3
Variables set in one RUN are expected in a later instruction
1Check
Each RUN is its own command in a new layer, so a shell that sourced a file is gone afterwards. The Dockerfile reference says only variables set with ENV persist into containers started from the image.
grep -n -E 'source|\. /' Dockerfile2Fix
Run source and the command that needs its variables in the same RUN. If a value must be available at run time, define it with ENV instead of sourcing a file.
RUN source /opt/app/env.sh && ./build.sh
ENV APP_MODE=production3Verify
Print the variable in a container made from the new image.
docker run --rm my-image printenv APP_MODEConfirmed by docs.docker.com
Cause 3 of 3
RUN is written in exec form, which uses no shell
1Check
The reference says the shell form automatically uses a command shell and the exec form does not. source is not a program on disk, so a RUN in exec form cannot run it directly.
grep -n 'RUN \[' Dockerfile2Fix
Use the shell form, or call the shell yourself in the exec form.
RUN ["/bin/bash", "-c", "source /opt/app/env.sh && ./build.sh"]3Verify
The step finishes and the build completes.
docker build --progress=plain .Confirmed by docs.docker.com
How do I stop it coming back?
- Declare SHELL once near the top of the Dockerfile when a stage needs bash features.
- Check that the image has /bin/bash before you point SHELL at it.
- Keep run-time settings in ENV, not in files that must be sourced.
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