Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Containers: Docker & Apptainer

University of Pisa

Why containers


Online resources

Docker docs: https://docs.docker.com/
Dockerfile reference: https://docs.docker.com/engine/reference/builder/
Apptainer docs: https://apptainer.org/docs/
Singularity Hub conversion: https://apptainer.org/docs/user/main/docker_and_oci.html


Docker 101


Dockerfile essentials

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "-m", "main"]

Layering and caching tips


Running tests inside the image


Jupyter in a container


Docker compose


Translating the Jupyter run command

CLI we had:
docker run --rm -p 8888:8888 -v "$PWD":/work -w /work myjupyter

Equivalent compose.yml service:

services:
  jupyter:
    image: myjupyter
    ports:
      - "8888:8888"
    working_dir: /work
    volumes:
      - ./:/work
    command:
      - jupyter
      - lab
      - --ip=0.0.0.0
      - --no-browser
      - "--NotebookApp.token=''"

Run it: docker compose up (add -d to detach). Stop with docker compose down.


Compose tips


VS Code + containers


Option 1: Remote - Containers extension


Minimal devcontainer.json (pre-built image)

{
  "name": "VWCE dev",
  "image": "myjupyter:latest",
  "workspaceFolder": "/work",
  "mounts": ["source=${localWorkspaceFolder},target=/work,type=bind"]
}

Using docker compose with VS Code


Apptainer (Singularity) for HPC


Security and reproducibility


Lab 07 outline