Docker容器化部署科研环境:基于JupyterLab与CUDA的实践指南 (版本 2024.08.20)
TL;DR: 本文档提供基于Docker部署包含JupyterLab与CUDA的科研环境的详细步骤。核心目标是实现环境隔离、版本控制与可复现性。适用于需要GPU加速的计算任务。
1. 前置条件
部署前请确保满足以下条件。版本号为最低要求。
- 操作系统: Ubuntu 20.04 LTS 或更高版本
- Docker Engine: 20.10.0 或更高版本
- NVIDIA Driver: 470.00 或更高版本 (兼容CUDA 11.4)
- NVIDIA Container Toolkit: 1.7.0 或更高版本
Note: NVIDIA驱动与CUDA版本需匹配。可通过 nvidia-smi 命令检查。
nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.82.00 Driver Version: 470.82.00 CUDA Version: 11.4 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MEX |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:01:00.0 Off | N/A |
| 30% 45C P8 10W / 120W | 0MiB / 11178MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
2. Dockerfile构建与环境配置
本节展示如何构建一个包含JupyterLab、Python科学计算库及CUDA支持的Docker镜像。使用Dockerfile进行版本控制。
2.1 创建Dockerfile
在项目根目录创建 Dockerfile 文件。
FROM nvidia/cuda:11.4.0-cudnn8-runtime-ubuntu20.04
LABEL maintainer="花呗和谐号 "
LABEL version="1.0.0"
LABEL date="2024-08-20"
ENV DEBIAN_FRONTEND=noninteractive
# Install necessary packages
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.8 \
python3-pip \
git \
vim \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Set Python 3.8 as default
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
RUN update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
# Install JupyterLab and scientific libraries
RUN pip install --no-cache-dir \
jupyterlab==3.4.8 \
numpy==1.23.5 \
pandas==1.5.3 \
scikit-learn==1.2.2 \
matplotlib==3.7.1 \
scipy==1.10.1 \
tensorflow-gpu==2.10.0 \
torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu116
# Configure JupyterLab
RUN jupyter lab --generate-config
RUN echo "c.ServerApp.token = ''" >> /root/.jupyter/jupyter_lab_config.py
RUN echo "c.ServerApp.password = 'sha1:...'" >> /root/.jupyter/jupyter_lab_config.py # Replace with actual hashed password
RUN echo "c.ServerApp.allow_origin = '*'" >> /root/.jupyter/jupyter_lab_config.py
RUN echo "c.ServerApp.ip = '0.0.0.0'" >> /root/.jupyter/jupyter_lab_config.py
WORKDIR /app
EXPOSE 8888
CMD ["jupyter", "lab", "--port=8888", "--no-browser", "--allow-root"]
Warning: 生产环境请勿使用空密码或弱密码。使用 jupyter lab password 生成哈希密码。
2.2 构建Docker镜像
在 Dockerfile 所在目录执行构建命令。
docker build -t hbhxh/jupyter-cuda-env:v1.0.0 .
[+] Building 123.4s (20/20) FINISHED
...
Successfully tagged hbhxh/jupyter-cuda-env:v1.0.0
3. 容器运行与访问
本节描述如何运行已构建的Docker镜像,并从宿主机访问JupyterLab服务。解决“JupyterLab打不开怎么办”的问题。
3.1 启动容器
使用--gpus all参数启用GPU支持。将宿主机当前目录挂载到容器的/app目录。
docker run --gpus all -it --rm -p 8888:8888 -v "$(pwd)":/app hbhxh/jupyter-cuda-env:v1.0.0
[I 2024-08-20 10:30:00.123 ServerApp] Jupyter Server 1.23.4 is running at:
[I 2024-08-20 10:30:00.123 ServerApp] http://0.0.0.0:8888/lab
[I 2024-08-20 10:30:00.123 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
Note: --rm 参数表示容器停止时自动删除,适用于临时环境。如需持久化,请删除该参数并考虑使用具名卷。
3.2 验证GPU可用性
在JupyterLab终端中验证CUDA是否可用。
import torch
print(torch.cuda.is_available())
print(torch.cuda.device_count())
print(torch.cuda.get_device_name(0))
True
1
NVIDIA GeForce RTX 3080
Note: 确保输出为True且设备名称正确,表明GPU已成功映射到容器内。