gpt4 book ai didi

docker - 构建 Go 程序 Docker 镜像时从远程基础镜像指向本地依赖

转载 作者:行者123 更新时间:2023-12-01 22:12:57 25 4
gpt4 key购买 nike

请注意,我是 Golang 和 Docker 开发的新手。我已经在其他地方问过这个问题并尝试阅读文档但找不到任何解决方案。

问题和代码

我有两个 repo /home/experience/keep-ecdsa/home/experience/keep-core我从 this project fork 的并在本地克隆。

我正在尝试为 keep-ecdsa 构建 docker 镜像本地。这是我的forked repo的链接.

有一些keep-core依赖项,我想指向我的本地 keep-core repo 。因此,我更改了我的 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 . ,我收到以下错误,该错误发生在 RUN go mod download DOCKERFILE 的步骤.
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

我的尝试和领先

我试图:
  • 更改我的GOPATH在 DOCKERFILE 中到各种绝对本地 fs 路径
  • 让我的APPDIR在 DOCKERFILE 中指向我的绝对本地路径 /home/experience/keep-ecdsa
  • 更改replace ( )中的路径go.mod 的声明到各种路径(绝对本地,相对于 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



    但我仍然不知道如何实现我想要的。也许替换 FROM ... AS runtine某些本地基础镜像在 DOCKERFILE 中的语句?但是我如何找到这样相关的基本图像,它不会改变 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)您可以将 DockerFile 移至 /home/experience/并使用 COPY复制命令 keep-core从本地计算机到 docker 镜像的文件夹。
    RUN mkdir -p /home/experience/keep-core
    COPY ./keep-core /home/experience/keep-core

    但是,如果您希望 DockerFile 保留在 /home/experience/keep-ecdsa你可以移动 keep-core文件夹到 keep-ecdsa文件夹并在 .gitignore 中忽略它文件。然后更新

    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
    )

    关于docker - 构建 Go 程序 Docker 镜像时从远程基础镜像指向本地依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62466870/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com