omics.co.in logo omics.co.in

Chapter 4: Containerization with Docker, Singularity & Apptainer

Tested Stacks: GATK 4.6.1 Samtools 1.21 Nextflow 24.10 Seurat v5.1 Python 3.12
Prerequisites: Basic Linux CLI & Sequencing Formats
📚 Chapter 4 of 13 0% Course Completed

Chapter 4: Containerization with Docker, Singularity & Apptainer

Running bioinformatics pipelines without containers leads to dependency chaos: conflicting GLIBC versions, incompatible Python packages, and broken system libraries. Container engines package the entire OS runtime, binary executables, and libraries into immutable images.

Docker vs. Singularity / Apptainer
  • Docker: Industry standard for local workstations and cloud environments (AWS, GCP). Requires root privileges / daemon, making it prohibited on shared institutional HPC clusters.
  • Singularity / Apptainer: Designed specifically for HPC security. Runs strictly as an unprivileged user, converting Docker images into single-file .sif (Singularity Image Format) binaries.

1. Writing Production Bio-Dockerfiles

# Multi-stage lightweight Dockerfile for Samtools & BWA
FROM ubuntu:22.04 AS builder

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libncurses5-dev \
    zlib1g-dev \
    libbz2-dev \
    liblzma-dev \
    wget \
    ca-certificates

# Compile samtools
RUN wget https://github.com/samtools/samtools/releases/download/1.19.2/samtools-1.19.2.tar.bz2 && \
    tar -xjf samtools-1.19.2.tar.bz2 && \
    cd samtools-1.19.2 && ./configure && make && make install

# Final minimal runtime container
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y --no-install-recommends \
    libncurses5 \
    zlib1g \
    libbz2-1.0 \
    liblzma5 && \
    rm -rf /var/lib/apt/lists/*

COPY --from=builder /usr/local/bin/samtools /usr/local/bin/samtools
ENTRYPOINT ["samtools"]

2. Enabling Containers in nextflow.config

// nextflow.config container settings
profiles {
    docker {
        docker.enabled = true
        docker.runOptions = '-u $(id -u):$(id -g)'
    }
    
    singularity {
        singularity.enabled = true
        singularity.autoMounts = true
        singularity.cacheDir = "$HOME/.singularity_cache"
    }
}

3. Leveraging BioContainers

Instead of building Docker images manually, bioinformatics pipelines leverage the BioContainers global registry. Every tool indexed in Bioconda is automatically compiled into pre-built Docker and Singularity images hosted on quay.io/biocontainers.

⚠️

Common Pitfalls & Troubleshooting Checklist

  • Java Heap Exhaustion (GATK / Picard): If processes terminate with java.lang.OutOfMemoryError, set your environment variable explicitly: export _JAVA_OPTIONS="-Xmx16g -Djava.io.tmpdir=/tmp".
  • Index Timestamp Mismatch: If tools complain that a BAM index is missing or out of date, regenerate with samtools index -@ 4 sample.bam.
  • Conda Channel Conflicts: Avoid ABI incompatibilities between bioconda and conda-forge by setting strict priority: conda config --set channel_priority strict.
  • Chromosome Naming Contamination: Always verify whether reference FASTA headers use chr1 or 1; divergent naming causes silent alignment drops.
Was this guide helpful to your research?
Help us maintain 100% accurate, peer-reviewed bioinformatics standards.
📝 Propose Edit on GitHub ↗