如何更快地构建Docker映像 [英] How to build Docker images quicker
问题描述
我目前正在构建一个docker映像,并运行容器以在其中针对我正在处理的Python应用程序运行一些测试。当前,Dockerfile从主机复制文件,将工作目录设置为那些复制的文件,运行 sudo apt-get
并安装pip,最后从以下位置运行测试 setup.py
。可以在下面看到Dockerfile。
I'm currently building a docker image and running the container to run some tests in it for a Python application I'm working on. Currently the Dockerfile copies the files over from the host machine, sets the working directory to those copied files, runs a sudo apt-get
and installs pip, and finally runs the tests from setup.py
. The Dockerfile can be seen below.
FROM ubuntu
ADD . /home/dev/ProjectName
WORKDIR /home/dev/ProjectName
RUN apt-get update && \
apt-get install -y python3-pip && \
python3 setup.py test
我很好奇是否有更常规的方法避免每次我想运行 apt-get
和 apt-get install pip
时测试。我的主要想法是构建一个上面已经有pip的图像,然后从该图像构建该图像。
I was curious if there were a more conventional way to avoid having to run the apt-get
and apt-get install pip
every time I'd like to run a test. The main idea I had was to build an image with pip already on it, and then build this image from that one.
推荐答案
Docker尽可能使用缓存层进行构建。通过添加已更改的文件,它将使所有后续规则的缓存无效。首先输入apt命令,这些命令仅在您第一次构建时运行。有关更多信息,请参见此博客。
Docker builds using cached layers if it can. By adding files you have changed it invalidates the cache for all subsequent rules. Put the apt commands first and those will only be run the first time you build. See this blog for more info.
这篇关于如何更快地构建Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!