Skip to content

Commit e3c38e7

Browse files
committed
Clarify build environment handling
* Host env not passed into the build scriptlets. * Values can be passed in using new templating support. * Re-write SINGULARITY_ENVIRONMENT example, which was less than clear. Fixes #15
1 parent 35c91c1 commit e3c38e7

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

build_env.rst

+9
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ meaning that no intermediate decrypted data is ever written to disk. See
271271
Environment Variables
272272
*********************
273273

274+
You can set environment variables on the host to influence the behaviour of a
275+
build. Note that environment variables are not passed into the build itself, and
276+
cannot be accessed in the ``%post`` section of a definition file. To pass values
277+
into a build, use the :ref:`templating / build-args support <sec:templating>`
278+
introduced in {Singularity} 4.0.
279+
280+
Environment variables that control a build are generally associated with an
281+
equivalent CLI option. The order of precendence is:
282+
274283
#. If a flag is represented by both a CLI option and an environment variable,
275284
and both are set, the CLI option will take precedence. This is true for all
276285
environment variables with the exception of ``SINGULARITY_BIND`` and

definition_files.rst

+19-7
Original file line numberDiff line numberDiff line change
@@ -388,26 +388,38 @@ This section is where you can download files from the internet with
388388
tools like ``git`` and ``wget``, install new software and libraries,
389389
write configuration files, create new directories, etc.
390390

391+
The commands in the ``%post`` section run in a clean environment. Environment
392+
variables from the host are not passed into the build. To pass values into a
393+
build you should use the :ref:`templating / build-args support <sec:templating>`
394+
introduced in {Singularity} 4.0.
395+
391396
Consider the ``%post`` section from the example definition file above:
392397

393398
.. code:: singularity
394399
395400
%post
396401
apt-get update && apt-get install -y netcat
402+
397403
NOW=`date`
398404
echo "export NOW=\"${NOW}\"" >> $SINGULARITY_ENVIRONMENT
399405
400406
This ``%post`` scriptlet uses the Ubuntu package manager ``apt`` to
401407
update the container and install the program ``netcat`` (that will be
402408
used in the ``%startscript`` section below).
403409

404-
The script also sets an environment variable at build time. Note that
405-
the value of this variable cannot be anticipated, and therefore cannot
406-
be set earlier in the ``%environment`` section. For situations like
407-
this, the ``$SINGULARITY_ENVIRONMENT`` variable is provided. Assigning a
408-
value to this variable will cause it to be written to a file called
409-
``/.singularity.d/env/91-environment.sh`` that will be sourced by the
410-
container at runtime.
410+
The scriptlet also sets an environment variable called ``NOW`` to the current
411+
date and time. It then writes an ``export`` statement for ``NOW`` into the file
412+
that is pointed to by ``$SINGULARITY_ENVIRONMENT``. This demonstrates how to set
413+
container environment variables (available when the container is run), that are
414+
not known when the definition file is written. Because the value of ``$NOW`` is
415+
only known when the ``date`` command in the ``%post`` scriptlet is run, it
416+
cannot be added to the ``%environment`` section.
417+
418+
Directing output into ``$SINGULARITY_ENVIRONMENT`` will cause it to be written
419+
to a shell script file called ``/.singularity.d/env/91-environment.sh`` that
420+
will be sourced by the container at runtime. The ``export NAME=VALUE`` syntax is
421+
needed to make the environment variable available to all processes that are
422+
started in the container.
411423

412424
.. note::
413425

0 commit comments

Comments
 (0)