1

Is it possible to use the same shell in all RUN commands when building a docker image? As opposed to each RUN command running on its own shell.

Use case: at some point, I need to source some file containing environment variables that are used later on. I cannot do this, because the commands run in different shells:

RUN source something.sh
RUN ./install.sh
RUN ... more commands

Instead I have to do:

RUN source something.sh && \
    ./install.sh && \
    ... more commands

Which I'm trying to avoid since it hurts readability, it's error prone and does not allow inserting comments in between commands.

Any ideas?

Thanks!

user1011113
  • 1,114
  • 8
  • 27
  • Can you avoid needing the `something.sh` script entirely, and embed its environment-variable settings in `ENV` directives? One simple case of this is [Activate python virtualenv in Dockerfile](https://stackoverflow.com/questions/48561981/activate-python-virtualenv-in-dockerfile). – David Maze Mar 28 '22 at 10:35
  • Sure, I could recreate `something.sh` in terms of Docker build commands, but that file comes from a third-party so every time I get an update I'd need to analyze it again and apply the changes to the Dockerfile. Not very easy to maintain. – user1011113 Mar 28 '22 at 13:25

2 Answers2

1

It's not possible to have separate RUN statement run in the same shell.

If you don't like the look of concatenated commands, you could write a shell script and RUN that.

You'll have to get it into the container by using a COPY statement. Or you can use wget or curl to fetch it and pipe it into a shell. That requires that wget or curl is present in the container, so you might have to install them first.

If you use curl and Debian, it could look like this

RUN apt update && \
    apt install -y curl && \
    curl -sL https://github.com/link/to/my/install-script.sh | bash

If you COPY it in, it'd look like this

COPY install-script.sh .
RUN ./install-script.sh
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
0

I'm not sure this will run every command in the same shell, but it seems to persist changes to env vars, including PATH. It allowed me to stop having to call source many times in my file to pick up newly installed PATH binaries. SHELL ["/bin/bash", "-lc"] Specifically the l flag.

Dakota Hipp
  • 749
  • 7
  • 16