如何使用缓存快速重建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屋!