dockerenvironment-variablescontainerssingularity-container

Singularity : run container and preserving env variables


I would like to run a docker image with singularity exec. I would like to preserve the env variables of the docker container notably the PATH variable. For that I used --envclean. However, thought PATH is not "inherited" from my host, I don't see the PATH of the docker container neither.

Specificaly :

singularity exec --no-home -e docker://ocaml/opam printenv PATH

returns :

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

While it should be :

/home/opam/.opam/5.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

as returned by :

docker run -ti ocaml/opam printenv PATH

Any idea why is that? And how can I fix it?


Solution

  • The --envclean (-e) option in Singularity clears the environment, including container-specific variables, potentially overriding those set in the original Docker image.

    Use Singularity's --env flag: To manually import the desired environment variables from the Docker container, you can use the --env option with the variables specified.

    Inspect Docker Image ENV: Run docker inspect ocaml/opam to check the environment variables defined within the Docker image. Look for entries in the Config.Env section.

    Run your Singularity command and manually set the PATH as defined in the Docker image

    singularity exec --no-home --env PATH="/home/opam/.opam/5.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" docker://ocaml/opam printenv PATH
    

    Instead of --envclean, use --env to set only the variables you want to preserve while keeping the Docker container's original environment intact.