登录
首页 >  Golang >  Go问答

Go 二进制文件应该放在哪个位置?

来源:stackoverflow

时间:2024-03-02 17:06:26 197浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《Go 二进制文件应该放在哪个位置?》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我正在尝试为 go 应用程序构建我的第一个 dockerfile 并使用 droneci 构建管道。

droneci 配置如下所示:

kind: pipeline
type: docker
name: build auto git tagger

steps:
  - name: test and build
    image: golang
    commands:
      - go mod download
      - go test ./test
      - go build ./cmd/git-tagger

  - name: build docker image
    image: plugins/docker
    pull: if-not-exists
    settings:
      username: 
      password: 
      repo: 
      dockerfile: ./build/ci/dockerfile
      registry: 
      auto_tag: true

trigger:
  branch:
    - master

我遵循了 https://github.com/golang-standards/project-layout 的结构约定:

到目前为止,dockerfile 如下所示:

FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /root/

下一步是将 go 应用程序二进制文件复制到容器中,这里的问题是,将编译后的二进制文件放在哪里?目前,它已放入项目文件夹中。


解决方案


您可以使用 go build -o 标志指定输出目录和文件名。例如:

go build ./cmd/git-tagger -o ./build/package/foo

然后编辑你的 dockerfile:

  1. 加载您使用 COPY or ADD 获得的二进制文件

  2. 使用 ENTRYPOINT or CMD 执行

p.s 您在配置中将 dockerfile 路径指定为 ./build/ci/dockerfile。但它位于屏幕截图中的 package 目录中。

不要忘记您链接的存储库只是中小型企业的个人意见,go 并没有真正强制您采用任何结构,这取决于您公司的风格标准或您的偏好。因此,将二进制文件放在哪里并不是非常重要。

如果 go 应用程序不需要 docker 映像本身的任何内容,例如:bashls 或任何其他操作系统级命令,我通常在 scratch 图像上进行多阶段构建。查看下面的 run cgo_enabled=0 ... 行,了解如何将应用程序编译为静态链接的二进制文件。以这种方式构建的 golang 应用程序应该在 scratch 容器中运行,而无需任何其他映像依赖项,因此空的 scratch 映像可以很好地保持容器大小非常小。我的 go 二进制文件为 25.5mb,基于 scratch 的最终 docker 镜像仅为 26.6mb

注意:在您的场景中,由于您的目录结构与我的不同,因此您需要在之前添加 workdir 命令run cgo_enabled=0 ... 行,类似于: workdir /go/src/app/cmd/git-tagger (我们已将存储库文件复制到第一阶段映像),以便您运行 go从与 main.go 相同的目录安装 命令。

go 二进制文件的多阶段 dockerfile 示例:

# Stage 1: Use base Alpine image to prepare our binary, label it 'app'
FROM golang:alpine as app
# Add golangdocker user and group so that the Docker process doesn't run as root
RUN addgroup -S golangdocker \
    && adduser -S -u 10000 -g golangdocker golangdocker
# Change to the correct directory to hold our application source code
WORKDIR /go/src/app
# Copy all the files from the base of our repository
COPY . .
# Compile the application to a single statically-linked binary file
RUN CGO_ENABLED=0 go install -ldflags '-extldflags "-static"' -tags timetzdata

# Stage 2: Use the Docker Scratch image to copy our previous stage into
FROM scratch
# Grab necessary certificates as Scratch has none
COPY --from=alpine:latest /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy our binary to the root of the Scratch image (note: --from=app, the name we gave our first stage)
COPY --from=app /go/bin/golangdocker /golangdocker
# Copy the user from the first stage so that we don't run the process as root
COPY --from=app /etc/passwd /etc/passwd
# Change to the non-root user
USER golangdocker
# Run our app
ENTRYPOINT ["/golangdocker"]

更多背景信息可以在这里找到:https://mwiater.github.io/golangdocker/docs.html#docker-docker-build-notes

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Go 二进制文件应该放在哪个位置?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>