Conda Environments#

Adding a conda environment to a container may be easily done using cotainr.

Make sure you have the rights to use the packages in your conda environment

When adding a conda environment, it is the responsibility of the user of cotainr to make sure they have the necessary rights to use the conda channels/repositories and packages specified in the conda environment, e.g. if using the default Anaconda repositories.

As an example, consider the following conda environment file, my_conda_env.yml:

my_conda_env.yml#
channels:
  - conda-forge
dependencies:
  - python=3.11.0
  - numpy=1.23.5

A container based on the official Ubuntu 22.04 DockerHub image, containing this conda environment, may be built using:

$ cotainr build my_conda_env_container.sif --base-image=docker://ubuntu:22.04 --conda-env=my_conda_env.yml

The conda environment is automatically activated when the container is run, allowing for directly using the Python and NumPy versions installed in the conda environment, e.g.

$ singularity exec my_conda_env_container.sif python3 --version
Python 3.11.0
$ singularity exec my_conda_env_container.sif python3 -c "import numpy; print(numpy.__version__)"
1.23.5

You must accept the Miniforge license to add a conda environment

Bootstrapping of the conda environment in the container is done using Miniforge. As part of the bootstrap process, you must accept the Miniforge license terms. This can be done either by accepting them during the container build process, or by specifying the --accept-licenses option when invoking cotainr build.

Pip packages#

cotainr does not support creating a container directly from a pip requirements.txt file. However, pip packages may be included in a conda environment, e.g. updating my_conda_env.yml to

my_conda_env.yml#
channels:
  - conda-forge
dependencies:
  - python=3.11.0
  - numpy=1.23.5
  - pip
  - pip:
    - scipy==1.9.3

allows for installing SciPy via pip.

Pip packages from private repositories#

A pip package from a private repository behind an ssh key may be installed by enabling ssh-agent forwarding on the host machine using cotainr.

With my_conda_env.yml as

my_conda_env.yml#
channels:
  - conda-forge
dependencies:
  - python=3.11.0
  - git
  - openssh
  - pip
  - pip:
    - "--editable=git+ssh://git@github.com/foo/bar.git@SOMEHASHCODE#egg=baz"

where github.com:foo/bar.git is a private repository.

This is fundamentally an apptainer limitation/feature and not related to cotainr per se. For this to work, the directory pointed to on the host by the SSH_AUTH_SOCK environment variable must be bound to the container. If echo $SSH_AUTH_SOCK already points to one of the directories bound by default, e.g. /tmp, everything should work. Otherwise, another solution must be found, as cotainr does not expose directory binding from apptainer.