3

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!

Constih
  • 145
  • 2
  • 8

1 Answers1

2

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.

Roland Weber
  • 3,395
  • 12
  • 26
  • Thank you very much for your answer. I still have two follow up questions: 1.) When I go for the BASH_ENV solution that you recommend and try to execute a python script from within the singularity container, do I have to run `singularity run opensuse_conda.sif bash -c "python test.py"` to ensure that the "tensorflow" environment is activated? For me this works but I wonder if I really have to use the `bash -c` command. – Constih Aug 14 '19 at 14:03
  • 2.) You said that the .bashrc file is only read when I start an interactive shell. This is true when I run `docker run -it /bin/bash`. As expected, I get an interactive session with the "tensorflow" environment being active. However, running `singularity run .sif /bin/bash` produces an interactive session without any environment being active. I first have to type `conda activate tensorflow` to end up in my custom environment. Why is that? It seems like the .bashrc file that I create in my Dockerfile is not read when starting an interactive singularity container session. – Constih Aug 14 '19 at 14:20
  • Singularity by default runs with `--norc` and a custom `$PS1` so that you are aware you are in a singularity image and not the host machine. When you use `bash -c ...` as the run command you are bypassing where it would normally use `--norc`, which is why your run environment is as you expect it. – tsnowlan Nov 05 '19 at 11:27
  • Hi @Constih, just wondering if you found a way to automatically activate the environment without using `bash -c ...`? – web Jan 30 '22 at 18:16