mfz.github.io

Replicating a conda environment in singularity

Posted at — Nov 25, 2021

How to replicate a conda environment in a singularity container, and automatically activate the environment on startup.

Given `environment.yml` file, create a Singularity definition file using

cat > MiniConda3.def <<EOF

Bootstrap: docker

From: continuumio/miniconda3

%files
    environment.yml

%post
    /opt/conda/bin/conda env create -f environment.yml
    echo ". /opt/conda/etc/profile.d/conda.sh" >> /etc/bash.bashrc
    echo "conda activate \$(head -n 1 environment.yml | cut -f 2 -d ' ')" >> /etc/bash.bashrc

%environment
    export SINGULARITY_SHELL=/bin/bash

EOF

Build the singularity container using

sudo singularity build MiniConda3.sif MiniConda3.def

Use like this

singularity shell MiniConda3.sif

Note: If running `singularity shell `, the `bash` shell is started with `–norc` flag, i.e. it does not read `/etc/bash.bashrc`. By defining `SINGULARITY_SHELL=/bin/bash` in the `%environment` section, `bash` is called as `/bin/bash` and does read rc files. This means the commands necessary to initialize conda and activate the environment can be put into the `/etc/bash.bashrc`.