如何使用缓存快速重建 dockerfile? [英] How to rebuild dockerfile quick by using cache?
问题描述
我想优化我的 Dockerfile.我希望将缓存文件保存在磁盘中.但是,我发现当我运行 docker build .
它总是尝试从网络获取每个文件.
I want to optimize my Dockerfile. And I wish to keep cache file in disk.
But, I found when I run docker build .
It always try to get every file from network.
我希望在构建期间共享我的缓存目录(例如/var/cache/yum/x86_64/6).但是,它只适用于 docker run -v ...
.
I wish to share My cached directory during build (eg. /var/cache/yum/x86_64/6).
But, it works only on docker run -v ...
.
有什么建议吗?(在这个例子中,只安装了 1 个 rpm,在实际情况下,我需要安装数百个 rpm)
Any suggestion?(In this example, only 1 rpm installed, in real case, I require to install hundreds rpms)
我的 Dockerfile 草案
My draft Dockerfile
FROM centos:6.4
RUN yum update -y
RUN yum install -y openssh-server
RUN sed -i -e 's:keepcache=0:keepcache=1:' /etc/yum.conf
VOLUME ["/var/cache/yum/x86_64/6"]
EXPOSE 22
第二次,我想构建一个类似的图像
At second time, I want to build a similar image
FROM centos:6.4
RUN yum update -y
RUN yum install -y openssh-server vim
我不希望再次从 internat 获取 openssh-server(它很慢).在我的实际情况下,它不是一个包,大约是 100 个包.
I don't want the fetch openssh-server from internat again(It is slow). In my real case, it is not one package, it is about 100 packages.
推荐答案
更新以前的答案,当前 docker 构建接受 --build-arg
传递环境变量,如 http_proxy
而不将其保存在结果图像中.
An update to previous answers, current docker build
accepts --build-arg
that pass environment variables like http_proxy
without saving it in the resulting image.
例子:
# get squid
docker run --name squid -d --restart=always
--publish 3128:3128
--volume /var/spool/squid3
sameersbn/squid:3.3.8-11
# optionally in another terminal run tail on logs
docker exec -it squid tail -f /var/log/squid3/access.log
# get squid ip to use in docker build
SQUID_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' squid)
# build your instance
docker build --build-arg http_proxy=http://$SQUID_IP:3128 .
这篇关于如何使用缓存快速重建 dockerfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!