Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rebuild dockerfile quick by using cache?

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.

like image 226
Daniel YC Lin Avatar asked Feb 26 '14 03:02

Daniel YC Lin


People also ask

Does Docker build cache?

Docker's build-cache is a handy feature. It speeds up Docker builds due to reusing previously created layers. You can use the --no-cache option to disable caching or use a custom Docker build argument to enforce rebuilding from a certain step.

How does Dockerfile cache work?

Each command that is found in a Dockerfile creates a new layer. Each layer contains the filesystem changes to the image for the state before the execution of the command and the state after the execution of the command. Docker uses a layer cache to optimize and speed up the process of building Docker images.


1 Answers

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.

Example:

# 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 .
like image 120
Piotr Czapla Avatar answered Oct 27 '22 10:10

Piotr Czapla