Search code examples
condahpcsingularity-container

Containerize a conda environment in a Singularity container


I've come across several instances where it would be really helpful to containerize a conda environment for long-term reproducibility. As I'm normally running in high-performance computing systems, they need to be Singularity containers for security reasons. How can this be done?


Solution

  • First, you'll want to get the environment YML for your particular conda environment.

    conda activate your_env
    conda env export > environment.yml
    

    Here's an example Singularity recipe (in file named 'Singularity' in same directory as 'environment.yml'):

    Bootstrap: docker
    
    From: continuumio/miniconda3
    
    %files
        environment.yml
    
    %post
        /opt/conda/bin/conda env create -f environment.yml
    
    %runscript
        exec /opt/conda/envs/$(head -n 1 environment.yml | cut -f 2 -d ' ')/bin/"$@"
    

    Build this with

    sudo singularity build conda.sif Singularity
    

    Now, you'll have a functioning container using libraries from your conda environment that can be run anywhere you have Singularity installed!

    Example:

    singularity run conda.sif ipython
    

    Notes:

    Depending on the version of Singularity you're using, you may need to alter $(head -n 1 environment.yml | cut -f 2 -d ' ') with the name of your environment.

    Since you can't activate the environment from the runscript, you'll be restricted to binaries installed in your particular environment with the provided runscript.