Dockerfile-缓存包以更快地重建?
我有一个Dockerfile,它构建了一个RESTApi。每当我更改代码中的一行代码并希望重建容器时,都会从0安装所有内容(代码位于应用程序文件夹中)。这是我的Dockerfile:Dockerfile-缓存包以更快地重建?,docker,flask,dockerfile,Docker,Flask,Dockerfile,我有一个Dockerfile,它构建了一个RESTApi。每当我更改代码中的一行代码并希望重建容器时,都会从0安装所有内容(代码位于应用程序文件夹中)。这是我的Dockerfile: # Use the Python3.7.2 image FROM python:3.7.2-stretch # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the contai
# Use the Python3.7.2 image
FROM python:3.7.2-stretch
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# grab the key that LLVM use to GPG-sign binary distributions
RUN apt-get update && \
apt-get install -y software-properties-common && \
rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y apt-utils && apt-get install -y curl
RUN apt -y install clang
# Install Mono (careful: Docker image runs on Debian 9)
RUN apt -y install apt-transport-https dirmngr gnupg ca-certificates
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
RUN echo "deb https://download.mono-project.com/repo/debian stable-stretch main" | tee /etc/apt/sources.list.d/mono-official-stable.list
RUN apt update
RUN apt -y install mono-complete
## Install python libraries
RUN apt-get -y install python3-pip
RUN pip install -r requirements.txt
RUN mono /app/pythonnet/tools/nuget/nuget.exe update -self
RUN git config --global http.proxy http://my.proxy.address:port
RUN /usr/bin/python3 -m pip install -U pycparser --user
RUN /usr/bin/python3 -m pip install -U pythonnet --user
# run the command to start uWSGI
CMD ["uwsgi", "app.ini"]
构建此映像需要很长时间,而且由于我只更改了几行代码,因此完全重建是一种不希望出现的行为
我运行
docker build-t flask\u应用程序来构建图像。是否只重新安装某些只在我的代码库中进行更改的软件包?docker build
使用一种可以避免重建部分映像的方法。这里的基本规则是,只要复制
(或添加
)已更改的文件,Dockerfile中该行之后的所有内容都将始终重复。在执行COPY
指令之前,或者如果Docker能够告知正在执行COPY
ed的文件没有更改,则将跳过这些内容(重用上一版本中的层)
为了支持这一点,典型的Dockerfile布局如下:
FROM some-base-image
RUN commands to install OS-level dependencies
WORKDIR /app
COPY only files that declare language-level dependencies ./
RUN commands to install language-level dependencies
COPY . ./
RUN commands to build the application
CMD the single command to launch the application
注意两个独立的COPY
命令。如果RUN
命令的文本没有更改,则重用操作系统级依赖项;如果语言级别的依赖项的特定文件没有更改,则重用它们;而应用程序本身通常都是重建的
在Dockerfile的上下文中,您应该在复制任何内容之前运行所有的RUN apt get…
命令。(最好只使用一个apt get install
;重要的是,它必须与apt get update
处于同一个命令中),然后只复制requirements.txt
文件并运行pip install
;然后复制应用程序的其余部分
FROM python:3.7.2-stretch
# Rearrange this so there is only one apt-get install line
# (Do you need all of these dependencies?)
RUN apt-get update && \
apt-get install -y \
apt-transport-https \
apt-utils \
ca-certificates \
clang \
curl \
dirmngr \
gnupg \
software-properties-common
# Similarly install Mono (is it actually needed?)
# Copy and install just Python dependencies
WORKDIR /app
COPY requirements.txt .
# There are no requirements that aren't in the requirements.txt file
RUN pip install -r requirements.txt
# Copy the rest of the app in
COPY . .
# Standard runtime metadata
EXPOSE 8000
CMD ["uwsgi", "app.ini"]