构建 Go 程序时从远程基础镜像指向本地依赖 Docker 镜像
Point to local dependency from remote base image when building Go program Docker image
请注意,我是 Golang 和 Docker 开发的新手。我已经在其他地方问过这个问题并尝试阅读文档但找不到任何解决方案。
问题及代码
我有两个存储库 /home/experience/keep-ecdsa
和 /home/experience/keep-core
,我从 this project 派生并克隆到本地。
我正在尝试在本地为 keep-ecdsa
构建一个 docker 图像。这是我的 forked repo 的 link。
有一些 keep-core
依赖项,我想指向我的本地 keep-core
存储库。因此,我将 go.mod
更改为:
module github.com/keep-network/keep-ecdsa
go 1.13
replace (
//unrelated stuff...
github.com/keep-network/keep-core => /home/experience/keep-core
)
require (
//unrelated stuff...
github.com/keep-network/keep-core v1.1.3
)
DOCKERFILE 开始如下 (click here to see the full file) :
FROM golang:1.13.8-alpine3.10 AS runtime
ENV APP_NAME=keep-ecdsa \
BIN_PATH=/usr/local/bin
FROM runtime AS gobuild
ENV GOPATH=/go \
GOBIN=/go/bin \
APP_NAME=keep-ecdsa \
APP_DIR=/go/src/github.com/keep-network/keep-ecdsa \
BIN_PATH=/usr/local/bin \
# GO111MODULE required to support go modules
GO111MODULE=on
//rest of the linked DOCKERFILE
当 运行 docker build .
时,我得到以下错误,该错误发生在 DOCKERFILE
.
的 RUN go mod download
步骤
Step 13/27 : RUN go mod download
--> Running in 88839fc42d4e
go: github.com/keep-network/keep-core@v1.1.3: parsing /home/experience/keep-core/go.mod: open /home/experience/keep-core/go.mod: no such file or directory
The command '/bin/sh -c go mod download' returned a non-zero code: 1
我的尝试和领先优势
我尝试过:
- 将 DOCKERFILE 中的
GOPATH
更改为各种绝对本地 fs 路径
- 使我的
APPDIR
在 DOCKERFILE 中指向我的绝对本地路径 /home/experience/keep-ecdsa
- 将
go.mod
的replace ( )
语句中的路径改为各种路径(绝对本地,相对于GOPATH
等
有人给了我这条线索:
you are inside a golang:1.13.8-alpine3.10
base image
so there is no /home/experience/keep-core
inside there
since that is only on your local fs
但我仍然不知道如何实现我想要的。也许用一些本地基础图像替换 DOCKERFILE 中的 FROM ... AS runtine
语句?但是我如何找到这样的相关基础图像,它不会改变其余的 DOCKERFILE 指令吗?
请记住,我将对 keep-core
依赖项进行本地更改,并且需要对其进行测试,因此包含 replace (github.com/mygithubprofile/keep-core)
的解决方案并不令人满意。
提前致谢。
you are inside a golang:1.13.8-alpine3.10 base image so there is no /home/experience/keep-core inside there since that is only on your local fs
根据我在文件中看到的内容,您尚未将机器上的 /home/experience/keep-core
目录复制到 docker 映像,因此它会抛出错误,因为该目录尚不存在.
Docker 无法跟踪当前上下文目录之外的链接,因此如果您不想编辑 replace (github.com/mygithubprofile/keep-core)
,您可以将 Docker 文件移动到 /home/experience/
并且使用 COPY
命令将 keep-core
文件夹从本地计算机复制到 docker 图像。
RUN mkdir -p /home/experience/keep-core
COPY ./keep-core /home/experience/keep-core
但是,如果您希望 Docker 文件保留在 /home/experience/keep-ecdsa
中,您可以将 keep-core
文件夹移动到 keep-ecdsa
文件夹中,并在 [=21] 中忽略它=] 文件。然后更新
replace (
//unrelated stuff...
github.com/keep-network/keep-core => /home/experience/keep-core
)
到
replace (
//unrelated stuff...
github.com/keep-network/keep-core => /home/experience/keep-ecdsa/keep-core
)
请注意,我是 Golang 和 Docker 开发的新手。我已经在其他地方问过这个问题并尝试阅读文档但找不到任何解决方案。
问题及代码
我有两个存储库 /home/experience/keep-ecdsa
和 /home/experience/keep-core
,我从 this project 派生并克隆到本地。
我正在尝试在本地为 keep-ecdsa
构建一个 docker 图像。这是我的 forked repo 的 link。
有一些 keep-core
依赖项,我想指向我的本地 keep-core
存储库。因此,我将 go.mod
更改为:
module github.com/keep-network/keep-ecdsa
go 1.13
replace (
//unrelated stuff...
github.com/keep-network/keep-core => /home/experience/keep-core
)
require (
//unrelated stuff...
github.com/keep-network/keep-core v1.1.3
)
DOCKERFILE 开始如下 (click here to see the full file) :
FROM golang:1.13.8-alpine3.10 AS runtime
ENV APP_NAME=keep-ecdsa \
BIN_PATH=/usr/local/bin
FROM runtime AS gobuild
ENV GOPATH=/go \
GOBIN=/go/bin \
APP_NAME=keep-ecdsa \
APP_DIR=/go/src/github.com/keep-network/keep-ecdsa \
BIN_PATH=/usr/local/bin \
# GO111MODULE required to support go modules
GO111MODULE=on
//rest of the linked DOCKERFILE
当 运行 docker build .
时,我得到以下错误,该错误发生在 DOCKERFILE
.
RUN go mod download
步骤
Step 13/27 : RUN go mod download
--> Running in 88839fc42d4e
go: github.com/keep-network/keep-core@v1.1.3: parsing /home/experience/keep-core/go.mod: open /home/experience/keep-core/go.mod: no such file or directory
The command '/bin/sh -c go mod download' returned a non-zero code: 1
我的尝试和领先优势
我尝试过:
- 将 DOCKERFILE 中的
GOPATH
更改为各种绝对本地 fs 路径 - 使我的
APPDIR
在 DOCKERFILE 中指向我的绝对本地路径/home/experience/keep-ecdsa
- 将
go.mod
的replace ( )
语句中的路径改为各种路径(绝对本地,相对于GOPATH
等
有人给了我这条线索:
you are inside a
golang:1.13.8-alpine3.10
base image so there is no/home/experience/keep-core
inside there since that is only on your local fs
但我仍然不知道如何实现我想要的。也许用一些本地基础图像替换 DOCKERFILE 中的 FROM ... AS runtine
语句?但是我如何找到这样的相关基础图像,它不会改变其余的 DOCKERFILE 指令吗?
请记住,我将对 keep-core
依赖项进行本地更改,并且需要对其进行测试,因此包含 replace (github.com/mygithubprofile/keep-core)
的解决方案并不令人满意。
提前致谢。
you are inside a golang:1.13.8-alpine3.10 base image so there is no /home/experience/keep-core inside there since that is only on your local fs
根据我在文件中看到的内容,您尚未将机器上的 /home/experience/keep-core
目录复制到 docker 映像,因此它会抛出错误,因为该目录尚不存在.
Docker 无法跟踪当前上下文目录之外的链接,因此如果您不想编辑 replace (github.com/mygithubprofile/keep-core)
,您可以将 Docker 文件移动到 /home/experience/
并且使用 COPY
命令将 keep-core
文件夹从本地计算机复制到 docker 图像。
RUN mkdir -p /home/experience/keep-core
COPY ./keep-core /home/experience/keep-core
但是,如果您希望 Docker 文件保留在 /home/experience/keep-ecdsa
中,您可以将 keep-core
文件夹移动到 keep-ecdsa
文件夹中,并在 [=21] 中忽略它=] 文件。然后更新
replace (
//unrelated stuff...
github.com/keep-network/keep-core => /home/experience/keep-core
)
到
replace (
//unrelated stuff...
github.com/keep-network/keep-core => /home/experience/keep-ecdsa/keep-core
)