bashdockershelldocker-compose

Sourcing ~/.bashrc doesn't source included script


I've a Docker image that installs rvm. In the Dockerfile, I append the following lines to the ~/.bashrc of the user.

export PATH="$PATH:$HOME/.rvm/bin"
source "$HOME/.rvm/scripts/rvm"

I also make bash act as the login shell.

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

Later, I create a container using Docker Compose, and run a shell script as the user in question.

entrypoint:
  - /bin/bash
  - '-lc'
command:
  - ~/.local/bin/ci

In the script ci, if I do source ~/.bashrc, the script can't find the rvm executable. However, if I specifically source "$HOME/.rvm/scripts/rvm", script runs successfully.

My question is, the .rvm/scripts is part of the .bashrc, so, why does sourcing .bashrc doesn't work?


Solution

  • You didn’t post your full .bashrc, but from your bash -x output it’s clear that your .bashrc is checking for an interactive session with code like

    case $- in
        *i*) ;;
          *) return;;
    esac
    

    This is a standard check which prevents .bashrc from taking effect in non-interactive shells. The sourced .bashrc returns before it executes any further commands (including the ones you added).

    To avoid this, you could add your code above these lines so it will be executed even in a non-interactive context. However, a better solution would be to explicitly source a new file instead to avoid fiddling with .bashrc and friends.