@@ -178,3 +178,182 @@ For commonly used software, it is often easier to use a cluster-provided image.
178178
179179If the software you need is not available, you can pull an image directly from Docker Hub and run it with Apptainer.
180180
181+ ## Using Containers in Batch Jobs
182+
183+ So far, we have run containers interactively from the command line. In practice, most work on Torch is submitted through Slurm batch jobs.
184+
185+ Containers can be used directly inside a Slurm job script in the same way as any other command.
186+
187+ Create a file named `container_job.sh`:
188+
189+ ```bash
190+ #!/bin/bash
191+ #SBATCH --job-name=container-demo
192+ #SBATCH --account=torch_pr_XXX_XXXXX
193+ #SBATCH --time=00:05:00
194+ #SBATCH --cpus-per-task=1
195+ #SBATCH --mem=1G
196+
197+ apptainer exec \
198+ /share/apps/images/ubuntu-24.04.3.sif \
199+ cat /etc/os-release
200+ ```
201+
202+ Replace the account name with your own project account.
203+
204+ Submit the job:
205+
206+ ```bash
207+ sbatch container_job.sh
208+ ```
209+
210+ When the job starts running, Slurm will launch the container and execute the command inside it.
211+
212+ You can monitor the job using:
213+
214+ ```bash
215+ squeue --me
216+ ```
217+
218+ After the job completes, inspect the output file:
219+
220+ ```bash
221+ cat slurm-<jobid>.out
222+ ```
223+
224+ You should see output similar to:
225+
226+ ```text
227+ PRETTY_NAME="Ubuntu 24.04.3 LTS"
228+ NAME="Ubuntu"
229+ VERSION_ID="24.04"
230+ ```
231+
232+ ## Running Software from a Container in a Batch Job
233+
234+ Instead of running a simple command, a batch job can also execute software provided by a container image.
235+
236+ For example:
237+
238+ ```bash
239+ #!/bin/bash
240+ #SBATCH --job-name=python-demo
241+ #SBATCH --account=torch_pr_XXX_XXXXX
242+ #SBATCH --time=00:05:00
243+ #SBATCH --cpus-per-task=1
244+ #SBATCH --mem=1G
245+
246+ /share/apps/images/run-anaconda3-2024.10-1.bash \
247+ python --version
248+ ```
249+
250+ Submit the job:
251+
252+ ```bash
253+ sbatch python_job.sh
254+ ```
255+
256+ When the job completes, the output file should contain:
257+
258+ ```text
259+ Python 3.12.7
260+ ```
261+
262+ This approach allows you to use containerized software in batch jobs without installing packages in your home directory.
263+
264+ ## Containers and Slurm
265+
266+ From Slurm's perspective, a container is simply another program being executed within the job allocation.
267+
268+ This means that containers can be used together with the same Slurm workflows discussed in previous tutorials, including:
269+
270+ - CPU jobs
271+ - GPU jobs
272+ - Interactive jobs
273+ - Job arrays
274+
275+ In most cases, the only difference is that the application is launched through Apptainer rather than directly from the host operating system.
276+
277+ ## Writable Containers and Overlays
278+
279+ Apptainer images are immutable by default. This means that files inside the image cannot be modified.
280+
281+ For example, if you start a shell inside a container:
282+
283+ ```bash
284+ apptainer shell \
285+ /share/apps/images/ubuntu-24.04.3.sif
286+ ```
287+
288+ you can view files inside the image, but changes made to the image itself will not be saved.
289+
290+ This behavior helps ensure that container images remain reproducible and consistent across users.
291+
292+ ## Why Use Overlays?
293+
294+ In some situations, you may want to install additional software or save files inside a container environment.
295+
296+ Examples include:
297+
298+ - Installing Python packages
299+ - Creating Conda environments
300+ - Saving configuration files
301+ - Building custom software
302+
303+ Rather than modifying the image itself, Apptainer provides writable overlays.
304+
305+ An overlay acts as a writable layer that sits on top of a read-only container image.
306+
307+ ```text
308+ Ubuntu Image (.sif)
309+ +
310+ Writable Overlay
311+ =
312+ Custom Environment
313+ ```
314+
315+ Changes are written to the overlay while the original image remains unchanged.
316+
317+ ## Using an Overlay
318+
319+ A container can be started with an overlay using the `--overlay` option:
320+
321+ ```bash
322+ apptainer shell \
323+ --overlay overlay.ext3:rw \
324+ /share/apps/images/ubuntu-24.04.3.sif
325+ ```
326+
327+ Files created during the session are stored in the overlay rather than inside the image.
328+
329+ The next time the same overlay is attached, those files will still be available.
330+
331+ ## Using Fakeroot
332+
333+ Some software installation workflows expect root privileges.
334+
335+ Apptainer provides a `--fakeroot` option that gives root-like permissions inside the container without requiring administrative access on the cluster.
336+
337+ For example:
338+
339+ ```bash
340+ apptainer shell \
341+ --fakeroot \
342+ --overlay overlay.ext3:rw \
343+ /share/apps/images/ubuntu-24.04.3.sif
344+ ```
345+
346+ This is commonly used when creating Conda environments or installing software into a writable overlay.
347+
348+ ## When Should You Use an Overlay?
349+
350+ For most users, Torch-provided container images are sufficient.
351+
352+ Consider using an overlay when:
353+
354+ - You need additional packages that are not included in an image.
355+ - You want to save changes between container sessions.
356+ - You need a custom software environment for a project.
357+
358+ Using overlays allows you to customize your environment while continuing to use centrally maintained container images.
359+
0 commit comments