mamba-org / micromamba-releases

Micromamba executables mirrored from conda-forge as Github releases
60 stars 18 forks source link

Activating environment inside container. #45

Open sorenwacker opened 5 months ago

sorenwacker commented 5 months ago

I have problems activating the environment by default within a container. Is micromamba using a different installation directory than conda?

Apptainer.def

Bootstrap: docker
From: mambaorg/micromamba:1.5.8

%files
    environment.yaml /environment.yaml
    requirements.txt /requirements.txt

%post
    apt-get update && apt-get install -y libopenmpi-dev curl wget vim watch procps ncdu tree

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

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

    # Add micromamba shell hook and activation to Singularity environment script
    echo 'micromamba shell init --shell bash' >> $SINGULARITY_ENVIRONMENT

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

Apptainer command

# Run script
srun apptainer exec \
    --nv \
    --env-file ~/.env \
    -B /home/:/home/ \
    $APPTAINER_SIF \
    python script.py

Error

source: open /opt/conda/bin/activate: no such file or directory
critical libmamba Shell not initialized

Output

'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}.
sorenwacker commented 4 months ago

It seems the container does not execute the entrypoint script.

cdeciampa commented 3 months ago

After banging my head against the wall for 2 days, I figured out a way to successfully activate the base environment for singularity exec and singularity shell.

Bootstrap: docker
From: mambaorg/micromamba:1.5.8

%files
    # Import yml into /opt
    ./environment.yml /opt

%environment
    export PATH="${MAMBA_ROOT_PREFIX}/bin:$PATH"

%post -c /bin/bash

    # Install into base environment from input yml
    micromamba install -y -n base -f /opt/environment.yml && \
    micromamba clean --all --yes

%runscript
    #!/bin/bash

    # Activate base environment
    source /usr/local/bin/_activate_current_env.sh

_activate_current_env.sh is included within the micromamba-docker image (see here).