Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Dockerfile RUN commands run in a login shell environment?

Or, in other words, do I need to run docker run -it <container> bash or docker run -it <container> bash --login to get the same environment as what they would run in?

like image 645
ivan_pozdeev Avatar asked Aug 31 '25 03:08

ivan_pozdeev


1 Answers

to get the same environment as what they would run in?

I understand "As they would run by default without specfifying a command"

That will simply depend on how your container is actually configured to run:

  • if the ENTRYPOINT or CMD is configured to run a login shell, you should use a login shell
  • if the ENTRYPOINT or CMD is configured to run a non-login shell, you should use a non-login shell

You can identify this by running docker inspect on your container or docker image inspect which will give you ENTRYPOINT and CMD

Same principle if you first run the container then create a shell using docker exec -it bash [--login]

For example, using this Dockerfile:

FROM alpine

RUN apk add bash
RUN echo "export MYVAR=frombashrc" > /root/.bashrc
RUN echo "export MYVAR=fromprofile" > /root/.bash_profile

ENTRYPOINT ["/bin/sh", "-c"]

And running:

$ docker build . -t mybashimage
$ docker run -it --name bashcontainer mybashimage "bash --login -c 'env && sleep 60'"
HOSTNAME=4aeb776a8c56
MYVAR=fromprofile
...

In another shell while container is running:

# Running a non-login shell does not have same effect
$ docker exec -it bashcontainer bash
bash-4.4# env
HOSTNAME=5f44398152bf
MYVAR=frombashrc
...

# Running login shell doe
$ docker exec -it bashcontainer bash --login -c 'env'
HOSTNAME=5f44398152bf
MYVAR=fromprofile
...

like image 141
Pierre B. Avatar answered Sep 04 '25 03:09

Pierre B.