Soerendip
Soerendip

Reputation: 9185

How to correctly activate micromamba environment inside Apptainer container automatically?

I am trying to build a micromamba based container, that automatically activates the correct environment to run an application (here jupyter-lab).

Apptainer.def

Bootstrap: docker
From: mambaorg/micromamba:1.5.8

%files
    environment.yaml /environment.yaml

%post
    micromamba create -n __apptainer__ && \
    micromamba install -n __apptainer__ --file environment.yaml && \
    micromamba clean --all --yes

    # Activate the environment and set it as default when starting the container
    echo "source /opt/conda/bin/activate __apptainer__" >> $SINGULARITY_ENVIRONMENT
    
    micromamba shell init --shell bash --root-prefix=~/micromamba

    # Update PATH to include micromamba binaries and other environment variables
    echo 'export PATH="/opt/conda/envs/__apptainer__/bin:$PATH"' >> $SINGULARITY_ENVIRONMENT

    # Add micromamba shell hook and activation to Singularity environment script
    echo '/usr/bin/micromamba activate __apptainer__' >> $SINGULARITY_ENVIRONMENT

environment.yaml

name: __apptainer__
channels:
  - conda-forge 
dependencies:
  - jupyterlab

after building the container with apptainer build jupyter-lab-micromamba.sif Apptainer.def, I get this:

apptainer exec jupyter-lab-micromamba.sif which jupyter-lab

'micromamba' is running as a subprocess and can't modify the parent shell.
Thus you must initialize your shell before using activate and deactivate.

To initialize the current  shell, run:
    $ eval "$(micromamba shell hook --shell )"
and then activate or deactivate with:
    $ micromamba activate
To automatically initialize all future () shells, run:
    $ micromamba shell init --shell  --root-prefix=~/micromamba
If your shell was already initialized, reinitialize your shell with:
    $ micromamba shell reinit --shell 
Otherwise, this may be an issue. In the meantime you can run commands. See:
    $ micromamba run --help

Supported shells are {bash, zsh, csh, xonsh, cmd.exe, powershell, fish}.
critical libmamba Shell not initialized
/opt/conda/envs/__apptainer__/bin/jupyter-lab

It works, but I would like to get rid of that warning.

I tried to run this during %post: eval "$(micromamba shell hook --shell bash)", but that also produces problems. How must this be done, so that the correct enviroment is automatically active?

Upvotes: 2

Views: 3241

Answers (1)

Ben Mares
Ben Mares

Reputation: 2157

Please see the documentation on using mambaorg/micromamba with apptainer. The particular part that seems relevant for your case is:

  1. apptainer exec does not execute the entrypoint script and therefore does not automatically activate the base environment. By prepending /usr/local/bin/_entrypoint.sh to the command you want to execute within the container, you can activate the base environment. For example:

    apptainer exec /usr/local/bin/_entrypoint.sh micromamba info
    

I'm not personally familiar with apptainer, but if this doesn't make obvious how to accomplish what you're trying to do, feel free to open an issue in mambaorg/micromamba and we'll try to help further.

Upvotes: 1

Related Questions