Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dockerfile-缓存包以更快地重建?_Docker_Flask_Dockerfile - Fatal编程技术网

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

我有一个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 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"]