Is there a way to auto-pass environment variables to the container?

1,438 views
Skip to first unread message

Jonathan Greenberg

unread,
Jun 25, 2018, 12:27:47 PM6/25/18
to singularity
I'm still a bit confused about how environment variables are passed to the containers.  Say I want to make sure some environment variable I've assigned my global environment "MOO" available in the container "automatically" (not tacking it to the singularity exec statement) -- how do I go about doing this?

--j

Thomas Hartmann

unread,
Jun 25, 2018, 12:43:17 PM6/25/18
to singu...@lbl.gov
Hi Jonathan,

if you create a Singularity container natively, you can add environment
variables in your Singularity file with the enironment flag, e.g.

%environment
MYFOOVAR="somevalue"
MYBAZVAR=23

Else, if you have imported a container from Docker or so, you maybe can
trick it by creating a sandboxed version and add an env-script to

yoursandbox.d/.singularity.d/env/99-mystuff.sh

(there should be a hidden .singularity/ directory in a container, where
Singularity keeps its info)

Cheers,
Thomas
> --
> You received this message because you are subscribed to the Google
> Groups "singularity" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to singularity...@lbl.gov
> <mailto:singularity...@lbl.gov>.

Jason Stover

unread,
Jun 25, 2018, 12:47:18 PM6/25/18
to singu...@lbl.gov
Hi,

As mentioned you can use the %environment section in the DEF file.
If you need to pass changing values, you can do something like:

export SINGULARITYENV_MOO="cow"

Then when you run singularity, you should have the "MOO" variable
available to you. Just prefix whatever with "SINGULARITYENV_" ... i.e.
SINGULARITYENV_MY_VAR ... for "MY_VAR".

-J
> To unsubscribe from this group and stop receiving emails from it, send an email to singularity...@lbl.gov.

Jonathan Greenberg

unread,
Jun 25, 2018, 12:59:57 PM6/25/18
to singularity
Hmm, so is "TMPDIR" a protected name?  The MOO -> cow thing worked properly, but:

export SINGULARITYENV_MOO="cow" 
export SINGULARITYENV_TMPDIR=/pathto/scratch/$USER
echo $SINGULARITYENV_MOO
cow
echo $SINGULARITYENV_TMPDIR
/pathto/scratch/jgreenberg

Now in Singularity:
singularity shell ~/gearslaboratory-gears-singularity-master-gears-general.simg

echo $MOO
cow

echo $TMPDIR
TMPDIR

Not working!  Thoughts?

--j

Jason Stover

unread,
Jun 25, 2018, 1:16:49 PM6/25/18
to singu...@lbl.gov
No, not protected. That should work. I'm not getting that at least...

$ echo $TMPDIR

$ export SINGULARITYENV_TMPDIR="/lala/tmp"
$ /usr/local/singularity/2.5.1/bin/singularity shell ~/images/ubuntu-latest.img
Singularity ubuntu-latest.img:~> echo $TMPDIR
/lala/tmp

-J
>> > > <mailto:singularity...@lbl.gov>.

Thomas Hartmann

unread,
Jun 25, 2018, 1:36:33 PM6/25/18
to singu...@lbl.gov
hmmm, can you grep in your container, if maybe another script
sets/overwrites TMPDIR?

grep -ri TMPDIR /.singularity.d/env
> > > an email to singularity...@lbl.gov <javascript:>
> > > <mailto:singularity...@lbl.gov <javascript:>>.
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "singularity" group.
> > To unsubscribe from this group and stop receiving emails from it,
> send an email to singularity...@lbl.gov <javascript:>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "singularity" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to singularity...@lbl.gov
> <mailto:singularity...@lbl.gov>.

Jonathan Greenberg

unread,
Jun 25, 2018, 1:59:50 PM6/25/18
to singularity, thomas....@desy.de
The grep (from inside the container) returns:

/.singularity.d/env/90-environment.sh:    TMPDIR=TMPDIR
/.singularity.d/env/90-environment.sh:    export TMPDIR TMP TEMP
>     > > <mailto:singularity+unsub...@lbl.gov <javascript:>>.
>     >
>     > --
>     > You received this message because you are subscribed to the Google
>     Groups "singularity" group.
>     > To unsubscribe from this group and stop receiving emails from it,
>     send an email to singularity...@lbl.gov <javascript:>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "singularity" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to singularity...@lbl.gov

Jonathan Greenberg

unread,
Jun 25, 2018, 2:01:18 PM6/25/18
to singularity, thomas....@desy.de
AH.  Got it.  I had (don't remember why):

%environment
    TMPDIR=TMPDIR
    TMP=TMP
    TEMP=TEMP
    export TMPDIR TMP TEMP 

in my container def.  Fixing it now!

--j
Reply all
Reply to author
Forward
0 new messages