Why `~/.bashrc` is not executed when run docker container?

Each command runs a separate sub-shell, so the environment variables are not preserved and .bashrc is not sourced (see this answer).

You have to source your script manually in the same process where you run your command so it would be:

CMD source /root/.bashrc && /workspace/launch.sh

provided your launch.sh is an executable.

As per documentation exec form you are using does not invoke a command shell, so it won’t work with your .bashrc.

Edit:

BASH wasn’t your default shell so

CMD /bin/bash -c "source /root/.bashrc && /workspace/launch.sh"

was needed in order to run your script.
If you want yo set your shell as BASH by default, you can use SHELL instruction as described in documentation, e.g.:

SHELL ["/bin/bash", "-c"]

Leave a Comment