Skip to content

Commit 20f1a20

Browse files
Merge pull request #219 from monicagiraldochica/edit_containersguide
Update containers.md
2 parents 15fe7e1 + 43a3a10 commit 20f1a20

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

docs/software/containers.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ For more information see the [Apptainer Quick Start guide](https://apptainer.org
8181

8282
### Download Pre-built Containers
8383

84-
Many software packages already have containers built by developers. These containers are available from external sites such as Docker Hub or ghcr.io. With apptainer you can use the `pull` or `build` commands to download containers.
84+
Many software packages already have containers built by developers that you can download with the `pull` or `build` commands. These containers are available from external sites such as the [Container Library](https://cloud.sylabs.io/library){:target="_blank"} or [Docker Hub](https://hub.docker.com/){:target="_blank"}.
8585

8686
The `apptainer pull` command will download a OCI image from a remote repository, combining the layers to a usable SIF image.
8787

@@ -101,7 +101,7 @@ $ ./lolcow_latest.sif
101101
|| ||
102102
```
103103

104-
The `apptainer build` command can also be used to download existing containers. The `build` command has many more uses for building containers from scratch or converting formats, which we cover later. For the purpose of downloading a pre-built container, `build` differs from `pull` by converting the image to the latest Apptainer image format after downloading it. Further, `build` allows for custom naming of the downloaded container.
104+
The `build` command has many more uses for building containers from scratch or converting formats, which we cover later. For the purpose of downloading a pre-built container, `build` differs from `pull` by converting the image to the latest Apptainer image format after downloading it. Further, `build` allows for custom naming of the downloaded container.
105105

106106
```txt
107107
$ apptainer build my_lolcow.sif docker://ghcr.io/apptainer/lolcow
@@ -119,6 +119,8 @@ $ ./my_lolcow.sif
119119
|| ||
120120
```
121121

122+
Because these containers are already built, they are immutable. So, you can run things but not install things inside them. To do this, you would build a container, which is explained in the next section.
123+
122124
### Build a Container
123125

124126
The `build` command is used to create new containers either from scratch or based on existing containers. It can also used to convert between container formats. As an example, we'll discuss the process of building custom Python container from scratch.
@@ -127,7 +129,7 @@ Please see the [Apptainer docs](https://apptainer.org/docs/user/1.3/build_a_cont
127129

128130
#### Definition File
129131

130-
First we write a definition file for the new container. A definition file defines the container build process including software installation, runtime functionality, etc. This file can be named how you like, but it is recommended to denote definition files with the `.def` extension. We will call our definition file `py_container.def`.
132+
First we write a definition file for the new container. A definition file defines the container build process including software installation, runtime functionality, etc. This file can be named how you like, but it is recommended to denote definition files with the `.def` extension. We will call our definition file `py_container.def`. See below for an explanation of each section.
131133

132134
=== "py_container.def"
133135

@@ -136,17 +138,13 @@ Bootstrap: docker
136138
From: ubuntu:20.04
137139

138140
%help
139-
# Add information describing your container.
140-
This container includes Python.
141+
This container includes Python.
141142

142143
%environment
143-
# Set useful environment variables here. This section is used only at runtime, not during build.
144144
export PATH=/opt/python/bin:$PATH
145145
export LD_LIBRARY_PATH=/opt/python/lib:$LD_LIBRARY_PATH
146146

147147
%post
148-
# Add build commands here. These commands are only run at build time.
149-
150148
# Create useful bind points for needed file systems. The following are set by default in RCC and should always be included for containers used in RCC.
151149
mkdir -p /scratch /hpc
152150

@@ -178,8 +176,24 @@ This container includes Python.
178176
/opt/python/bin/python --version
179177
```
180178

179+
As you can see in the previous example, a definition file has two parts. First is the header, which contains information about the base operating system for the container. `Bootstrap` is the first keyword and it's mandatory. It will specify the bootstrap agent. The most common ones are: `library`, `docker`, `shub`, or `localimage`. When using `library` as bootstrap agent, the next keyword must be `From`, followed by the operating system and the version. For example, `From: ubuntu:22.04`.
180+
181+
The second part of a definition file is the sections, which execute commands at different times while the container is built or run. All sections are optional. The following table explains them in detail:
182+
183+
| Section | Explanation |
184+
| --- | --- |
185+
| `%files` | Use this section to copy files from the host into the container. Never copy files into /home, /tmp, or any other directories that are bound at runtime. |
186+
| `%environment` | Specify variables that will be set **at run** time in this section (NOT available when building the container). Variables set here are global (available to all apps). |
187+
| `%post` | In this section you can: specify variables that will be set **at build time**, download files from the internet with tools like git and wget, install new software and libraries, write configuration files, create new directories. Never install things into /home, /tmp, or any other directories that are bound at runtime. |
188+
| `%runscript` | This section is executed when the container is run via the `apptainer run` command or by executing the container directly as a command. Any arguments passed to the container will be processed in this section. We can use here variables set in`%environment` or `%post`. |
189+
| `%test` | This section runs at the end of the build process to validate the container using a method of your choice. |
190+
| `%labels` | You can add metadata in this section with name-value format such as `Author Me` or `Version v1.0`. |
191+
| `%help` | Use this section to explain to the user how to interact with the container. |
192+
181193
Full documentation of definition files at [Apptainer Docs](https://apptainer.org/docs/user/1.3/definition_files.html){:target="_blank"}.
182194

195+
Examples of definition files: [Apptainer examples](https://github.com/apptainer/apptainer/tree/main/examples){:target="_blank"}.
196+
183197
#### Build
184198

185199
Build the container using the definition file:

0 commit comments

Comments
 (0)