废话不多说,以拉取ubuntu
构建自己的学习环境为例,详细步骤如下:
ubuntu
镜像的拉取
docker search ubuntu
:查看可用 ubuntu 镜像docker pull ubuntu
:拉取名为 ubuntu 的镜像至服务器docker imgaes
:查看服务器中可用镜像
- 通过镜像创建容器
docker run --gpus all -p 6009:6006 -p 7600:22 --name 容器名 -v 主机目录:容器目录 -it 镜像ID /bin/bash
。容器创建后自动进入容器内。
- 容器中必要设置与下载
passwd
:设置 root 用户的密码apt-get update
apt-get install vim
- Miniconda/Anaconda 安装
apt-get install wget
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py39_4.9.2-Linux-x86_64.sh 或 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh
:下载bash Miniconda-3.6.0-Linux-x86_64.sh 或 bash Anaconda3-5.3.1-Linux-x86_64.sh
:安装export PATH=$PATH:/root/anaconda3/bin
:添加路径rm Miniconda3-py39_4.9.2-Linux-x86_64.sh
:删除安装包conda -V
:查看 conda 版本
- Miniconda/Anaconda 换源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
- SSH 的安装配置
apt-get install openssh-server
vim /etc/ssh/sshd_config
:修改 PermitRootLogin yes ,允许 root 用户使用 ssh 登录service ssh restart
:重启 SSH 服务
- Pycharm 中 Interpreter 设置(确保容器中SSH服务开启)
Setting -> Python Interpreter -> show all -> add -> SSH Interpreter -> New server configuration(服务器IP/前文所设端口7600/用户名root) -> next -> 输入root密码(前文所设置密码) -> 选择容器中的 Python 编译器、映射本机目录和容器中目录(生成容器时所设容器目录) -> Finish
至此,可通过 Pycharm 连接 Docker 中的环境进行网络模型的训练。容器中只装有环境,代码和数据集放在服务器中,二者通过目录的挂载相连接(相当于服务器在容器中创建了一个目录的快捷方式)。本地通过 SSH 服务连接容器,选择容器中的 python 编译器,同时工作目录与容器中的工作目录进行映射(因挂载的缘故,实质上映射的是服务器中的文件目录)。
- 容器打包为新镜像
docker commit -a "作者信息" -m "提交信息" 容器名/ID 新镜像名:tag标签
【参考】