type
status
date
slug
summary
tags
category
icon
password
Last edited time
Mar 16, 2024 07:01 AM
😀
基于Nvidia官方镜像构建包含必要软件的Docker镜像,并创建相应容器进行深度网络的训练

📝 主旨内容

镜像内容

  • 安装当前CUDA版本下最新版本的Pytorch
  • 安装miniconda并更换为清华源
  • ubuntn软件源更换为清华源
  • pip更换成中科大源
  • 安装vim,tmux,wget,git,unzip,sudo等软件
  • 终端美化
    • notion image

一行命令,开箱即用

docker run -itd --gpus all --name gpu_container -p 8022:22 --shm-size 8G ziuch/gpu_image:cuda-11.8
💡
可根据本地驱动选择对应的cuda版本进行安装,本地运行nvidia-smi 查看最高支持的cuda版本(可选有cuda-12.1, cuda-11.8, cuda-11.6)

容器的操作

1. 进入容器

docker exec -it gpu_container /bin/bash
💡
也可以通过SSH工具连接进入,注意端口是8022,用户是ubuntu,密码是123(需要先进入容器启动ssh服务 sudo service ssh start)
ssh ubuntu@localhost -p 8022

2. 本地-容器文件拷贝

# 拷贝test.txt到gpu_container容器内的/home/ubuntu目录下 # 容器内文件拷贝出来只需要交换位置即可 docker cp test.txt gpu_container:/home/ubuntu

3. 退出容器

exit

4. 打包容器为镜像文件

4.1 基于容器创建镜像
docker commit gpu_container gpu_image:V2.0
4.2 将镜像导出成文件
docker save -o gpu_image_V2.tar gpu_image:V2.0

🤗 总结归纳

以下是通过Dockerfile构建镜像
以下压缩包包含全部所需文件,点击即可下载
wget https://file.ziuch.top/directlink/local/config/Docker.zip

运行脚本生成镜像(cuda:11.6.1-cudnn8-devel-ubuntu20.04)

💡
当前目录下需存在Dockerfile文件.condarc文件.tmux.conf文件.vimrc文件
docker build -t gpu_image:V1.0 .

镜像的创建(上面四个文件的内容 可选修改)

Dockerfile文件

# 基础镜像使用 Ubuntu 20.04 FROM nvidia/cuda:11.6.1-cudnn8-devel-ubuntu20.04 SHELL ["/bin/bash", "-o", "pipefail", "-c"] # 设置时区为上海 ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 ENV DEBIAN_FRONTEND noninteractive # 更新源并安装必要的包和软件 RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list && \ echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" >> /etc/apt/sources.list && \ echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list && \ echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \ echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \ echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \ echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \ echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \ apt-get update RUN apt-get install -y --no-install-recommends vim ssh sudo wget curl git vim bash-completion tmux python3 python3-pip unzip tree htop p7zip-full git-lfs tzdata libgl1-mesa-glx libglib2.0-0 && \ ln -s /usr/bin/python3 /usr/bin/python RUN useradd -m -s /bin/bash ubuntu && \ echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \ echo 'ubuntu:123' | chpasswd && \ mkdir /var/run/sshd && \ sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config && \ sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config && \ apt-get -y clean && \ rm -rf /var/lib/apt/lists/* ADD .condarc /root/.condarc ADD .tmux.conf /home/ubuntu/.tmux.conf ADD .vimrc /home/ubuntu/.vimrc USER ubuntu # 执行 sudo usermod -aG sudo ubuntu RUN sudo usermod -aG sudo ubuntu # 修改~/.bashrc文件,force_color_prompt=yes RUN sudo sed -i 's/#force_color_prompt=yes/force_color_prompt=yes/g' ~/.bashrc # 修改~/.bashrc文件,PS1变量 修改为:\[\033[35m\][\\t]\[\033[00m\] \[\033[36m\]\u@\h\[\033[00m\] [\w]\n\[\033[34m\]->\[\033[00m\] RUN sudo sed -i 's/PS1=.*$/PS1="\\n\\[\\033[35m\\][\\\\t]\\[\\033[00m\\] \\[\\033[36m\\]\\u@\\h\\[\\033[00m\\] [\\w]\\n\\[\\033[34m\\]->\\[\\033[00m\\] "/g' ~/.bashrc # 修改/etc/inputrc 使得pageup和pagedown可以使用 history 查找历史命令 RUN sudo sed -i 's/# "\e\[5~": history-search-backward/"\e\[5~": history-search-backward/g' /etc/inputrc RUN sudo sed -i 's/# "\e\[6~": history-search-forward/"\e\[6~": history-search-forward/g' /etc/inputrc # 修改~/.bashrc文件,alias gpu='watch -n 1 nvidia-smi' RUN echo "alias gpu='watch -n 1 nvidia-smi'" >> ~/.bashrc ENV PATH /home/ubuntu/miniconda3/bin:$PATH ADD .condarc /home/ubuntu/.condarc RUN wget --quiet https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py38_4.8.3-Linux-x86_64.sh -O ~/miniconda.sh && \ /bin/bash ~/miniconda.sh -b -p /home/ubuntu/miniconda3 && \ rm ~/miniconda.sh && \ /home/ubuntu/miniconda3/bin/conda clean -tipsy && \ echo ". /home/ubuntu/miniconda3/etc/profile.d/conda.sh" >> ~/.bashrc && \ echo "conda activate base" >> ~/.bashrc && \ find /home/ubuntu/miniconda3/ -follow -type f -name '*.a' -delete && \ find /home/ubuntu/miniconda3/ -follow -type f -name '*.js.map' -delete && \ /home/ubuntu/miniconda3/bin/conda clean -afyi # 更新 pip3 配置文件,使用国内源 RUN pip install --no-cache-dir -i https://mirrors.ustc.edu.cn/pypi/web/simple pip -U RUN pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple RUN sudo chown ubuntu /home/ubuntu/.condarc RUN conda install cudatoolkit=11.6 cudnn=8.4.1.50 -y -c conda-forge && conda clean -afy RUN pip install -i https://mirrors.ustc.edu.cn/pypi/web/simple --no-cache-dir matplotlib tensorboard scipy pandas scikit-learn numpy tqdm RUN mkdir /home/ubuntu/.agit && sudo mkdir /workspace && sudo chown -R ubuntu /workspace EXPOSE 22 WORKDIR /home/ubuntu CMD ["service ssh start && /bin/bash"]

.condarc文件

channels: - defaults show_channel_urls: true default_channels: - https://mirrors.bfsu.edu.cn/anaconda/pkgs/main - https://mirrors.bfsu.edu.cn/anaconda/pkgs/r - https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.bfsu.edu.cn/anaconda/cloud msys2: https://mirrors.bfsu.edu.cn/anaconda/cloud bioconda: https://mirrors.bfsu.edu.cn/anaconda/cloud menpo: https://mirrors.bfsu.edu.cn/anaconda/cloud pytorch: https://mirrors.bfsu.edu.cn/anaconda/cloud simpleitk: https://mirrors.bfsu.edu.cn/anaconda/cloud

.tmux.conf 文件

set-option -g status-keys vi setw -g mode-keys vi setw -g monitor-activity on # setw -g c0-change-trigger 10 # setw -g c0-change-interval 100 # setw -g c0-change-interval 50 # setw -g c0-change-trigger 75 set-window-option -g automatic-rename on set-option -g set-titles on set -g history-limit 100000 #set-window-option -g utf8 on # set command prefix set-option -g prefix C-a unbind-key C-b bind-key C-a send-prefix bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R bind -n M-Left select-pane -L bind -n M-Right select-pane -R bind -n M-Up select-pane -U bind -n M-Down select-pane -D bind < resize-pane -L 7 bind > resize-pane -R 7 bind - resize-pane -D 7 bind + resize-pane -U 7 bind-key -n M-l next-window bind-key -n M-h previous-window set -g status-interval 1 # status bar set -g status-bg black set -g status-fg blue #set -g status-utf8 on set -g status-justify centre set -g status-bg default set -g status-left " #[fg=green]#S@#H #[default]" set -g status-left-length 20 # mouse support # for tmux 2.1 # set -g mouse-utf8 on set -g mouse on # # for previous version #set -g mode-mouse on #set -g mouse-resize-pane on #set -g mouse-select-pane on #set -g mouse-select-window on #set -g status-right-length 25 set -g status-right "#[fg=green]%H:%M:%S #[fg=magenta]%a %m-%d #[default]" # fix for tmux 1.9 bind '"' split-window -vc "#{pane_current_path}" bind '%' split-window -hc "#{pane_current_path}" bind 'c' new-window -c "#{pane_current_path}" # run-shell "powerline-daemon -q" # vim: ft=conf

.vimrc 文件

" An example for a vimrc file. " " To use it, copy it to " for Unix and OS/2: ~/.vimrc " for Amiga: s:.vimrc " for MS-DOS and Win32: $VIM\_vimrc " for OpenVMS: sys$login:.vimrc " When started as "evim", evim.vim will already have done these settings. if v:progname =~? "evim" finish endif " Use Vim settings, rather then Vi settings (much better!). " This must be first, because it changes other options as a side effect. set nocompatible " allow backspacing over everything in insert mode set backspace=indent,eol,start if has("vms") set nobackup " do not keep a backup file, use versions instead else set backup " keep a backup file endif set history=50 " keep 50 lines of command line history set ruler " show the cursor position all the time set showcmd " display incomplete commands set incsearch " do incremental searching "========================================================================== "My Setting-sunshanlu "========================================================================== vmap <leader>y :w! /tmp/vitmp<CR> nmap <leader>p :r! cat /tmp/vitmp<CR> "语法高亮 syntax enable syntax on "显示行号 set nu "修改默认注释颜色 "hi Comment ctermfg=DarkCyan "允许退格键删除 "set backspace=2 "启用鼠标 set mouse=a set selection=exclusive set selectmode=mouse,key "按C语言格式缩进 set cindent set autoindent set smartindent set shiftwidth=4 " 允许在有未保存的修改时切换缓冲区 "set hidden " 设置无备份文件 set writebackup set nobackup "显示括号匹配 set showmatch "括号匹配显示时间为1(单位是十分之一秒) set matchtime=5 "显示当前的行号列号: set ruler "在状态栏显示正在输入的命令 set showcmd set foldmethod=syntax "默认情况下不折叠 set foldlevel=100 " 开启状态栏信息 set laststatus=2 " 命令行的高度,默认为1,这里设为2 set cmdheight=2 " 显示Tab符,使用一高亮竖线代替 set list "set listchars=tab:\|\ , set listchars=tab:>-,trail:- "侦测文件类型 filetype on "载入文件类型插件 filetype plugin on "为特定文件类型载入相关缩进文件 filetype indent on " 启用自动补全 filetype plugin indent on "设置编码自动识别, 中文引号显示 filetype on "打开文件类型检测 "set fileencodings=euc-cn,ucs-bom,utf-8,cp936,gb2312,gb18030,gbk,big5,euc-jp,euc-kr,latin1 set fileencodings=utf-8,gb2312,gbk,gb18030 "这个用能很给劲,不管encoding是什么编码,都能将文本显示汉字 "set termencoding=gb2312 set termencoding=utf-8 "新建文件使用的编码 set fileencoding=utf-8 "set fileencoding=gb2312 "用于显示的编码,仅仅是显示 set encoding=utf-8 "set encoding=utf-8 "set encoding=euc-cn "set encoding=gbk "set encoding=gb2312 "set ambiwidth=double set fileformat=unix "设置高亮搜索 set hlsearch "在搜索时,输入的词句的逐字符高亮 set incsearch " 着色模式 set t_Co=256 "colorscheme wombat256mod "colorscheme gardener "colorscheme elflord colorscheme desert "colorscheme evening "colorscheme darkblue "colorscheme torte "colorscheme default " 字体 && 字号 set guifont=Monaco:h10 "set guifont=Consolas:h10 " :LoadTemplate 根据文件后缀自动加载模板 "let g:template_path='/home/ruchee/.vim/template/' " :AuthorInfoDetect 自动添加作者、时间等信息,本质是NERD_commenter && authorinfo的结合 ""let g:vimrc_author='sunshanlu' ""let g:vimrc_email='sunshanlu@baidu.com' ""let g:vimrc_homepage='http://www.sunshanlu.com' " " " Ctrl + E 一步加载语法模板和作者、时间信息 ""map <c-e> <ESC>:AuthorInfoDetect<CR><ESC>Gi ""imap <c-e> <ESC>:AuthorInfoDetect<CR><ESC>Gi ""vmap <c-e> <ESC>:AuthorInfoDetect<CR><ESC>Gi " ======= 引号 && 括号自动匹配 ======= " " ":inoremap ( ()<ESC>i ":inoremap ) <c-r>=ClosePair(')')<CR> " ":inoremap { {}<ESC>i " ":inoremap } <c-r>=ClosePair('}')<CR> " ":inoremap [ []<ESC>i " ":inoremap ] <c-r>=ClosePair(']')<CR> " ":inoremap < <><ESC>i " ":inoremap > <c-r>=ClosePair('>')<CR> " "":inoremap " ""<ESC>i " ":inoremap ' ''<ESC>i " ":inoremap ` ``<ESC>i " ":inoremap * **<ESC>i " 每行超过80个的字符用下划线标示 ""au BufRead,BufNewFile *.s,*.asm,*.h,*.c,*.cpp,*.java,*.cs,*.lisp,*.el,*.erl,*.tex,*.sh,*.lua,*.pl,*.php,*.tpl,*.py,*.rb,*.erb,*.vim,*.js,*.jade,*.coffee,*.css,*.xml,*.html,*.shtml,*.xhtml Underlined /.\%81v/ " " " For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries " let &guioptions = substitute(&guioptions, "t", "", "g") " Don't use Ex mode, use Q for formatting map Q gq " This is an alternative that also works in block mode, but the deleted " text is lost and it only works for putting the current register. "vnoremap p "_dp " Switch syntax highlighting on, when the terminal has colors " Also switch on highlighting the last used search pattern. if &t_Co > 2 || has("gui_running") syntax on set hlsearch endif " Only do this part when compiled with support for autocommands. if has("autocmd") " Enable file type detection. " Use the default filetype settings, so that mail gets 'tw' set to 72, " 'cindent' is on in C files, etc. " Also load indent files, to automatically do language-dependent indenting. filetype plugin indent on " Put these in an autocmd group, so that we can delete them easily. augroup vimrcEx au! " For all text files set 'textwidth' to 80 characters. autocmd FileType text setlocal textwidth=80 " When editing a file, always jump to the last known cursor position. " Don't do it when the position is invalid or when inside an event handler " (happens when dropping a file on gvim). autocmd BufReadPost * \ if line("'\"") > 0 && line("'\"") <= line("$") | \ exe "normal g`\"" | \ endif augroup END else set autoindent " always set autoindenting on endif " has("autocmd") " 增加鼠标行高亮 set cursorline hi CursorLine cterm=NONE ctermbg=darkred ctermfg=white " 设置tab是四个空格 set ts=4 set expandtab " 主要给Tlist使用 let Tlist_Exit_OnlyWindow = 1 let Tlist_Auto_Open = 1

📎 参考文章

 
曹云康——华中科技大学2020级博士Docker常用命令
Loading...
ziuch
ziuch
一个普通的干饭人🍚
最新发布
CFM的踩坑指南
2025-3-18
AnomalyPainter——合理,真实,多样我全都要
2025-3-13
拉链异常检测项目
2025-3-11
FastGPT——构建你自己的GPT
2025-3-11
Git常用命令
2025-3-11
MMAD——为大模型出的异常检测试卷
2025-3-6
公告