skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    Running the CI script with -x showed that the .bashrc is indeed sourced, so, why the last statement in it didn't work I've no idea. I ended up appending source "$HOME/.rvm/scripts/rvm to the ~/.profile (instead of .bashrc), and running the CI script with a #!/bin/bash -l shebang. That seemed to have solved the executable not found problem, although the original question I asked remains unanswered.

    -x trace:

    + '[' '' ']'
    + '[' -d /etc/profile.d ']'
    + for i in /etc/profile.d/*.sh
    + '[' -r /etc/profile.d/01-locale-fix.sh ']'
    + . /etc/profile.d/01-locale-fix.sh
    +++ /usr/bin/locale-check C.UTF-8
    ++ eval 'LANG='''C.UTF-8''''
    +++ LANG=C.UTF-8
    + unset i
    + '[' -n '5.0.17(1)-release' ']'
    + '[' -f /home/jekyll/.bashrc ']'
    + . /home/jekyll/.bashrc
    ++ case $- in
    ++ return
    + '[' -d /home/jekyll/bin ']'
    + '[' -d /home/jekyll/.local/bin ']'
    + PATH=/home/jekyll/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    + /home/jekyll/.local/bin/ci
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search