复制文件而不是Dir,在Docker 2阶段构建中使用相同名称

发布于 2025-01-30 07:16:07 字数 323 浏览 3 评论 0原文

在以下dockerfile中,

WORKDIR /app
RUN go build -ldflags "-s -w" -a -installsuffix cgo -o tool-web ./tool-web

...

FROM scratch
COPY --from=build /app/tool-web /app/tool-web

复制指令复制Directory tool> tool-web而不是二进制。

是否有一种方法可以指定要复制二进制文件的复制?

In the following Dockerfile

WORKDIR /app
RUN go build -ldflags "-s -w" -a -installsuffix cgo -o tool-web ./tool-web

...

FROM scratch
COPY --from=build /app/tool-web /app/tool-web

turns out the COPY directive copies the directory tool-web and not the binary.

Is there a way to specify to the COPY that the binary is to be copied?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昇り龍 2025-02-06 07:16:07

根据go命令 documentationation

-o标志力构建以编写所得的可执行文件或对象,以将其构建到命名的输出文件或目录,而不是最后两段中描述的默认行为。如果命名的输出是现有目录或以斜线或后斜线结束,则将将任何结果的可执行文件写入该目录。

因此,如果您使用-o tool-web./ tool-web/是现有目录,则输出二进制文件将以>中的文件写入工具-Web目录。一个不错的猜测是您的二进制可能已安装为./ tool-web/main

为了解决此问题,两种简单的方法是:

  1. 找出您的二进制的路径,并调整复制以将该路径用作源
    • 复制-from = build/app/tool-web/main/app/tool-web
  2. 或,更改-O以写入其他文件名,然后调整复制将该名称用作源
    • -o tool-web-binary
    • 复制-from = build/app/tool-web-binary/app/tool-web

Per the go command documentation:

The -o flag forces build to write the resulting executable or object to the named output file or directory, instead of the default behavior described in the last two paragraphs. If the named output is an existing directory or ends with a slash or backslash, then any resulting executables will be written to that directory.

Thus, if you use -o tool-web and ./tool-web/ is an existing directory, your output binary will be written as a file within the tool-web directory. A decent guess is your binary may have been installed as ./tool-web/main.

To fix this, two easy approaches would be:

  1. Figure out the path your binary is installed as, and adjust COPY to use that path as the source
    • COPY --from=build /app/tool-web/main /app/tool-web
  2. Or, change the -o to write to a different filename, and then adjust COPY to use that name as the source
    • -o tool-web-binary
    • COPY --from=build /app/tool-web-binary /app/tool-web
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文