I build a Docker image based on the following Dockerfile:
ARG PYTORCH="1.6.0"
ARG CUDA="10.1"
ARG CUDNN="7"
FROM pytorch/pytorch:${PYTORCH}-cuda${CUDA}-cudnn${CUDNN}-devel
ENV TORCH_CUDA_ARCH_LIST="6.0 6.1 7.0 PTX"
ENV TORCH_NVCC_FLAGS="-Xfatbin -compress-all"
ENV CMAKE_PREFIX_PATH="$(dirname $(which conda))/../"
# SSH SERVER
RUN apt-get update && apt-get install openssh-server sudo -y
RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
RUN echo 'root:root' | chpasswd
WORKDIR /
EXPOSE 22
CMD ["service ssh start"]
I launch the Docker container with
docker run -it -d -p 7220:22 --name ssh-server-test ssh-server-image /bin/bash
If I connect to the container with docker exec -it ssh-server-test /bin/bash
or docker attach ssh-server-test
, I get the PATH
I expect:
root@9264667daf83:/# echo $PATH
/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
However, if I start an ssh server with
root@9264667daf83:/# service ssh start
* Starting OpenBSD Secure Shell server sshd [ OK ]
root@9264667daf83:/#
and I connect to the Docker container through ssh as root, then the PATH
is completely different!
root@9264667daf83:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Why? And how can I get the right PATH
variable also when I connect to the container through ssh?
CodePudding user response:
The environment variable you are asking of has been set by docker itself, using ENV
clause. Check the source code of the initial Dockerfile.
Variables that are set by ENV
exist on the build stage, run, and when you exec into a running container. More here.
But when you SSH into the container, the usual Linux path for sourcing files like ~/.bashrc
is working. But there is no PATH
with conda, nvidia, etc in these files.
As a workaround, you can patch /root/.bashrc
on the build stage with the corresponding export PATH
. For example, you can add to the Dockerfile
RUN echo 'export PATH=/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:$PATH' >> /root/.bashrc
UPD
I case if you want to use exactly the same PATH
as on the build stage, you can use
RUN echo "export PATH=${PATH}" >> /root/.bashrc