Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

The source command does nothing in a Dockerfile RUN step

ErrorRUN source setup.sh fails during docker build

Short 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.

  • Applies to Docker Engine 27, BuildKit, Dockerfile syntax 1.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3The Dockerfile has a JSON-array RUN such as RUN ["source", "env.sh"]RUN is written in exec form, which uses no shellFix

What does the error look like?

  • RUN source setup.sh fails during docker build
  • Environment variables loaded with source are missing in the next RUN or in the running container
  • Using 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)' Dockerfile

2Fix

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

Did this fix it?

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|\. /' Dockerfile

2Fix

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=production

3Verify

Print the variable in a container made from the new image.

docker run --rm my-image printenv APP_MODE

Confirmed by docs.docker.com

Did this fix it?

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 \[' Dockerfile

2Fix

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.