我需要在CI服务器构建映像中使用checkout项目重用docker cache。
Docker ADD在结帐同一个文件时不使用缓存。
我在git branch A 执行docker build -t somename .
它正常使用docker缓存,但如果我通过git checkout B
转到 B 分支,什么都没有,通过git checkout A
转到分支 A 并再次运行docker build -t somename .
docker缓存仅在第一个ADD之前使用。
这里的例子:
Dockerfile
# DOCKER-VERSION 0.10.0
FROM myregistry:5000/ruby:2.1.2-1
MAINTAINER ME
# Gem sources
RUN gem source -r https://rubygems.org/
RUN gem source -a http://gems.mydomain
# Never install a ruby gem docs
RUN echo "gem: --no-rdoc --no-ri" >> ~/.gemrc
# gems install
RUN mkdir /foo
WORKDIR /foo
RUN gem install bundler
ADD Gemfile /foo/Gemfile
RUN bundle install
# expose ports
EXPOSE 8080
从构建日志
Sending build context to Docker daemon 19.54 MB
Sending build context to Docker daemon
Step 0 : FROM myregistry:5000/ruby:2.1.2-1
---> 9ce683a713b4
Step 1 : MAINTAINER ME
---> Using cache
---> 8f54114fd2e7
Step 2 : RUN gem source -r https://rubygems.org/
---> Using cache
---> f58a08708863
Step 3 : RUN gem source -a http://gems.mydomain
---> Using cache
---> 3e69e17c5954
Step 4 : RUN echo "gem: --no-rdoc --no-ri" >> ~/.gemrc
---> Using cache
---> 1edb37962dd4
Step 5 : RUN mkdir /foo
---> Running in 3d3441c34ee3
---> aeb90f00bc9b
Removing intermediate container 3d3441c34ee3
Step 6 : WORKDIR /foo
---> Running in 84b881d8f621
---> 10e1d8984458
Removing intermediate container 84b881d8f621
Step 7 : RUN gem install bundler
---> Running in 31e98523ce46
Successfully installed bundler-1.6.2
1 gem installed
---> 84d5195ab831
Removing intermediate container 31e98523ce46
Step 8 : ADD Gemfile /foo/Gemfile
---> 3e5f2675ee22
Removing intermediate container c90e8be5ea17
Step 9 : RUN bundle install
---> Running in ac0e83e5eebb
Fetching gem metadata from http://gems.mydomain/......
Fetching additional metadata from http://gems.mydomain/..
Resolving dependencies...
Installing rake 10.3.2
Installing i18n 0.6.9
Installing multi_json 1.10.1
.
.
.
Installing railties 3.2.19
Installing responders 0.9.3
Using bundler 1.6.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
---> 19beae703adb
Removing intermediate container ac0e83e5eebb
Step 10 : EXPOSE 8080
---> Running in 1b1e55d349e5
---> 32405bdac6d1
Removing intermediate container 1b1e55d349e5
Successfully built 32405bdac6d1
git checkout B
git checkout A
docker build -t somename。
第二次构建日志
Sending build context to Docker daemon 19.52 MB
Sending build context to Docker daemon
Step 0 : FROM myregistry:5000/ruby:2.1.2-1
---> 9ce683a713b4
Step 1 : MAINTAINER ME
---> Using cache
---> 8f54114fd2e7
Step 2 : RUN gem source -r https://rubygems.org/
---> Using cache
---> f58a08708863
Step 3 : RUN gem source -a http://gems.mydomain
---> Using cache
---> 3e69e17c5954
Step 4 : RUN echo "gem: --no-rdoc --no-ri" >> ~/.gemrc
---> Using cache
---> 1edb37962dd4
Step 5 : RUN mkdir /foo
---> Using cache
---> aeb90f00bc9b
Step 6 : WORKDIR /foo
---> Using cache
---> 10e1d8984458
Step 7 : RUN gem install bundler
---> Using cache
---> 84d5195ab831
Step 8 : ADD Gemfile /foo/Gemfile
---> 4977e35c80f7
Removing intermediate container bd59cc0d5e51
Step 9 : RUN bundle install
---> Running in 6ff16f32e94a
Fetching gem metadata from http://gems.mydomain/......
Fetching additional metadata from http://gems.mydomain/..
Resolving dependencies...
Installing rake 10.3.2
Installing i18n 0.6.9
Installing multi_json 1.10.1
.
.
.
Installing railties 3.2.19
Installing responders 0.9.3
Using bundler 1.6.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
---> d9332f9035c3
Removing intermediate container 6ff16f32e94a
Step 10 : EXPOSE 8080
---> Running in b20252a00160
---> 4d9932882e06
Removing intermediate container b20252a00160
Successfully built 4d9932882e06
答案 0 :(得分:12)
当文件的mtime值发生更改时,Docker会使docker构建缓存失效,并且git不会跟踪文件mtime值。此缓存失效也会在其他情况下出现,例如在涉及docker,git和branches的持续集成或构建环境中。
我一直在Makefile中使用“触摸”目标,我在要求docker构建容器之前运行该目标:
touch:
@echo "Reset timestamps on git working directory files..."
find ./ | grep -v .git | xargs touch -t 200001010000.00
接下来,始终在make touch
之前运行docker build
或在同一个Makefile中运行任何基于docker的“build”目标......
另一种选择是设置一个自动修改mtime值的git钩子: https://git.wiki.kernel.org/index.php/ExampleScripts#Setting_the_timestamps_of_the_files_to_the_commit_timestamp_of_the_commit_which_last_touched_them
另一种可能的解决方案是fork docker并从其缓存定义中删除mtime: https://github.com/docker/docker/blob/master/pkg/tarsum/tarsum.go
注意:从docker 1.8开始,
mtime
不再被考虑在内 当使缓存无效时。请求#12031已更新此行为
答案 1 :(得分:0)
git checkout,git clone,git fetch,等等修改文件的创建日期。然后docker看到另一个文件甚至是相同的。
docker ADD命令在文件更改的创建日期时无法缓存。