0

I am trying to build a docker image with a conda environment and start the environment when I start a container, but I cannot figure out how to. My Dockerfile is currently:

FROM nvidia/cuda:10.2-cudnn7-runtime-ubuntu18.04
ENV PATH="/root/miniconda3/bin:${PATH}"
ARG PATH="/root/miniconda3/bin:${PATH}"

RUN apt update \
    && apt install -y htop python3-dev wget git imagemagick

RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && mkdir root/.conda \
    && sh Miniconda3-latest-Linux-x86_64.sh -b \
    && rm -f Miniconda3-latest-Linux-x86_64.sh \
    && conda create -y -n .venv python=3.7

RUN /bin/bash -c "source activate .venv \
    && pip install -r requirements.txt"

# More omitted installs

CMD ["/bin/bash", "source activate .venv"]
RUN /bin/bash -c "source activate .venv"

And then I build and run with:

docker build -f Dockerfile -t adlr .
docker run -it adlr /bin/bash

-->The conda environment is not being activated upon starting the container, but I would like it to be.

a-doering
  • 1,149
  • 10
  • 21
  • 1
    In the linked question I'm partial to [this answer](https://stackoverflow.com/a/64911137) that uses an entrypoint wrapper to run `conda activate` and then run the image's `CMD`, but there are several alternatives listed there (including the one you landed on of updating the `.bashrc` and then setting the default `SHELL` to actually read it). – David Maze Jan 12 '22 at 16:06

1 Answers1

0

You can activate the environment upon starting the container by replacing the last line of the Dockerfile with:

RUN echo "source activate .venv" >> ~/.bashrc
a-doering
  • 1,149
  • 10
  • 21