Docker忽略用于多阶段构建的文件
我需要在第一个阶段中排除Docker副本中的文件,但在第二阶段的另一个副本中使用它,因为它会破坏Docker层的缓存。
我的文件夹结构看起来像:
src/
public/
.env
package.json
nginx.conf
我的dockerfile看起来像这样:
FROM node:14 as builder # line-1
WORKDIR /app # line-2
COPY . /app # line-3 nginx.conf is copied but not needed
RUN yarn && yarn build # line-4
FROM nginx:1.21.1 # line-5
WORKDIR /app # line-6
COPY --from=builder /app/build /app/build # line-7
COPY nginx.conf /app # line-8 nginx.conf is copied
CMD nginx -c /app/nginx.conf # line-9
当我更改nginx.conf
docker layer layer缓存由于第三行而变得无效。
我不能使用.dockerignore
,因为它在两个阶段都会忽略它。
明显的解决方案是一个一个一个一个一个一个一个一个一个,但这似乎是不可靠的,因为项目结构可能会不时改变,我想避免额外的努力Dockerfile Vaintenance&更新。
是否有较短的方法来克服此功能,除此之外,在这里?
I need to exclude file in Docker COPY for the first stage but use it in another COPY for the second stage, because it breaks docker layer caching.
My folder structure looks like:
src/
public/
.env
package.json
nginx.conf
My Dockerfile looks like this:
FROM node:14 as builder # line-1
WORKDIR /app # line-2
COPY . /app # line-3 nginx.conf is copied but not needed
RUN yarn && yarn build # line-4
FROM nginx:1.21.1 # line-5
WORKDIR /app # line-6
COPY --from=builder /app/build /app/build # line-7
COPY nginx.conf /app # line-8 nginx.conf is copied
CMD nginx -c /app/nginx.conf # line-9
When I change nginx.conf
docker layer caching becomes invalidated because of the 3rd line.
I cannot use .dockerignore
because it will ignore it for both stages.
Obvious solution is to copy all the needed folders one by one, but that seems to be unreliable because project structure can change from time to time and I'd like to avoid extra effort of Dockerfile maintenance & updating.
Is there shorter way to overcome this apart from that is mentioned here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论