@@ -388,26 +388,38 @@ This section is where you can download files from the internet with
388
388
tools like ``git `` and ``wget ``, install new software and libraries,
389
389
write configuration files, create new directories, etc.
390
390
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
+
391
396
Consider the ``%post `` section from the example definition file above:
392
397
393
398
.. code :: singularity
394
399
395
400
%post
396
401
apt-get update && apt-get install -y netcat
402
+
397
403
NOW=`date`
398
404
echo "export NOW=\"${NOW}\"" >> $SINGULARITY_ENVIRONMENT
399
405
400
406
This ``%post `` scriptlet uses the Ubuntu package manager ``apt `` to
401
407
update the container and install the program ``netcat `` (that will be
402
408
used in the ``%startscript `` section below).
403
409
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.
411
423
412
424
.. note ::
413
425
0 commit comments