Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activating environment inside container. #45

Open
sorenwacker opened this issue May 14, 2024 · 2 comments
Open

Activating environment inside container. #45

sorenwacker opened this issue May 14, 2024 · 2 comments

Comments

@sorenwacker
Copy link

sorenwacker commented May 14, 2024

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
Copy link
Author

It seems the container does not execute the entrypoint script.

@cdeciampa
Copy link

cdeciampa commented Jul 18, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants