Consider the following dockerfile.
FROM phusion/baseimage:jammy-1.0.4
#Create user and add to sudo, then add home to path
RUN useradd u1
RUN adduser u1 sudo
RUN mkdir /home/u1
RUN chown -R u1:u1 /home/u1
RUN apt-get update
RUN apt-get install -y sudo
ENV PATH=$PATH:/home/u1
#Create a little script and add it to path
RUN echo "echo Hello!" > /home/u1/t1.sh
RUN chown u1:u1 /home/u1/t1.sh
RUN chmod 700 /home/u1/t1.sh
#Check if script in path then run script - works fine
RUN echo $PATH
RUN t1.sh
#Same. This time script shows in path but does NOT work
RUN sudo -H -u u1 echo $PATH
RUN sudo -H -u u1 t1.sh
If you try to docker build this. The script will work just fine the first time. However, the second time – when I print the $PATH
to screen as user u1 I see the folder containing the script in the path (this $PATH
is same as when I echo it as root) – but the script behaves as if it is not in PATH
. This seems inconsistent. Output something like this:
#14 [11/14] RUN echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/u1
#14 0.393 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/u1
#14 DONE 0.4s
#15 [12/14] RUN t1.sh
#15 0.478 Hello!
#15 DONE 0.5s
#16 [13/14] RUN sudo -H -u u1 echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/u1
#16 0.330 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/u1
#16 DONE 0.3s
#17 [14/14] RUN sudo -H -u u1 t1.sh
#17 0.364 sudo: t1.sh: command not found
#17 ERROR: process "/bin/sh -c sudo -H -u u1 t1.sh" did not complete successfully: exit code: 1
------
> [14/14] RUN sudo -H -u u1 t1.sh:
0.364 sudo: t1.sh: command not found