Constih
Constih

Reputation: 165

Activate conda environment in singularity container from dockerfile

I'm trying to set up a singularity container from an existing docker image in which a conda environment named "tensorflow" is activated as soon as I run the container. I've found some answers on this topic here. Unfortunately, in this post they only explain how they would set up the the singularity .def file to activate the conda environment by default. However, I want to modify my existing Dockerfile only and then build a singularity image from it.

What I've tried so far is setting up the Dockerfile like this:

FROM opensuse/tumbleweed

ENV PATH /opt/conda/bin:$PATH
ENV PATH /opt/conda/envs/tensorflow/bin:$PATH

# Add conda environment files (.yml)
COPY ["./conda_environments/", "."]

# Install with zypper
RUN zypper install -y sudo wget bzip2 vim tree which util-linux

# Get installation file
RUN wget --quiet https://repo.anaconda.com/archive/Anaconda3-2019.07-Linux-x86_64.sh -O ~/anaconda.sh

# Install anaconda at /opt/conda
RUN /bin/bash ~/anaconda.sh -b -p "/opt/conda"

# Remove installation file
RUN rm ~/anaconda.sh

# Make conda command available to all users
RUN ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh

# Create tensorflow environment
RUN conda env create -f tensorflow.yml

# Activate conda environment with interactive bash session
RUN echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc
RUN echo "conda activate tensorflow" >> ~/.bashrc

# Default command
CMD ["/bin/bash"]

After building the docker image I run the docker container with:

docker run -t -d --rm --name=my_container opensuse_conda:latest

and enter the container with:

docker exec -it my_container bash

The result is as expected. The shell session is started directly with the "tensorflow" environment being active which is indicated by the (tensorflow) prefix.

To build a singularity image from this docker image I use:

sudo singularity build opensuse_conda.sif docker-daemon://opensuse_conda:latest

and run the container with:

sudo singularity run opensuse_conda.sif

This is where the problem occurs. Instead of the "tensorflow" environment the "base" environment is activated by default. However, I would rather have the "tensorflow" environment being activated when I run the singularity container.

How can I modify my Dockerfile so that when running both the docker container and the singularity container the default environment is "tensorflow"?

Thank you very much for your help!

Upvotes: 4

Views: 4031

Answers (1)

Roland Weber
Roland Weber

Reputation: 3675

Your problem is that .bashrc will only be read when you start an interactive shell, but not when the container is running with the default command. See this answer for background information.

There are a bunch of bash startup files where you could put the conda activate tensorflow command in instead. I recommend to define a file of your own, and put the filename into the BASH_ENV environment variable. Both can easily be done from the Dockerfile.

Upvotes: 2

Related Questions