Server - 使用 Docker 配置 PyTorch 研发环境

发布时间:2026/7/29 18:16:49
Server - 使用 Docker 配置 PyTorch 研发环境 欢迎关注我的CSDNhttps://spike.blog.csdn.net/本文地址https://spike.blog.csdn.net/article/details/148421901免责声明本文来源于个人知识与公开资料仅用于学术交流欢迎讨论不支持转载。建议使用 Docker 配置 PyTorch 研发环境原因是部分机器配置差异较大而且环境各不相同导致安装到最后仍然无法启动训练任务浪费大量时间。建议直接使用 Docker Conda(Mamba) 环境构建虚拟环境即可支持多数任务。1. 网络代理常用的 GitHub 下载较慢建议使用代理提速。使用特定的网络代理即exporthttps_proxyhttp://xxx:80exporthttp_proxyhttp://xxx:80unsethttps_proxy http_proxyxxx 是 IP 地址。或 使用在线的免费代理即https://ghproxy.link/# https://ghfast.topgitclone https://ghfast.top/https://github.com/hiyouga/LLaMA-Factory.git# 示例注意免费代理可能失效需要实时查看。Huggingface 环境参考https://hf-mirror.com/exportHF_ENDPOINThttps://hf-mirror.com2. 环境变量打印系统环境变量printenv配置大模型相关的环境变量写入~/.bashrc如下exportWORK_DIRxxxexportTORCH_HOME$WORK_DIR/torch_home/exportHF_HOME$WORK_DIR/huggingface/exportHUGGINGFACE_TOKENxxxexportMODELSCOPE_CACHE$WORK_DIR/modelscope_models/exportMODELSCOPE_API_TOKENxxxexportCUDA_HOME/usr/local/cudaexportOMP_NUM_THREADS643. Docker建议使用 Nvidia 的镜像其中包含默认的配置与环境https://docker.aityp.com/r/docker.io/nvcr.io/nvidia/pytorch拉取 Docker 镜像(国内代理)建议使用24.12-py3版本不要使用最新版本兼容异常dockerpull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nvcr.io/nvidia/pytorch:24.12-py3启动 Docker 的标准模版即dockerrun-itd\--name[your name]\--gpusall\--shm-size128g\--memory256g\--cpus64\--restartunless-stopped\--ipchost\--ulimitmemlock-1\--ulimitstack67108864\-v[your path]:[your path]\-eNVIDIA_DRIVER_CAPABILITIEScompute,utility\--privileged\--networkhost\swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nvcr.io/nvidia/pytorch:24.12-py3\/bin/bash4. 虚拟环境建议使用 Conda 或 Mamba以 Mamba 为例${SHELL}(curl-Lmicro.mamba.pm/install.sh)手动下载模式直接下载 micro.mamba.pm/install.sh即 GitHub 路径同时 替换代理https://ghfast.top/。配置 pip 源# docker 优先级rm-rf/usr/pip.confrm-rf/root/.config/pip/pip.confrm-rf/etc/pip.confrm-rf/etc/xdg/pip/pip.conf# 配置其他源mkdir~/.pipvim~/.pip/pip.conf[global]no-cache-dirtrueindex-urlhttp://mirrors.aliyun.com/pypi/simple/ trusted-hostmirrors.aliyun.com安装torch_def环境micromamba create-ntorch_defpython3.11pip3installtorch torchvision torchaudio--timeout100下载速度较慢避免超时--timeout100验证 PyTorch 环境importtorchprint(torch.__version__)# 2.7.0cu126print(torch.cuda.is_available())# True下载相关 Python 包pipinstalldatasets accelerate bitsandbytes peft swanlab sentencepiece trl deepspeed modelscope pipinstall-Uhuggingface_hub[cli]配置下载 Huggingface 模型与数据集参考huggingface-cli download Qwen/Qwen3-8B --local-dir Qwen/Qwen3-8B huggingface-cli download --repo-type dataset FreedomIntelligence/medical-o1-reasoning-SFT --local-dir FreedomIntelligence/medical-o1-reasoning-SFT参考数据集FreedomIntelligence/medical-o1-reasoning-SFT5. VSCode 切换 PyCharm 快捷键搜索IntelliJ IDEA Keybindings与Python插件并且安装即可实现全部功能。Name: IntelliJ IDEA Keybindings Id: k--kato.intellij-idea-keybindings Description: Port of IntelliJ IDEA Keybindings, includingforWebStorm, PyCharm, PHP Storm, etc. Version:1.7.7 Publisher: Keisuke Kato VS Marketplace Link: https://marketplace.visualstudio.com/items?itemNamek--kato.intellij-idea-keybindings其他已优化的 Mamba 安装文件mamba_install.sh如下#!/bin/shset-eu# Detect the shell from which the script was calledparent$(ps-ocomm$PPID|tail-1)parent${parent#-}# remove the leading dash that login shells havecase$parentin# shells supported by micromamba shell initbash|fish|xonsh|zsh)shell$parent;;*)# use the login shell (basename of $SHELL) as a fallbackshell${SHELL##*/};;esac# Parsing argumentsif[-t0];thenprintfMicromamba binary folder? [~/.local/bin] readBIN_FOLDERprintfInit shell ($shell)? [Y/n] readINIT_YESprintfConfigure conda-forge? [Y/n] readCONDA_FORGE_YESfi# FallbacksBIN_FOLDER${BIN_FOLDER:-${HOME}/.local/bin}INIT_YES${INIT_YES:-yes}CONDA_FORGE_YES${CONDA_FORGE_YES:-yes}# Prefix location is relevant only if we want to call micromamba shell initcase$INIT_YESiny|Y|yes)if[-t0];thenprintfPrefix location? [~/micromamba] readPREFIX_LOCATIONfi;;esacPREFIX_LOCATION${PREFIX_LOCATION:-${HOME}/micromamba}# Computing artifact locationcase$(uname)inLinux)PLATFORMlinux;;Darwin)PLATFORMosx;;*NT*)PLATFORMwin;;esacARCH$(uname-m)case$ARCHinaarch64|ppc64le|arm64);;# pass*)ARCH64;;esaccase$PLATFORM-$ARCHinlinux-aarch64|linux-ppc64le|linux-64|osx-arm64|osx-64|win-64);;# pass*)echoFailed to detect your OS2exit1;;esacif[${VERSION:-}];thenRELEASE_URLhttps://ghfast.top/https://github.com/mamba-org/micromamba-releases/releases/latest/download/micromamba-${PLATFORM}-${ARCH}elseRELEASE_URLhttps://ghfast.top/https://github.com/mamba-org/micromamba-releases/releases/download/${VERSION}/micromamba-${PLATFORM}-${ARCH}fi# Downloading artifactmkdir-p${BIN_FOLDER}ifhashcurl/dev/null21;thencurl${RELEASE_URL}-o${BIN_FOLDER}/micromamba-fsSL--compressed${CURL_OPTS:-}elifhashwget/dev/null21;thenwget${WGET_OPTS:-}-qO${BIN_FOLDER}/micromamba${RELEASE_URL}elseechoNeither curl nor wget was found2exit1fichmodx${BIN_FOLDER}/micromamba# Initializing shellcase$INIT_YESiny|Y|yes)case$(${BIN_FOLDER}/micromamba--version)in1.*|0.*)shell_arg-sprefix_arg-p;;*)shell_arg--shellprefix_arg--root-prefix;;esac${BIN_FOLDER}/micromambashell init$shell_arg$shell$prefix_arg$PREFIX_LOCATIONechoPlease restart your shell to activate micromamba or run the following:\necho source ~/.bashrc (or ~/.zshrc, ~/.xonshrc, ~/.config/fish/config.fish, ...);;*)echoYou can initialize your shell later by running:echo micromamba shell init;;esac# Initializing conda-forgecase$CONDA_FORGE_YESiny|Y|yes)${BIN_FOLDER}/micromambaconfig append channels conda-forge${BIN_FOLDER}/micromambaconfig append channels nodefaults${BIN_FOLDER}/micromambaconfigsetchannel_priority strict;;esac