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.
I wish to share My cached directory during build (eg. /var/cache/yum/x86_64/6).
But, it works only on docker run -v ...
.
Any suggestion?(In this example, only 1 rpm installed, in real case, I require to install hundreds rpms)
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
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.
Just use an intermediate/base image:
Base Dockerfile, build it with
docker build -t custom-base
or something:Application Dockerfile:
You should use a caching proxy (f.e Http Replicator, squid-deb-proxy ...) or apt-cacher-ng for Ubuntu to cache installation packages. I think, you can install this software to the host machine.
EDIT:
Option 1 - caching http proxy - easier method with modified Dockerfile:
add to your Dockerfile (before first RUN line):
You should optionally clear the cache from time to time.
Option 2 - caching transparent proxy, no modification to Dockerfile:
You need to start the replicator as some user (non root!).
Set up the transparent redirect:
Disable redirect:
This method is the most transparent and general and your Dockerfile does not need to be modified. You should optionally clear the cache from time to time.
An update to previous answers, current docker build accepts
--build-arg
that pass environment variables likehttp_proxy
without saving it in the resulting image.Example: