How to rebuild dockerfile quick by using cache?

后端 未结 3 1046
伪装坚强ぢ
伪装坚强ぢ 2021-02-02 01:59

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.

<
3条回答
  •  有刺的猬
    2021-02-02 02:30

    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:

    > cd ~/your-project
    > git clone https://github.com/gertjanvanzwieten/replicator.git
    > mkdir cache
    > replicator/http-replicator -r ./cache -p 8080 --daemon ./cache/replicator.log  --static   
    

    add to your Dockerfile (before first RUN line):

    ENV http_proxy http://172.17.42.1:8080/
    

    You should optionally clear the cache from time to time.

    Option 2 - caching transparent proxy, no modification to Dockerfile:

    > cd ~/your-project
    > curl -o r.zip https://codeload.github.com/zahradil/replicator/zip/transparent-requests
    > unzip r.zip
    > rm r.zip
    > mv replicator-transparent-requests replicator
    > mkdir cache
    > replicator/http-replicator -r ./cache -p 8080 --daemon ./cache/replicator.log --static
    

    You need to start the replicator as some user (non root!).

    Set up the transparent redirect:

    > iptables -t nat -A OUTPUT -p tcp -m owner ! --uid-owner  --dport 80 -j REDIRECT --to-port 8080
    

    Disable redirect:

    > iptables -t nat -D OUTPUT -p tcp -m owner ! --uid-owner  --dport 80 -j REDIRECT --to-port 8080
    

    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.

提交回复
热议问题