12

I am making anaconda3 environment with docker.

However it shows the error like this below.

I guess it is related with some shell problem.. but I can't fixed yet.

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

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'.

My dockerfile is here.

FROM ubuntu:18.04

RUN apt-get -y update
RUN apt-get -y install emacs wget
RUN wget https://repo.continuum.io/archive/Anaconda3-2019.07-Linux-x86_64.sh
RUN /bin/bash Anaconda3-2019.07-Linux-x86_64.sh -b -p $HOME/anaconda3
RUN echo 'export PATH=/root/anaconda3/bin:$PATH' >> /root/.bashrc 

#RUN source /root/.bashrc
RUN . /root/.bashrc
RUN /root/anaconda3/bin/conda init bash
RUN /root/anaconda3/bin/conda create -n py37 python=3.7 anaconda
RUN /root/anaconda3/bin/conda activate py37
whitebear
  • 11,200
  • 24
  • 114
  • 237

2 Answers2

6

I believe your issue may be that you are sourcing your .bashrc on a separate line from the commands that rely on it. From the Dockerfile documentation:

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

This means that your are sourcing your .bashrc in one layer (the first RUN line), then RUNning the conda command in a new layer, one which doesn't know anything about environment in the previous layer.

Try something like this:

RUN . /root/.bashrc && \
    /root/anaconda3/bin/conda init bash && \
    /root/anaconda3/bin/conda create -n py37 python=3.7 anaconda && \
    /root/anaconda3/bin/conda activate py37

By running them all on one line, you're running them in a single layer.

Kryten
  • 15,230
  • 6
  • 45
  • 68
  • Thanks for the sharing, could you tell what is the "RUN . /root/.bashrc" for ? – RandomFellow Sep 08 '22 at 19:45
  • The original error was "Your shell has not been properly configured". That configuration is done in the `.bashrc` file, so we run that first. – Kryten Sep 08 '22 at 21:24
4

You can also do this in quite a bit easier way, if you place just one command SHELL ... in your Dockerfile after creation your venv like this:

FROM continuumio/anaconda3

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY ./environment.yml .
RUN conda env create -f environment.yml
SHELL ["conda", "run", "-n", "venv", "/bin/bash", "-c"]

COPY . .

This stuff helps you if you want to use conda with the docker-compose util.

Egor Richman
  • 559
  • 3
  • 13