跳转至

826. 容器方案对比:Docker vs Singularity/Apptainer

一句话概述:Docker是容器世界的"通用标准",Singularity/Apptainer是"HPC专属方案"——开发用Docker,集群跑用Singularity,两者可互转。

核心知识点速查表

维度DockerSingularity/Apptainer
定位通用容器平台HPC/科学计算专用
权限要求需root(守护进程)无需root
安全性多用户环境有风险为共享集群设计
性能有一定开销接近裸机性能
镜像格式分层镜像单文件.sif格式
MPI支持需额外配置原生支持
GPU支持--gpus参数--nv参数
兼容性行业标准可直接运行Docker镜像
网络隔离完整网络栈共享宿主网络
文件系统隔离(需挂载)自动绑定$HOME

一、为什么生信需要容器?(白话版)

想象你写了一个分析脚本,依赖BWA 0.7.17 + samtools 1.15 + Python 3.9。你把脚本发给合作者,他电脑上装的版本都不一样,跑不通。

容器 = 把你的软件+所有依赖打包成一个"密封箱",不管搬到哪台电脑上,打开就能用,环境完全一致。

  • Docker = 通用密封箱,适合笔记本和云服务器
  • Singularity = 实验室专用密封箱,适合超算集群(不需要管理员权限)

二、基础操作对比

2.1 Docker基础

# 拉取镜像(从Docker Hub下载)
docker pull biocontainers/bwa:v0.7.17  # 下载BWA容器镜像

# 运行容器(一次性命令)
docker run --rm \                      # --rm: 运行后自动删除容器
  -v $(pwd):/data \                    # 挂载当前目录到容器的/data
  biocontainers/bwa:v0.7.17 \          # 使用BWA镜像
  bwa mem /data/ref.fa /data/reads.fq  # 在容器内运行BWA

# 交互式进入容器
docker run -it \                       # -it: 交互模式
  -v $(pwd):/data \                    # 挂载目录
  ubuntu:22.04 bash                    # 进入Ubuntu容器的bash

# 构建自定义镜像(Dockerfile)
docker build -t my-bioinfo:v1 .        # 从当前目录的Dockerfile构建
docker push username/my-bioinfo:v1     # 推送到Docker Hub

2.2 Singularity/Apptainer基础

# 拉取镜像(从Docker Hub转换)
singularity pull bwa.sif \             # 下载并转换为.sif文件
  docker://biocontainers/bwa:v0.7.17   # 源:Docker Hub镜像

# 运行容器(直接执行)
singularity exec bwa.sif \             # exec: 在容器中执行命令
  bwa mem ref.fa reads.fq              # 无需挂载,自动绑定$HOME

# 绑定额外目录
singularity exec \
  --bind /data/reference:/ref \        # 绑定宿主目录到容器路径
  bwa.sif bwa mem /ref/genome.fa reads.fq

# 交互式Shell
singularity shell bwa.sif              # 进入容器Shell

# GPU支持
singularity exec --nv \                # --nv: 启用NVIDIA GPU
  parabricks.sif \                     # GPU加速基因组分析
  pbrun germline --ref ref.fa \
  --in-fq reads.fq --out-bam out.bam

# 构建镜像(从定义文件)
sudo singularity build my-bio.sif \    # 构建.sif镜像
  my-bio.def                           # 从定义文件构建

2.3 Dockerfile vs Singularity Definition

# Dockerfile 示例
FROM ubuntu:22.04                      # 基础镜像
RUN apt-get update && \                # 更新包列表
    apt-get install -y \               # 安装依赖
    bwa samtools python3               # 生信工具
COPY scripts/ /opt/scripts/            # 复制脚本到容器
WORKDIR /data                          # 设置工作目录
ENTRYPOINT ["python3"]                 # 默认入口命令
# Singularity定义文件 (my-bio.def)
Bootstrap: docker                      # 基于Docker镜像
From: ubuntu:22.04                     # 基础镜像

%post                                  # 构建时执行的命令
    apt-get update                     # 更新包列表
    apt-get install -y bwa samtools python3  # 安装工具

%files                                 # 复制文件到容器
    scripts/ /opt/scripts/             # 复制脚本

%environment                           # 设置环境变量
    export PATH=/opt/scripts:$PATH     # 添加脚本目录到PATH

%runscript                             # 默认运行脚本
    python3 "$@"                       # 运行Python

三、HPC集群中的使用

# SLURM + Singularity 提交脚本
#!/bin/bash
#SBATCH --job-name=bwa_align           # 作业名称
#SBATCH --nodes=1                      # 节点数
#SBATCH --cpus-per-task=16             # CPU数
#SBATCH --mem=32G                      # 内存
#SBATCH --time=04:00:00                # 最大运行时间
#SBATCH --gres=gpu:1                   # 申请1块GPU

# 加载Singularity模块
module load singularity                # 加载模块

# 使用Singularity运行分析
singularity exec \
  --bind /scratch:/scratch \           # 绑定scratch目录
  --nv \                               # 启用GPU
  /shared/containers/bioinfo.sif \     # 容器镜像路径
  bash /opt/scripts/run_pipeline.sh    # 运行分析脚本

四、推荐工作流

开发阶段(本地)         部署阶段(集群)
┌──────────────┐        ┌──────────────────┐
│  Docker 构建  │  ───→  │ Singularity 运行  │
│  Dockerfile   │  转换  │  .sif 单文件      │
│  docker build │        │  singularity exec │
└──────────────┘        └──────────────────┘

转换命令:
singularity pull output.sif docker://username/my-image:tag

五、面试高频问题

  1. Q: 为什么HPC集群不用Docker? A: Docker守护进程需要root权限,在多用户共享集群上有安全风险。Singularity无需root,用户权限映射到容器内,安全可控。

  2. Q: Singularity能运行Docker镜像吗? A: 可以。singularity pull docker://xxx直接转换Docker镜像为.sif格式。

  3. Q: Apptainer和Singularity什么关系? A: 2021年Singularity开源版加入Linux基金会后更名为Apptainer,功能基本一致。Sylabs继续维护商业版Singularity。

常见报错与解决

报错原因解决
permission denied (Docker)未加入docker组sudo usermod -aG docker $USER
FATAL: could not open image (Singularity).sif文件损坏或路径错误重新pull或检查路径
WARNING: Skipping mount绑定目录不存在创建目录或检查--bind路径
Docker镜像太大层太多多阶段构建(multi-stage build)
Singularity构建需要sudo普通用户无法构建--fakeroot或远程构建

速查表

# Docker 速查
docker pull image:tag          # 拉取镜像
docker run --rm -v path:path   # 运行容器
docker build -t name:tag .     # 构建镜像
docker images                  # 列出镜像
docker ps                      # 列出运行中容器
docker system prune            # 清理无用镜像

# Singularity 速查
singularity pull docker://...  # 从Docker Hub拉取并转换
singularity exec img.sif cmd   # 执行命令
singularity shell img.sif      # 交互式Shell
singularity build out.sif def  # 从定义文件构建
singularity exec --nv          # GPU支持
singularity exec --bind a:b    # 绑定目录