docker 打包 requirements.txt 变化之后自动下载新的包

子标题:

  • docker 打包 requirements.txt 变化之后自动下载新的包 python
  • docker 判断 requirements.txt 是否发生变化 python
  • requirements.txt 变化自动更新 python

使用下面的 dockerfile 就好了:

FROM python:3.9-buster
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN (/usr/local/bin/python -m pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple) && (pip install -i https://mirrors.aliyun.com/pypi/simple -r requirements.txt)
COPY . /code/

关键是这行:COPY requirements.txt /code/
requirements.txt 发生变化的时候(通过文件的 hashcode 判断),COPY 命令就会丢弃之前的 cache 镜像,重新打包,因为 docker 的镜像之间是前后关系,后面的镜像也都会 cache 失效,所以重新执行 RUN (/usr/local/bin/python -m pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple) && (pip install -i https://mirrors.aliyun.com/pypi/simple -r requirements.txt)COPY . /code/

参考文章:
利用构建缓存机制缩短Docker镜像构建时间

你可能感兴趣的:(dockerpython)