我有一个拥有各种 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/