关于docker:docker-打包-requirementstxt-变化之后自动下载新的包

3次阅读

共计 735 个字符,预计需要花费 2 分钟才能阅读完成。

子标题:

  • 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 镜像构建工夫

正文完
 0