@@ -388,26 +388,38 @@ This section is where you can download files from the internet with
388388tools like ``git `` and ``wget ``, install new software and libraries,
389389write 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+
391396Consider 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
401407update the container and install the program ``netcat `` (that will be
402408used 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