OpenShift 3.1 - 阻止 Docker 缓存 curl 资源
OpenShift 3.1 - Prevent Docker from caching curl resource
我的 Dockerfile 中有这个 curl 命令:
RUN curl -H 'Cache-Control: no-cache' -f ${NEXUS_URL}${ARTIFACT_PATH}-${ARTIFACT_VERSION}.war?nocache=true -o $JBOSS_HOME/standalone/deployments/ROOT.war
第一次运行看到下载信息。但是在那之后它似乎正在缓存远程资源,因此不再更新它:
Step 6 : RUN curl -H 'Cache-Control: no-cache' -f ${NEXUS_URL}${ARTIFACT_PATH}-${ARTIFACT_VERSION}.war?nocache=true -o $JBOSS_HOME/standalone/deployments/ROOT.war
30 ---> Using cache
31 ---> be50412bf6c3
我该如何防止这种情况发生?
You can bust the cache at a specific Dockerfile instruction using ARG.
In the Dockerfile:
ARG CACHEBUST=1
RUN git clone https://github.com/octocat/Hello-World.git
On the command line:
docker build -t your-image --build-arg CACHEBUST=$(date +%s) .
Setting CACHEBUST to the current time means that it will always be
unique, and instructions after the ARG declaration in the Dockerfile
won't be cached. Note that you can also build without specifying the
CACHEBUST build-arg, which will cause it to use the default value of 1
and preserve the cache. This can be used to always check out fresh
copies of git repos, pull latest SNAPSHOT dependencies, etc.
还有:
You can use --no-cache, or --build-arg to invalidate the cache. You
can minimize the effect of --no-cache by having a base image with all
the cacheable commands.
I can't control the way docker is run, I mean I am running in a PaaS that calls my dockerfile so I can't pass arguments in docker build
不过您可以尝试控制 Dockerfile 的内容。
如果您可以在让 PaaS 调用它之前重新生成 Dockerfile,那将有助于确保缓存失效:
sed -i 's/ROOT.war.*/ROOT.war && echo $(date)'/g' Dockerfile
根据 OpenShift 文档 (https://docs.openshift.com/enterprise/3.1/dev_guide/builds.html#no-cache),您可以使用以下语法强制不缓存构建:
strategy:
type: "Docker"
dockerStrategy:
noCache: true
这意味着不会缓存任何步骤,这会使您的构建变慢,但意味着您在构建中拥有正确的工件版本。
我知道这是一个老问题,但我认为我的解决方案比上面的更好。您可以只在特定点使缓存无效,而不是完全删除缓存。在我看来,最好的方法是这样的:
# No caching from now on!
ADD http://worldclockapi.com/api/json/utc/now build_time.json
即获取文件中的当前时间,每次 运行 它都会不同,从而使缓存无效。
我的 Dockerfile 中有这个 curl 命令:
RUN curl -H 'Cache-Control: no-cache' -f ${NEXUS_URL}${ARTIFACT_PATH}-${ARTIFACT_VERSION}.war?nocache=true -o $JBOSS_HOME/standalone/deployments/ROOT.war
第一次运行看到下载信息。但是在那之后它似乎正在缓存远程资源,因此不再更新它:
Step 6 : RUN curl -H 'Cache-Control: no-cache' -f ${NEXUS_URL}${ARTIFACT_PATH}-${ARTIFACT_VERSION}.war?nocache=true -o $JBOSS_HOME/standalone/deployments/ROOT.war
30 ---> Using cache
31 ---> be50412bf6c3
我该如何防止这种情况发生?
You can bust the cache at a specific Dockerfile instruction using ARG.
In the Dockerfile:
ARG CACHEBUST=1 RUN git clone https://github.com/octocat/Hello-World.git
On the command line:
docker build -t your-image --build-arg CACHEBUST=$(date +%s) .
Setting CACHEBUST to the current time means that it will always be unique, and instructions after the ARG declaration in the Dockerfile won't be cached. Note that you can also build without specifying the CACHEBUST build-arg, which will cause it to use the default value of 1 and preserve the cache. This can be used to always check out fresh copies of git repos, pull latest SNAPSHOT dependencies, etc.
还有:
You can use --no-cache, or --build-arg to invalidate the cache. You can minimize the effect of --no-cache by having a base image with all the cacheable commands.
I can't control the way docker is run, I mean I am running in a PaaS that calls my dockerfile so I can't pass arguments in docker build
不过您可以尝试控制 Dockerfile 的内容。
如果您可以在让 PaaS 调用它之前重新生成 Dockerfile,那将有助于确保缓存失效:
sed -i 's/ROOT.war.*/ROOT.war && echo $(date)'/g' Dockerfile
根据 OpenShift 文档 (https://docs.openshift.com/enterprise/3.1/dev_guide/builds.html#no-cache),您可以使用以下语法强制不缓存构建:
strategy:
type: "Docker"
dockerStrategy:
noCache: true
这意味着不会缓存任何步骤,这会使您的构建变慢,但意味着您在构建中拥有正确的工件版本。
我知道这是一个老问题,但我认为我的解决方案比上面的更好。您可以只在特定点使缓存无效,而不是完全删除缓存。在我看来,最好的方法是这样的:
# No caching from now on!
ADD http://worldclockapi.com/api/json/utc/now build_time.json
即获取文件中的当前时间,每次 运行 它都会不同,从而使缓存无效。