How to start a conda/mamba environment in a container during execution

420 views Asked by At

I would like to do exec on my container and execute my command:

apptainer exec myContainer.sif myCommand

(or singularity)

but myCommand is within a conda/mamba environment, so I am doing:

apptainer exec myContainer.sif bash -c 'source /opt/mamba/etc/profile.d/mamba.sh; source activate mambaEnvironment; myCommand'

And this works fine. I tried to set up the mamba environment activation on the section of the definition file like so:

%environment
  PATH=$PATH:/bbmap/:/opt/mamba/bin/
  . /opt/mamba/etc/profile.d/mamba.sh
  mamba activate blee

but it doesn't seem to do anything. I basically would like to set it up so that everytime I do apptainer exec (or singularity exec) it loads to appropriate mamba/conda environment (this so that I can run it easily as part of a nextflow pipeline). How can I do this?

Below is an example recipe:

Bootstrap: docker
From: ubuntu:20.04

%post
  apt-get -y update
  apt-get -y upgrade
  DEBIAN_FRONTEND=noninteractive
  apt-get -y install default-jre
  apt-get -y --no-install-recommends install wget git zlib1g-dev curl openssh-client tar gzip ca-certificates build-essential cmake

  #Install Mamba
  readonly mamba_installer="Mambaforge-$(uname)-$(uname -m).sh"
  readonly mamba_version="4.10.3-4"
  readonly mamba_prefix="/opt/mamba"
  wget "https://github.com/conda-forge/miniforge/releases/download/${mamba_version}/${mamba_installer}"
  bash "${mamba_installer}" -b -p "${mamba_prefix}"
  rm "${mamba_installer}"
  export PATH="/opt/mamba/bin:$PATH"

  PATH=$PATH:/bbmap/
  mamba config --add channels defaults
  mamba config --add channels conda-forge
  mamba config --add channels bioconda

  mamba create -y -n blee python=3.6 pip
  mamba init
  . /opt/mamba/etc/profile.d/mamba.sh
  . activate blee
  mamba install -n blee -c bioconda nanocomp porechop

%environment
  PATH=$PATH:/bbmap/:/opt/mamba/bin/
  . /opt/mamba/etc/profile.d/mamba.sh
  mamba activate blee

%runscript
    exec "$@"
1

There are 1 answers

0
dthorbur On

To my knowledge, conda/mamba environments are only activated on interactive logins, even if you put activation in the .bashrc file.

If you add the env bin to PATH it usually works, but can require more paths being added to scripts. Something like ENV PATH /opt/mamba/env/blee/bin:$PATH. Also remember to update shebang if you are using executable scripts.