Home > Mobile >  Why dockerfile ENV cannot override $PATH for non-root user?
Why dockerfile ENV cannot override $PATH for non-root user?

Time:09-09

I want to install miniconda in ubuntu docker image for a non-root user. So far the installation part is successful but I cannot add the path of conda to the environment variable $PATH of that user. I tried to achieve that with docker ENV. Here is my Dockerfile

FROM ubuntu:20.04

RUN apt-get update && apt-get install -y openssh-server sudo

RUN useradd -rm -d /home/devp -s /bin/bash -g root -G sudo -u 1000 devp && \
 echo 'devp:devp11' | chpasswd && \
 adduser devp sudo

WORKDIR /home/devp

USER devp
RUN wget \
    https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && mkdir ~/.conda \
    && bash Miniconda3-latest-Linux-x86_64.sh -b \
    && rm -f Miniconda3-latest-Linux-x86_64.sh 
ENV PATH=/home/devp/miniconda3/bin:${PATH}
ARG PATH=/home/devp/miniconda3/bin:${PATH}
#RUN conda env create --quiet -n env1 --file env1.yml

USER root

RUN mkdir /var/run/sshd
RUN service ssh start
EXPOSE 22

CMD ["/usr/sbin/sshd","-D"]

I ran docker with docker run --name img -p 10022:22 -d img:tag

For my root user, $PATH is overridden

/home/devp/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

For my non-root user, $PATH is not overridden when I ssh [email protected] -p 10022 'echo $PATH'

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

I have 2 questions:

  1. How can I override $PATH for non-root user?
  2. Why ENV does not work for non-root user?

CodePudding user response:

The difference here stems not from root vs. non-root user but from how the process is started, i.e., directly by docker vs. via ssh login.

With the ENV directive you specify environment variables which will be set by docker for processes started inside the container. But when logging in via ssh the new shell presented to you is a login shell started by the ssh daemon.

But ssh clears and resets most of the environment variables before starting the login shell. Additionally the login shell usually reads some config files to setup it's own environment and PATH is almost always one of the configured environment variables.

So to change PATH for shells started via ssh you must configure this in the sshd_config or via the user's login shell configs, e.g., /etc/profile, ~/.bashrc etc.

See also https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell


But as David Maze said in the comment: using a sshd inside docker container is discouraged. Containers are not VMs and are instead meant to only run a single process/application.

So depending on your use case there is probably a cleaner solution using the containers the docker way!

  • Related