一.以python为基础镜像
- Dockerfile写法:
FROM python:3.8.0
COPY ./smart_info_sim_project /code/smart_info_sim_project
COPY ./run /code/run
WORKDIR /code/smart_info_sim_project
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
RUN pip install -r requirements.txt
CMD ["python"."/code/smart_info_sim_project/app.py "]
- 与dockerfile同级别目录下执行命令:docker build -t test:v1.0
- 打包镜像为tar文件执行命令:docker save -o test.tar test:v1.0
- 解压tar运行镜像命令:docker load -i test.tar
- 运行启动docker服务的命令:
#!/bin/bash
echo "开始导入docker镜像"
docker load -i sim_gpu_v2.tar
echo "结束导入docker镜像"
sleep 2
chmod -R 777 $PWD/smart_info_sim_project/publish/
sleep 1
echo "开始部署docker容器"
sudo docker run --gpus all --restart=always --privileged=true --name smart_info_sim_gpu -itd --network="host" -e LANG=C.UTF-8 -v $PWD/smart_info_sim_project:/code/smart_info_sim_project -v $PWD/run:/code/run -w /code/smart_info_sim_project/publish test:v1.0 /bin/bash -c "./python_docker_start.sh;/bin/bash"
echo "结束部署docker容器"
其中:–gpus all:该服务需使用gpu添加命令;chmod命令给赋予权限,该文件夹下需存在启动脚本(python_docker_start.sh):
#!/bin/bash
nohup python -u /code/smart_info_sim_project/app.py > /code/run/run.log 2>&1 &
二. 以cuda作为基础镜像
- Dockerfile写法:
FROM nvidia/cuda:12.0.0-cudnn8-runtime-ubuntu20.04
# Copy project files
COPY ./smart_rerank_project /code/smart_rerank_project
COPY ./run /code/run
RUN chmod +x /code/run
# Install Miniconda
COPY ./Miniconda3-py38_23.11.0-2-Linux-x86_64.sh /code/Miniconda3-py38_23.11.0-2-Linux-x86_64.sh
# 确保脚本具有执行权限
RUN chmod +x /code/Miniconda3-py38_23.11.0-2-Linux-x86_64.sh
RUN /bin/bash /code/Miniconda3-py38_23.11.0-2-Linux-x86_64.sh -b -p /code/miniconda3 && \
rm /code/Miniconda3-py38_23.11.0-2-Linux-x86_64.sh && \
/code/miniconda3/bin/conda clean -all && \
ln -s /code/miniconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
echo ". /code/miniconda3/etc/profile.d/conda.sh" >> ~/.bashrc
# Set environment path
ENV PATH /code/miniconda3/bin:$PATH
# Create and activate conda environment
RUN conda create -n rerank_conda python=3.8.0 && \
echo "conda activate rerank_conda" >> ~/.bashrc
# Set up bashrc and update apt sources
RUN sed -i 's@/archive.ubuntu.com/@/mirrors.aliyun.com/@g' /etc/apt/sources.list && \
sed -i 's@/deb.debian.org/@/mirrors.aliyun.com/@g' /etc/apt/sources.list && \
apt-get clean && \
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7EA0A9C3F273FCD8 && \
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A4B469963BF863CC && \
apt-get update
# Install pip and dependencies
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --upgrade pip
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
RUN pip install -r /code/smart_rerank_project/requirements.txt
#RUN pip install /code/smart_rerank_project/torch-2.0.1+cu118-cp38-cp38-linux_x86_64.whl
注意:提前拉取docker镜像:
docker pull dockerproxy.cn/nvidia/cuda:12.0.0-cudnn8-runtime-ubuntu20.04
docker pull nvidia/cuda:12.0.0-cudnn8-runtime-ubuntu20.04
rerank_conda为conda环境的名字
2. python_docker_start.sh写法:
#!/bin/bash
# get the full path of this shell
dir=$(cd `dirname $BASH_SOURCE[0]`; pwd)
# get the parameter count
para_num=$#
# activate python env
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/root/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/root/miniconda3/etc/profile.d/conda.sh" ]; then
. "/root/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/root/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
conda activate rerank_conda
export PYTHONPATH=${dir}:$PYTHONPATH
# change current workdir to project root
cd ${dir}
nohup python -u /code/smart_rerank_project/app.py > /code/run/run.log 2>&1 &
这样打包的好处:使用显卡的时候不用考虑显卡版本过低的问题。
此时应注意,服务器驱动版本过低也会导致该镜像无法使用。