Docker忽略用于多阶段构建的文件

发布于 2025-01-28 10:20:04 字数 1061 浏览 3 评论 0原文

我需要在第一个阶段中排除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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文