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"]

with CMD and shell form

CMD /bin/bash -i "/workspace/launch.sh"

Edit should also work with ENTRYPOINT and and using exec form using

ENTRYPOINT ["bash","-i","/workspace/entrypoint.sh"]

I believe the -i flag works in the intended way, the .bashrc file is used as intended, the other solutions did not work for me, the .bashrc file was never used

solution may not be ideal for everyone, with the -i flag the program may prompt for user interaction

ps: I used docker create and docker start -i "container name"


You can add source /path/to/bashrc in launch.sh and change the CMD to the following instead of changing to bash through CMD itself:

CMD ["/workspace/launch.sh"]

Alternatively, You can do the following in your Dockerfile instead of depending on bashrc

ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 7.9.0
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules #Ensure that this is the actual path
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
RUN . $NVM_DIR/nvm.sh && \
  nvm install $NODE_VERSION && npm install -g [email protected]

Tags:

Docker

Shell

Bash