Dockerfile快速启动

# 指定镜像
FROM ubuntu:20.04
# 设置环境变量名称
ENV LANG="C.UTF-8"
# 执行命令
RUN apt update
# 拷贝文件进容器指定位置
COPY cmake-3.28.3.tar.gz /root/
# 使用出错暂时未知
# CMD ["source", "/root/.bashrc"]
# 执行docker build根据dockerfile快速构建镜像
docker build -f dockerfile路径 -t 建好的镜像名 如有复制文件指定文件路径

ssh

# 指定镜像
FROM ubuntu:20.04
# 支持中文
ENV LANG="C.UTF-8"
# 设置时区, 以防某些软件需要确定时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    echo "$TZ" > /etc/timezone
    
# 安装ssh并设置自动启动脚本
COPY install_ssh write_ssh /root/
RUN cat /root/install_ssh | bash && \
    rm -rf /root/install_ssh

install_ssh

# install_ssh

# 安装依赖
apt update
apt install openssh-client openssh-server -y

# 写入配置
cat /root/write_ssh | bash

# 启动应用
/etc/init.d/ssh start

# 收尾
rm -rf /root/write_ssh


write_ssh

# write_ssh

# 允许root登录
echo -e '\n
Port 20000
PermitRootLogin yes
PasswordAuthentication yes
' >> /etc/ssh/sshd_config

# 自动启动应用脚本
echo -e '
import os
ssh_result = os.popen("service ssh status")
if "not running" in ssh_result.readlines()[0]:
    os.system("service ssh start")
' >> /root/.start.py

# 将脚本加入bashrc
echo -e '
python3 /root/.start.py
' >> /root/.bashrc

# 设置root密码
echo -e 'root:why_hhu_2024' | chpasswd

# 对未知ip不再出现检查确认
## 取原有ssh_config过滤掉有StrictHostKeyChecking的那一行并生成新的ssh_config
cat /etc/ssh/ssh_config | grep -v StrictHostKeyChecking > /etc/ssh/ssh_config.new 
## 在新的ssh_config中加入不对未知ip进行检查这句
echo '
    StrictHostKeyChecking no
' >> /etc/ssh/ssh_config.new
## 用修改后的ssh_config覆盖掉原有的ssh_config 
mv /etc/ssh/ssh_config.new /etc/ssh/ssh_config

cmake

cmake官网下载

# 安装cmake
COPY ./depend_file/cmake-3.28.3.tar.gz \
     install_cmake \
     /root/
RUN cat /root/install_cmake | bash && \
    rm -rf /root/install_cmake

install_cmake

# install_cmake

# 安装依赖
apt update
apt-get install g++ build-essential libssl-dev -y

# 解压在指定位置
tar -zxvf cmake-3.28.3.tar.gz -C /usr/local/src/

# 编译
cd /usr/local/src/cmake-3.28.3/
./bootstrap
make
make install

# 收尾
rm -rf /root/cmake-3.28.3.tar.gz

python

# 注意! 需要在ssh前安装! 不然自动启动ssh的脚本会缺少依赖!
# 依赖cmake
# 安装python3.10
COPY ./depend_file/Python-3.10.0.tgz \
     install_python \
     write_python \
     /root/
RUN cat /root/install_python | bash && \
    rm -rf /root/install_python

install_python

# install_python

# 卸载ubuntu20.04自带的python3.8
apt remove python3 python3-minimal python3.8 python3.8-minimal
rm -rf /usr/lib/python3.8 /usr/bin/python3 /etc/python3 /etc/python3.8

# 安装依赖的静态库
apt update
apt install zlib1g-dev libffi-dev libbz2-dev liblzma-dev libsqlite3-dev libncurses5 libncurses5-dev libncursesw5 -y

# 更新库
ldconfig
cd

# 解压到指定位置
tar zxvf Python-3.10.0.tgz -C /usr/local/src/

# 编译安装
cd /usr/local/src/Python-3.10.0
./configure --enable-optimizations --with-ssl --prefix=/usr/local/python-3.10.0 && make -j 12 && make install

# 写入配置
cat /root/write_python | bash

# 收尾
rm -rf /root/write_python
rm -rf /root/Python-3.10.0.tgz


write_python

# write_python

# 写入环境变量
echo '
PATH=/usr/local/python-3.10.0/bin:$PATH
alias python=/usr/local/python-3.10.0/bin/python3.10
alias pip=pip3
' >> ~/.bashrc

wireguard

# docker容器启动时需要加上--cap-add NET_ADMIN
# 需要挂载容器内的/etc/wireguard目录, 并确保挂载的目录下有名为wg0.conf的文件, 否则wireguard的自动启动不生效
# 依赖python和自动启动脚本
# 安装wireguard
COPY install_wireguard \
     /root/
RUN cat /root/install_wireguard | bash && \
    rm -rf /root/install_wireguard

install_wireguard

# install_wireguard

# 安装依赖
apt update
apt install iproute2 wireguard -y

# 配置开机自启
cat /root/write_wireguard | bash

# 收尾
rm -rf /root/write_wireguard


write_wireguard

# write_wireguard

echo -e '
import os
if os.path.exists("/etc/wireguard/wg0.conf"):
    ssh_result = os.popen("wg")
    if len("".join(ssh_result.readlines())) == 0:
        os.system("wg-quick up wg0")
' >> /root/.start.py

CUDA

# 依赖python
# 安装CUDA, 仅适用于ubuntu20.04版本
COPY ./depend_file/cuda-ubuntu2004.pin \
     ./depend_file/cuda-repo-ubuntu2004-12-8-local_12.8.1-570.124.06-1_amd64.deb \
     ./install_cuda \
     ./write_cuda \
     /root/
RUN cat /root/install_cuda | bash && \
    rm -rf /root/install_cuda

install_cuda

# install_cuda

# 获取cuda库密钥
mv /root/cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
# 安装cuda依赖包
dpkg -i /root/cuda-repo-ubuntu2004-12-8-local_12.8.1-570.124.06-1_amd64.deb
# 运行apt访问nvidia的库下载软件
cp /var/cuda-repo-ubuntu2004-12-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
apt-get update
# 安装cuda
apt-get -y install cuda-toolkit-12-8

# 写入配置
cat /root/write_cuda | bash

# 收尾
rm -rf /root/write_cuda
rm -rf /root/cuda-ubuntu2004.pin
rm -rf /root/cuda-repo-ubuntu2004-12-8-local_12.8.1-570.124.06-1_amd64.deb


write_cuda

# write_cuda

# 写入环境变量
echo '
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:/usr/local/cuda/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64
' >> ~/.bashrc

cudnn

# 依赖CUDA, 但对操作系统没有依赖
# cudnn安装
COPY ./depend_file/cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz \
     install_cudnn \
     /root/
RUN cat /root/install_cudnn | bash && \
    rm -rf /root/install_cudnn

install_cudnn

# install_cudnn

# 解压文件
tar -xvf /root/cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz -C /usr/local/src

# 复制到cuda下
cp /usr/local/src/cudnn-linux-x86_64-8.9.7.29_cuda12-archive/lib/*     /usr/local/cuda/lib64/
cp /usr/local/src/cudnn-linux-x86_64-8.9.7.29_cuda12-archive/include/* /usr/local/cuda/include/

# 给予权限
chmod a+r /usr/local/cuda/lib64/*
chmod a+r /usr/local/cuda/*

# 收尾
rm -rf /root/install_cudnn
rm -rf /root/cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz

nccl

# 安装nccl, 似乎不需要安装cmake?
COPY ./depend_file/nccl_git.tar.gz \
     install_nccl \
     write_nccl \
     /root/
RUN cat /root/install_nccl | bash && \
    rm -rf /root/install_nccl

install_nccl

# install_nccl

# 解压安装包
tar -zxvf /root/nccl_git.tar.gz -C /usr/local/src

# 编译
cd /usr/local/src/nccl
make -j12 src.build BUILDDIR=/usr/local/nccl CUDA_HOME=/usr/local/cuda

# 写入配置
cat /root/write_nccl | bash

# 收尾
rm -rf /root/write_nccl
rm -rf /root/nccl_git.tar.gz


write_nccl

# write_nccl

echo '
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/nccl/lib
export PATH=$PATH:/usr/local/nccl/bin
' >> ~/.bashrc

torch

# 依赖python
# 安装torch
COPY ./depend_file/torch_whl \
     install_torch \
     /root/
RUN cat /root/install_torch | bash && \
    rm -rf /root/install_torch

install_torch

# install_torch

# 指定pip位置
PATH=/usr/local/python-3.10.0/bin:$PATH

# 安装安装包
pip3 install /root/torch_whl/*.whl

# 收尾
rm -rf /root/torch_whl

ignite

# 依赖torch
# 安装ignite
COPY ./depend_file/ignite_whl \
     install_ignite \
     /root/
RUN cat /root/install_ignite | bash && \
    rm -rf /root/install_ignite

install_ignite

# install_ignite

# 指定环境变量
PATH=/usr/local/python-3.10.0/bin:$PATH

# 安装安装包
pip3 install /root/*.whl

# 收尾
rm -rf /root/torch_whl

TCL

TCL官网下载

# 依赖: cmake
# 安装TCL
COPY tcl8.6.14-src.tar.gz install_tcl /root/
RUN cat /root/install_tcl | bash && \
    rm -rf /root/install_tcl

install_tcl

# install_tcl

# 解压tcl包
tar -zxvf /root/tcl8.6.14-src.tar.gz -C /usr/local/src/

# 编译安装
cd /usr/local/src/tcl8.6.14/unix/
./configure
make
make install

# 收尾
rm -rf /root/tcl8.6.14-src.tar.gz

expect

# 依赖: cmake tcl
# 安装expect
COPY expect5.45.tar.gz install_expect /root/
RUN cat /root/install_expect | bash && \
    rm -rf /root/install_expect

install_expect

# install_expect

# 解压压缩包
tar -zxvf /root/expect5.45.tar.gz -C /usr/local/src/
# 编译安装
cd /usr/local/src/expect5.45/
./configure --with-tclinclude=/usr/local/src/tcl8.6.14/generic/ --with-tclconfig=/usr/local/lib/
make
make install

# 收尾
rm -rf /root/expect5.45.tar.gz

nodejs

# 依赖: cmake
# 安装nodejs
COPY node-v20.12.0.tar.gz install_nodejs /root/
RUN cat /root/install_nodejs | bash && \
    rm -rf /root/install_nodejs

install_nodejs

# install_nodejs

# 安装依赖
apt update
apt install g++ curl libssl-dev apache2-utils git-core build-essential python3-distutils sudo -y

# 解压文件
tar -zxvf /root/node-v20.12.0.tar.gz -C /usr/local/src/

# 编译安装
cd /usr/local/src/node-v20.12.0/
./config
make
make install

# 收尾
rm -rf /root/node-v20.12.0.tar.gz

nginx

nginx官网下载

# 依赖: cmake
# 安装nginx
COPY ./depend_file/nginx-1.24.0.tar.gz \
     install_nginx \
     /root/
RUN cat /root/install_nginx | bash && \
    rm -rf /root/install_nginx

install_nginx

# install_nginx

# 安装依赖
apt update
apt-get install libpcre3 libpcre3-dev openssl zlib1g zlib1g-dev -y

# 解压文件
tar -zxvf /root/nginx-1.24.0.tar.gz -C /usr/local/src/

# 编译安装
cd /usr/local/src/nginx-1.24.0/
./configure
make
make install

# 收尾
rm -rf /root/nginx-1.24.0.tar.gz

nginx_rtmp

rtmp推流: nginx-rtmp-module

# 依赖: cmake
# 安装nginx_rtmp内核
COPY ./depend_file/nginx-1.24.0.tar.gz ./depend_file/nginx-rtmp-module-master.zip ./depend_file/nginx_rtmp.conf \
     write_nginx_rtmp_setting install_nginx_rtmp \
     /root/
RUN cat /root/install_nginx_rtmp | bash && \
    rm -rf /root/install_nginx_rtmp

install_nginx_rtmp

# install_nginx_rtmp

# 安装依赖
apt update
apt-get install unzip libpcre3 libpcre3-dev openssl zlib1g zlib1g-dev -y

# 解压文件
tar -zxvf /root/nginx-1.24.0.tar.gz -C /usr/local/src/
unzip /root/nginx-rtmp-module-master.zip -d /usr/local/src/

# 编译安装
cd /usr/local/src/nginx-1.24.0/
./configure --add-module=../nginx-rtmp-module-master/
make
make install

# 配置文件及nginx自启
cat /root/write_nginx_rtmp_setting | bash

# 收尾
rm -rf /root/nginx-1.24.0.tar.gz
rm -rf /root/nginx-rtmp-module-master.zip
rm -rf /root/write_nginx_rtmp_setting

write_nginx_rtmp_setting

# write_nginx_rtmp_setting

# nginx配置文件
mv /root/nginx_rtmp.conf /usr/local/nginx/conf/nginx.conf

# 自动启动nginx
echo -e '
import os
if "nginx: worker process" not in "".join(os.popen("ps aux | grep nginx").readlines()):
    os.system("/usr/local/nginx/sbin/nginx")
' >> /root/.start.py

nginx_http-flv

# 依赖: cmake
# 安装nginx_http-flv内核
COPY ./depend_file/nginx-1.24.0.tar.gz ./depend_file/nginx-http-flv-module-1.2.11.tar.gz ./depend_file/nginx_http-flv.conf \
     write_nginx_http-flv_setting install_nginx_http-flv \
     /root/
RUN cat /root/install_nginx_http-flv | bash && \
    rm -rf /root/install_nginx_http-flv

install_nginx_http-flv

# install_nginx_http-flv

# 安装依赖
apt update
apt-get install libpcre3 libpcre3-dev openssl zlib1g zlib1g-dev -y

# 解压文件
tar -zxvf /root/nginx-1.24.0.tar.gz                 -C /usr/local/src/
tar -zxvf /root/nginx-http-flv-module-1.2.11.tar.gz -C /usr/local/src/

# 编译安装
cd /usr/local/src/nginx-1.24.0/
./configure --add-module=../nginx-http-flv-module-1.2.11/ --with-http_ssl_module 
make
make install

# 配置文件及nginx自启
cat /root/write_nginx_http-flv_setting | bash

# 收尾
rm -rf /root/nginx-1.24.0.tar.gz
rm -rf /root/nginx-http-flv-module-1.2.11.tar.gz
rm -rf /root/write_nginx_http-flv_setting

write_nginx_http-flv_setting

# write_nginx_http-flv_setting

# nginx配置文件
mv /root/nginx_http-flv.conf /usr/local/nginx/conf/nginx.conf

# 自动启动nginx
echo -e '
import os
if "nginx: worker process" not in "".join(os.popen("ps aux | grep nginx").readlines()):
    os.system("/usr/local/nginx/sbin/nginx")
' >> /root/.start.py

Django

# django安装
COPY install_django /root/
RUN cat /root/install_django | bash && \
    rm -rf /root/install_django

install_django

# install_django

# 安装依赖
apt update -y
apt-get install python3-setuptools python3-pip redis-server python3-mysqldb libmysqlclient-dev pkg-config python3-dev mysql-client zip unzip -y

# 安装django
pip3 install django django_redis django-cors-headers

mysql

COPY install_mysql write_file_mysql /root/
RUN cat /root/install_mysql write_file_mysql | bash && \
    rm -rf /root/install_mysql

install_mysql

# install_mysql

# 安装依赖
apt update
apt install mysql-server -y

# 写入配置
cat /root/write_file_mysql | bash

# 初始化
service mysql stop
usermod -d /var/lib/mysql/ mysql
service mysql restart
cat /root/mysql_opt1 | mysql
cat /root/mysql_opt2 | mysql_secure_installation

# 收尾
rm -rf /root/write_file /root/mysql_opt*

write_file_mysql

# write_file_mysql

# 设置mysql自动启动
echo -e '
import os\n
mysql_result = os.popen("service mysql status").read()\n
if "is stopped" in mysql_result:\n
    os.system("service mysql start")\n
' >> /root/start.py

# mysql允许远程登录
echo -e '
port = 3306\n
bind-address = 0.0.0.0\n
mysqlx-bind-address = 0.0.0.0\n
' >> /etc/mysql/mysql.conf.d/mysqld.cnf

# 设置mysql密码
echo -e '
ALTER USER "root"@"localhost" IDENTIFIED WITH mysql_native_password by "database_password";\n
use mysql;\n
update user set host = "%" where User = "root";\n
' >> /root/mysql_opt1

# 初始化 ?应该大概或许可能貌似也许是成功运行的吧?
echo -e "A3MYSQL\nn\nn\nn\nn\nn\nn\nn" >> /root/mysql_opt2

mmdetection3

注意!现在已经可以直接--gpus all导入镜像了, 无非再重新编译一遍ncll, 该教程已老旧

# 直接调NVIDIA提供的docker镜像
FROM nvidia/cuda:11.0.3-cudnn8-devel-ubuntu20.04
COPY mmdetection3.zip install_mmdet3 write_file_mmdet3 /root/
RUN cat /root/install_mmdet3 | bash && \
    rm -rf /root/install_mmdet3

install_mmdet3

# install_mmdet3

# 安装依赖
apt update
apt install tmux python3-pip python3-venv axel libxml2 zip unzip -y

# 指定显卡驱动位置?
cat /root/write_file_mmdet3 | bash

# 下载并安装运行驱动?
axel -n 50 -o /root/cuda_linux.run https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
chmod u+x /root/cuda_linux.run
/root/cuda_linux.run --silent --toolkit

# 在虚拟环境中配置安装mmdetection3所需要的包
python3 -m venv /root/mmdet3
source /root/mmdet3/bin/activate
pip install pip==19.4 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install torch==1.13.0 torchvision==0.14.0 torchaudio==0.13.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install openmim setuptools -i https://pypi.tuna.tsinghua.edu.cn/simple
mim install mmcv -i https://pypi.tuna.tsinghua.edu.cn/simple

# 安装mmdetection3
unzip /root/mmdetection3.zip -d /usr/local/src/
cd /usr/local/src/mmdetection3
pip install -v -e. -i https://pypi.tuna.tsinghua.edu.cn/simple

# 收尾
rm -rf /root/write_file_mmdet3
rm -rf /root/cuda_linux.run
rm -rf /root/mmdetection3.zip

write_file_mmdet3

# write_file_mmdet3

# 配置环境
echo -e '\n
export PATH=/usr/local/cuda/bin:$PATH\n
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH\n
' >> /root/.bash

nvm

# 安装nvm
COPY install_nvm.sh install_nvm /root/
RUN cat /root/install_nvm | bash && \
    rm -rf /root/install_nvm

install_nvm

# install_nvm

# 更新依赖
apt update

# 运行脚本
bash /root/install_nvm.sh

echo -e '
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
' >> ~/.bashrc

# 收尾
rm -rf /root/install_nvm.sh

nodejs(nvm)

# 依赖: nvm
# nvm安装nodejs
COPY install_nvm_nodejs /root/
RUN cat /root/install_nvm_nodejs | bash && \
    rm -rf /root/install_nvm_nodejs

install_nodejs

# install_nvm_nodejs

# 安装依赖
apt update

# 指定nvm位置
export NVM_DIR="$HOME/.nvm" 
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm 
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion 
nvm install --lts
# nvm install v16.20.2

go

# 安装golang
COPY ./depend_file/go1.22.2.linux-amd64.tar.gz install_go /root/
RUN cat /root/install_go | bash && \
    rm -rf /root/install_go

install_go

# install_go

# 解压
tar -zxvf /root/go1.22.2.linux-amd64.tar.gz -C /usr/local/

# 写入配置
echo -e '
export PATH=$PATH:/usr/local/go/bin
'>> /etc/profile

# 收尾
rm -rf /root/go1.22.2.linux-amd64.tar.gz

gitea

# 依赖: cmake nvm nodejs go

# gitea不能在root用户下运行
# 添加用户运行gitea
COPY gitea_add_user /root/
RUN cat /root/gitea_add_user | bash && \
    rm -rf /root/gitea_add_user

# 安装gitea
COPY ./depend_file/gitea-src-1.22.0-rc0.tar.gz install_gitea /home/gitea/
RUN cat /home/gitea/install_gitea | bash && \
    rm -rf /home/gitea/install_gitea

gitea_add_user

# gitea_add_user

# 安装依赖
apt update
apt install git sudo -y

# 创建gitea用户
sudo useradd -m gitea
# 增加gitea用户sudo权限
sudo usermod -aG sudo gitea
# 更改gitea用户密码
echo -e "gitea:gitea" | chpasswd

# 设置密码方便sudo调用
sudo -u gitea -s
echo "gitea" >> ~/.passwd

# 写入部分配置进.bashrc
# nvm的脚本位置
echo -e '
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
' >> ~/.bashrc
# go的运行文件位置
echo -e '
export PATH=$PATH:/usr/local/go/bin
' >> ~/.bashrc

install_gitea

# install_gitea

# 解压文件
sudo -u gitea -s
tar -zxvf ~/gitea-src-1.22.0-rc0.tar.gz -C ~/

# 指定go位置
export PATH=$PATH:/usr/local/go/bin

# 指定nvm位置
export NVM_DIR="$HOME/.nvm" 
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm 
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion 

# 编译
cd ~/gitea-src-1.22.0-rc0/
TAGS="bindata sqlite sqlite_unlock_notify" make build

# 收尾
rm -rf ~/gitea-src-1.22.0-rc0.tar.gz

ffmpeg

ffmpeg官网

apt install yasm pkgconf -y
# 解压
tar -xvf ffmpeg-6.1.1.tar.xz

# 安装h264支持
git clone https://code.videolan.org/videolan/x264.git
cd x264
./configure --disable-asm --enable-shared --enable-pic
make

apt-get install x264 libx264-dev -y
# 软链接
ln -s /usr/local/ffmpeg/bin/ffmpeg /usr/bin/ffmpeg
ln -s /usr/local/ffmpeg/bin/ffprobe /usr/bin/ffprobe
# 配置依赖
vim /etc/ld.so.conf
/usr/local/ffmpeg/lib
ldconfig #更新环境变量
# 依赖: cmake

待记录

最后修改:2025 年 03 月 19 日
赛博讨口子