1:创建Docker文件
首先,你需要在你的项目根目录下创建一个名为 Dockerfile的文件。这个文件将包含构建你的 Docker 镜像所需的指令。
以我目前在构建的一个项目为例,它的Dockerfile是这样的:
FROM nvidia/cuda:11.0.3-cudnn8-devel-ubuntu20.04
ENV NVIDIA_VISIBLE_DEVICES=all
ENV PATH="/root/miniconda3/bin:${PATH}"
ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility
RUN apt-get upgrade && apt-get update -y
RUN apt-get install -y wget && rm -rf /var/lib/apt/lists/*
RUN wget \
https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& mkdir /root/.conda \
&& bash Miniconda3-latest-Linux-x86_64.sh -b \
&& rm -f Miniconda3-latest-Linux-x86_64.sh
RUN conda create -n envd python=3.8.8
ENV ENVD_PREFIX=/root/miniconda3/envs/envd/bin
RUN update-alternatives --install /usr/bin/python python ${ENVD_PREFIX}/python 1 && \
update-alternatives --install /usr/bin/python3 python3 ${ENVD_PREFIX}/python3 1 && \
update-alternatives --install /usr/bin/pip pip ${ENVD_PREFIX}/pip 1 && \
update-alternatives --install /usr/bin/pip3 pip3 ${ENVD_PREFIX}/pip3 1
RUN apt-get upgrade && apt-get update -y
RUN DEBIAN_FRONTEND="noninteractive" apt-get install -y python3-opencv git
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
ENV TORCH_CUDA_ARCH_LIST=8.0
RUN pip install 'git+https://github.com/facebookresearch/detectron2.git'
WORKDIR /workspace
COPY . .
CMD ["python", "main.py"]
2:有了上面这个文件以后开始用命令构建镜像
docker build -t your-image-name:tag .
(特别注意 这里有个'.',表示Dockerfile所在的当前目录)
然后就开始啦,这个过程可能有点久
3:等待好了以后,我们验证镜像是否构建成功
docker images
然后就可以创建容器拉去镜像使用了!
如有问题请评论,大家一起交流呀~