基于docker-compose和ngc cloud创建jupyter-lab环境
简介
本博客以构建指定版本的pytorch运行环境为例,给出Dockerfile、docker-compose.yml以及启动容器的运行脚本。
找到想要的容器
从中 https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/overview.html#overview中找到想要的镜像版本。本文以torch 1.8
为例,要安装的镜像版本为pytorch:20.12-py3
在https://catalog.ngc.nvidia.com/orgs/nvidia/containers/pytorch/tags中找到镜像的pull tag:
docker pull nvcr.io/nvidia/pytorch:20.12-py3
文件目录
文件目录如下
- torch_1_8
- Dockerfile
- run.sh
- docker-compose.yml
Dockerfile
FROM nvcr.io/nvidia/pytorch:20.12-py3 # 此处修改为根镜像的tag
MAINTAINER yourname
RUN apt-get update
RUN apt-get install -y openssh-server curl screen
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - && apt install -y nodejs
RUN python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jupyterlab ipympl nodejs ptvsd
RUN jupyter labextension install @jupyterlab/debugger
RUN conda install xeus-python -c conda-forge
RUN sed -ri 's/^PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/^#PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
COPY run.sh /run.sh
RUN chmod 777 /run.sh
ENV NOTEBOOK_APP_TOKEN=jupyter_passwd NOTEBOOK_DIR=/data # 此处设定jupyterlab的访问密码以及容器内的数据路径
RUN echo "root:ssh_passwd" | chpasswd
# ssh
EXPOSE 22
# jupyter
EXPOSE 8888
# tensorboard
EXPOSE 6006
docker-compose.yml
version: '3'
services:
dl:
build: .
entrypoint: /run.sh
ports:
- "12222:22" #对应ssh端口,注意填写前先查看端口号占用情况(可只更改x)
- "18888:8888" #对应jupyter端口,注意填写前先查看端口号占用情况(可只更改x)
- "16006:6006" #tensorboard
volumes:
- ${host_data}:/data # {要挂载的主机数据目录}:{容器内的数据目录}
cap_add:
- SYS_ADMIN
- DAC_READ_SEARCH
security_opt:
- apparmor:unconfined
deploy:
restart_policy:
condition: on-failure
resources:
reservations:
devices:
- driver: nvidia
device_ids: ['2'] # gpu编号,可以写多个
capabilities: [gpu]
run.sh
#!/bin/bash
service ssh start # 容器打开时打开ssh sever
jupyter lab --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token=${NOTEBOOK_APP_TOKEN} --notebook-dir=${NOTEBOOK_DIR}
docker-compose构建镜像及启动容器
构建镜像同时启动容器
docker-compose up -d
关闭并删除容器
docker-compose down
构建镜像
docker-compose build
容器的访问
ssh访问
ssh访问机器的12222
端口,账户为root
,密码ssh_passwd
浏览器访问upyter lab
浏览器访问18888
端口,密码为jupyter_passwd