3

I'm trying to create a docker image with a new conda environment specified in an environment.yml file which is then automatically activated on calling docker run. I've followed the workflow from this post exactly, and am now stumped after much research. I've also done a lot of digging on stack overflow, e.g. here, but so for no luck.

Here are the contents of my dockerfile (I believe this should all be fully reproducible):

FROM continuumio/anaconda3

ADD environment.yml /tmp/environment.yml
RUN conda env create -f /tmp/environment.yml

RUN echo "conda activate $(head -1 /tmp/environment.yml | cut -d' ' -f2)" >> ~/.bashrc
ENV PATH /opt/conda/envs/$(head -1 /tmp/environment.yml | cut -d' ' -f2)/bin:$PATH

And the yaml file defining the environment:

name: pointcloudz

channels:
  - conda-forge
  - defaults

dependencies:
  - python=3.7
  - gdal
  - pdal
  - entwine

The dockerfile builds without error, but when I execute

docker run -it pdal_pipeline

I get the following error (inside the container), and the new environment is not active:

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

    $ conda init <SHELL_NAME>

I've gotten to the bottom of the internet in search of an answer, but can't find a solution. I really need the environment to be created, the packages specified in environment.yml to be installed into it, and for it to be running automatically upon running the container. Strangely, the following dockerfile in which the environment is created directly with a conda create, rather than a yaml file works exactly as expected but I have not been able to install the desired packages to the newly created environment from the dockerfile itself.

FROM continuumio/miniconda3
RUN conda create -n env python=3.6
RUN echo "source activate env" > ~/.bashrc
ENV PATH /opt/conda/envs/env/bin:$PATH

Any wisdom here would be massively appreciated.

JmeCS
  • 497
  • 4
  • 17
  • I don't see anywhere you tried the recommended action in your error message. Did you try to `RUN conda init bash` in your Dockerfile? – Zeitounator Dec 12 '19 at 13:38
  • I just tried the example and it worked fine for me. Maybe you need `--pull` on docker build to ensure `continuumio/anaconda3` is the latest image. Don't modify PATH, conda activate does that for you. (and the head command is not resolved as part of ENV, the command itself is added to PATH) – William D. Irons Dec 13 '19 at 16:14

1 Answers1

3

I'm answering my own question but only in case someone runs into the same problem. All the credit goes to @mathematicalmichael! (see the bottom of this thread)

Turns out I needed to add a CONDA_DEFAULT_ENV environment variable to the end of the dockerfile:

For completeness, here is the full contents of the dockerfile:

FROM continuumio/miniconda3

ADD environment.yml /tmp/environment.yml
RUN conda env create -f /tmp/environment.yml

RUN echo "conda activate $(head -1 /tmp/environment.yml | cut -d' ' -f2)" >> ~/.bashrc
ENV PATH /opt/conda/envs/$(head -1 /tmp/environment.yml | cut -d' ' -f2)/bin:$PATH

ENV CONDA_DEFAULT_ENV $(head -1 /tmp/environment.yml | cut -d' ' -f2)
JmeCS
  • 497
  • 4
  • 17