Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sourcing .bashrc in Docker via ENTRYPOINT

I have created an image with a bash script called by ENTRYPOINT that itself launches an executable from a conda environment. I'm building this from a single layer directly (for now) which I realize is not best practice, but let's ignore that for a hot second...

Dockerfile

 FROM alexholehouse/seq_demo:demo_early
 SHELL ["/bin/bash", "-c"]
 ENTRYPOINT ["/seq_demo/launcher/launcher.sh"]

Where launcher.sh is

#!/bin/bash

# source bashrc which includes conda init section (and works fine in an interactive terminal)
source /root/.bashrc

# activate the conda environment
conda activate custom_conda

if [ -d /mount ]
then
    cd /mount

    # run the executable from the conda environment
    demo_seq -k KEYFILE.kf
else
    echo "No storage mounted..."
fi

Now the problem is that when I build the image using the above Dockerfile, the .bashrc file doesn't get sourced because of the following (standard) line at the top of .bashrc.

[ -z "$PS1" ] && return

... <bashrc stuff>

__conda_setup="$('/root/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/root/miniconda3/etc/profile.d/conda.sh" ]; then
    . "/root/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/root/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup

So running the image using

docker run -it -v $(pwd):/mount b29c47a060

Means .bashrc is not sourced and the launcher.sh fails because conda can't be found.

If, on the other hand, I edit .bashrc so all the conda stuff is above the [ -z "$PS1" ] && return line then (a) conda gets sourced and (b) the rest of the .bashrc is read too.

Now, editing .bashrc solves my issue but this cannot be the right way to do this! So, what's the correct way to set up an image/Dockerfile so:

(a) A specific bash script gets run upon running the container and (b) That bash script sources the .bashrc

I feel like I'm just missing something super obvious here...

like image 234
Alex Avatar asked Nov 07 '22 09:11

Alex


1 Answers

$PS1 is containing your command prompt (symbol/ e.g. '#: ').
Example of changing prompt

So you have to figure out why PS1 isnt set in the first place. Because [ -z "$PS1" ] && return will exit the script only when $PS1 is not set at all.

When the baseimage youre using doesnt provide any you might just add on in your Dockerfile via ENV PS1.

In case you never ever login into your container to use command line you might just drop that check.

See here for more information about how PS1 is propagated in bash.

like image 189
knecht_rootrecht Avatar answered Nov 11 '22 04:11

knecht_rootrecht