Skip to content

Commit 02bb9dc

Browse files
authored
Create 05_customize_container_environments.mdx
1 parent 7a5f2d7 commit 02bb9dc

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## Why Customize a Container Environment?
2+
Prebuilt container images provide a convenient starting point, but they may not contain all of the software required for a particular project.
3+
4+
Most Apptainer images are distributed as `.sif` files, which are typically read-only. If you need to install additional software or make persistent changes to a container, Apptainer provides overlays, which add a writable layer on top of the original image while leaving the `.sif` file unchanged.
5+
6+
## Example: Creating a Custom Directory with an Overlay
7+
8+
9+
Pull a Python container image from Docker Hub:
10+
11+
```bash
12+
apptainer pull python.sif docker://python:3.10
13+
```
14+
15+
Create a writable overlay:
16+
17+
```bash
18+
apptainer overlay create --size 1024 python_overlay.ext3
19+
```
20+
21+
Launch the container with the overlay attached:
22+
23+
```bash
24+
apptainer shell \
25+
--overlay python_overlay.ext3:rw \
26+
python.sif
27+
```
28+
29+
Verify that numpy is not installed in the base image:
30+
31+
```bash
32+
python -c "import numpy"
33+
```
34+
35+
You should see:
36+
37+
```text
38+
ModuleNotFoundError: No module named 'numpy'
39+
```
40+
41+
Install numpy:
42+
43+
```bash
44+
pip install numpy
45+
```
46+
47+
Verify that the packages are available:
48+
49+
```bash
50+
python -c "import numpy; print(numpy.__version__)"
51+
```
52+
53+
Exit the container:
54+
55+
```bash
56+
exit
57+
```
58+
59+
The next time you want to use the customized environment, launch the container using the same overlay:
60+
61+
```bash
62+
apptainer shell \
63+
--overlay python_overlay.ext3:rw \
64+
python.sif
65+
```
66+
67+
This demonstrates that software installed within the overlay persists across sessions, while the original `.sif` image remains unchanged.

0 commit comments

Comments
 (0)