title: Activate and switch Anaconda environment in Dockerfile during build
body: <p>I've been trying for hours and can't figure out how to activate and switch anaconda environments in a Dockerfile during the build process</p>
  
  <p>Here's the initial code:</p>
  
  <pre><code>FROM nvidia/cuda:10.1-cudnn7-devel-ubuntu16.04
  
  # Set user
  ENV SETUSER myuser
  
  RUN useradd -m $SETUSER
  USER $SETUSER
  WORKDIR /home/$SETUSER
  
  # Install Anaconda
  RUN wget https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh
  RUN bash Anaconda3-2019.03-Linux-x86_64.sh -b
  RUN rm Anaconda3-2019.03-Linux-x86_64.sh
  
  # Set path to conda
  ENV CONDA_ENV_NAME mynewenv
  RUN /home/$SETUSER/anaconda3/condabin/conda create -q --name $CONDA_ENV_NAME python=3.6 &amp;&amp; \
      /home/$SETUSER/anaconda3/condabin/conda clean --yes --all
  RUN /home/$SETUSER/anaconda3/condabin/conda activate base #Just for testing anaconda environment
  </code></pre>
  
  <p>At first, anaconda in Docker will complain that the shell is not setup properly, so after the conda create command I added:</p>
  
  <pre><code>RUN /home/$SETUSER/anaconda3/condabin/conda init bash
  RUN /bin/bash -c "source /home/$SETUSER/.bashrc"
  RUN /home/$SETUSER/anaconda3/condabin/conda activate base
  </code></pre>
  
  <p>Running the 3 commands after building the docker image works (i.e. running interactively after calling docker run container-name), but for some reason it does not work when <em>building</em> the container. I figured out that the $PATH variable was not being updated during the build, so comparing my $PATH when building and after building.</p>
  
  <pre><code>ENV PATH /home/$SETUSER/anaconda3/envs/$CONDA_ENV_NAME/bin:$PATH
  ENV PATH /home/$SETUSER/anaconda3/condabin:$PATH
  ENV PATH /home/$SETUSER/anaconda3/bin:$PATH
  RUN conda init bash
  RUN /bin/bash -c "source /home/$SETUSER/.bashrc"
  RUN conda activate base
  </code></pre>
  
  <p>Now, the Docker $PATH when building and the $PATH when modified interactively when running the container after-building is the same, but I'm still getting the shell not properly setup error.</p>
  
  <p><em>CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
  To initialize your shell, run
      $ conda init 
  Currently supported shells are:
    - bash
    - fish
    - tcsh
    - xonsh
    - zsh
    - powershell
  See 'conda init --help' for more information and options.
  IMPORTANT: You may need to close and restart your shell after running 'conda init'.</em></p>
  
  <p>Why is this not working???</p>
  
  <p>I've seen that there may be workaround using a miniconda docker template, but I <em>cannot</em> use that. How do I create and switch anaconda environment during the Docker building process? Thanks!</p>