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
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 appendingsource "$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: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
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.