怎么用Docker文件构建Docker镜像

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

然后就可以创建容器拉去镜像使用了!

如有问题请评论,大家一起交流呀~

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值