使用Docker的标准Go项目布局会导致构建上下文问题

问题描述

我正在遵循似乎有冲突的两个指南

  1. Standard Go Project Layout
  2. Google Cloud Endpoints Sample for Go using gRPC

Go的Standard Go Project Layout推荐一个/build目录,其使用方式为described

包装和持续集成。

放入您的云(AMI),容器(Docker),操作系统(deb,rpm,pkg)软件包 / build / package目录中的配置和脚本。

Google Cloud Endpoints Sample for Go using gRPC之后,我有一个Dockerfile,该文件复制应用程序源代码并使用go get安装所有依赖项。

# /build/Dockerfile

FROM golang:alpine

# Alpine Linux package management
RUN apk update
RUN apk add git

COPY ../ /go/src/github.com/username/repository

# Downloads the packages named by the import paths,along with their
# dependencies. It then installs the named packages,like 'go install'.
# ****Don't do this in production! Use vendoring instead.****
RUN go get -v github.com/username/repository/foo-module

# Compiles and installs the packages named by the import paths.
RUN go install github.com/username/repository/foo-module

ENTRYPOINT ["/go/bin/foo-module"]

Standard Go Project Layout之后,我将上述Dockerfile放置在/build中。

由于Dockerfile现在位于/build目录中,因此我修改了COPY命令以复制 parent 目录以查找应用程序源代码(COPY ../ /go/src/github.com/username/repository )。

Docker build命令不是不直接运行,而是以Google Cloud Build

开始构建
cloud-build-local --config=build/cloudbuild.yaml .

cloudbuild.yaml文件非常简单,并且刚刚启动了Docker构建。由于Cloud Build命令是从项目根目录而不是从--file开始的,因此build/Dockerfile标志指向/build

# /build/cloudbuild.yaml

steps:
    - id: Build
      name: "gcr.io/cloud-builders/docker"

      dir: ${_SOURCE}
      args:
        [
            "build","--file build/Dockerfile",".",]

按预期,这将失败

COPY failed: Forbidden path outside the build context: ../ ()

How to include files outside of Docker's build context?建议使用--file标志,但是,这假定构建是从根上下文开始的。使用Google的Cloud Build时,该构建是从cloudbuild.yaml开始的,该/build也位于cloudbuild.yaml中。

我可以将Docker文件放置在go模块的根目录中,但我想遵循最佳实践,并将Dockerfile/build保留在<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> ... 中。

遵循Standard Go Project Layout时实现此目标的正确方法是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)