Why does PATH differ when I connect to my Docker container with ssh or with exec/attach?
I build a Docker image based on the following Dockerfile:
ARG PYTORCH=&quot;1.6.0&quot;
ARG CUDA=&quot;10.1&quot;
ARG CUDNN=&quot;7&quot;

FROM pytorch/pytorch:${PYTORCH}-cuda${CUDA}-cudnn${CUDNN}-devel

ENV TORCH_CUDA_ARCH_LIST=&quot;6.0 6.1 7.0+PTX&quot;
ENV TORCH_NVCC_FLAGS=&quot;-Xfatbin -compress-all&quot;
ENV CMAKE_PREFIX_PATH=&quot;$(dirname $(which conda))/../&quot;

# SSH SERVER
RUN apt-get update &amp;&amp; apt-get install openssh-server sudo -y
RUN echo &quot;PermitRootLogin yes&quot; &gt;&gt; /etc/ssh/sshd_config
RUN echo 'root:root' | chpasswd

WORKDIR /

EXPOSE 22
CMD [&quot;service ssh start&quot;]

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?

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' &gt;&gt; /root/.bashrc

UPD
I case if you want to use exactly the same PATH as on the build stage, you can use
RUN echo &quot;export PATH=${PATH}&quot; &gt;&gt; /root/.bashrc

