Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Set export PATH in Dockerfile

I have to set export PATH=~/.local/bin:$PATH. As long I do it via docker exec -it <container> bash manually it works. However, I tried to automate it in my .dockerfile with:

FROM jupyter/scipy-notebook
RUN conda install --yes -c conda-forge fbprophet
ENV PATH="~/.local/bin:${PATH}"
RUN pip install awscli --upgrade --user

And it seems like ENV PATH="~/.local/bin:${PATH}" is not having the same effect as I receive that WARNING here. Do you see what I am doing wrong?

WARNING: The scripts pyrsa-decrypt, pyrsa-decrypt-bigfile, pyrsa-encrypt, pyrsa-encrypt-bigfile, pyrsa-keygen, pyrsa-priv2pub, pyrsa-sign and pyrsa-verify are installed in '/home/jovyan/.local/bin' which is not on PATH.
like image 227
Joey Coder Avatar asked Dec 23 '22 22:12

Joey Coder


2 Answers

Make use of ENV directive in dockerfile

ENV PATH "$PATH:/home/jovyan/.local/bin"

Hope this helps.

like image 125
mchawre Avatar answered Jan 06 '23 14:01

mchawre


$PATH is a list of actual directories, e.g., /bin:/usr/bin:/usr/local/bin:/home/dmaze/bin. No expansion ever happens while you're reading $PATH; if it contains ~/bin, it looks for a directory named exactly ~, like you might create with a shell mkdir \~ command.

When you set $PATH in a shell, PATH="~/bin:$PATH", first your local shell expands ~ to your home directory and then sets the environment variable. Docker does not expand ~ to anything, so you wind up with a $PATH variable containing a literal ~.

The best practice here is actually to avoid needing to set $PATH at all. A Docker image is an isolated filesystem space, so you can install things into the "system" directories and not worry about confusing things maintained by the package manager or things installed by other users; the Dockerfile is the only thing that will install anything.

RUN pip install awscli
# without --user

But if you must set it, you need to use a Dockerfile ENV directive, and you need to specify absolute paths. ($HOME does seem to be well-defined but since Docker containers aren't usually multi-user, "home directory" isn't usually a concept.)

ENV PATH="$HOME/.local/bin:$PATH"

(In a Dockerfile, Docker will replace $VARIABLE, ${VARIABLE}, ${VARIABLE:-default}, and ${VARIABLE:+isset}, but it doesn't do any other shell expansion; path expansion of ~ isn't supported but variable expansion of $HOME is.)

like image 45
David Maze Avatar answered Jan 06 '23 13:01

David Maze