docker - 复制失败 : Forbidden path outside the build context

标签 docker go

我有一个拥有各种 Go 服务和库的 monorepo。它有一个结构

monorepo
 services 
  service-a 
   - Dockerfile
go.mod
go.sum

我的 go.mod 驻留在 monorepo 的根目录中,服务使用 go.mod 文件中声明的依赖项。

我用

docker build -t some:tag ./services/service-a/



当我尝试使用上述 docker 命令从 monorepo 的根目录构建我的 Docker 镜像时,出现以下错误。

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



下面是我的 Dockerfile
FROM golang:1.14.1-alpine3.11

RUN apk add --no-cache ca-certificates git

# Enable Go Modules
ENV GO111MODULE=on

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY ../../go.mod go.sum ./

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build -o service-a

ENTRYPOINT ["/app/service-a"]

我需要做些什么才能将不在当前目录中的文件添加到我的 Docker 镜像中,而不必在 monorepo 中的每个服务中使用单独的 go.mod 和 go.sum?

最佳答案

Docker 只允许从上下文向图像添加文件,默认情况下,上下文是包含 Dockerfile 的目录。您可以在构建时指定不同的上下文,但同样,它不会让您包含该上下文之外的文件:

docker build -f ./services/service-a/Dockerfile .

这应该使用当前目录作为上下文。

或者,您可以创建一个临时目录,将所有工件复制到那里并将其用作构建上下文。这可以通过 makefile 或构建脚本自动化。

关于docker - 复制失败 : Forbidden path outside the build context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61196371/

相关文章:

docker - .gitlab-ci.yml 错误 : "apk: command not found"

linux - Docker 创建了两个破坏我的互联网访问的网桥

c - 如何从 C 库中检索 probuf

c++ - Go context.Background() 的 gRPC++ 等价物是什么?

python - "Lambda function for the route not found"使用SAM在本地运行Golang函数出错?

golang http处理程序上下文

docker - 为什么我的绑定(bind)挂载实际上并没有绑定(bind)某些文件?

bash - 在 Docker Compose 中执行/bin/bash

docker - Docker上的带有Kibana的Elastic表示Kibana服务器尚未准备好

go - 复合类型和指针方法