0

我正在尝试构建一个 Docker 容器,该容器下载https://github.com/micromdm/scep的源代码并使用go get命令和cds 进入结果目录以执行一些后续命令(即make deps,make buildCOPY将相关生成的二进制文件放入容器的PATH) 中。

到目前为止,我已经尝试过Dockerfile

FROM golang:latest
ENV GO111MODULE on
RUN ["go", "get", "github.com/micromdm/scep"]
RUN cd $GOPATH/src/github.com/micromdm/scep

但是,如果我尝试运行它,我会收到以下错误:

> docker build . --tag loadtest
Sending build context to Docker daemon  4.608kB
Step 1/4 : FROM golang:latest
 ---> 52b59e9ead8e
Step 2/4 : ENV GO111MODULE on
 ---> Using cache
 ---> 28335bf0142b
Step 3/4 : RUN ["go", "get", "github.com/micromdm/scep"]
 ---> Using cache
 ---> 86760bf0c490
Step 4/4 : RUN cd $GOPATH/src/github.com/micromdm/scep
 ---> Running in b86fc3ab0ab8
/bin/sh: 1: cd: can't cd to /go/src/github.com/micromdm/scep
The command '/bin/sh -c cd $GOPATH/src/github.com/micromdm/scep' returned a non-zero code: 2

GO111MODULE如果我在没有将环境变量设置为 的情况下尝试该命令on,我会得到他的错误:

> docker build . --tag loadtest
Sending build context to Docker daemon  4.608kB
Step 1/3 : FROM golang:latest
 ---> 52b59e9ead8e
Step 2/3 : RUN ["go", "get", "github.com/micromdm/scep"]
 ---> Running in 8cb54311a416
package github.com/micromdm/scep: no Go files in /go/src/github.com/micromdm/scep
The command 'go get github.com/micromdm/scep' returned a non-zero code: 1

https://github.com/ponzu-cms/ponzu/issues/204之后,我还尝试...在最后运行它,

FROM golang:latest
RUN ["go", "get", "github.com/micromdm/scep/..."]
RUN cd $GOPATH/src/github.com/micromdm/scep \

但这会导致

> docker build . --tag loadtest
Sending build context to Docker daemon  4.608kB
Step 1/3 : FROM golang:latest
 ---> 52b59e9ead8e
Step 2/3 : RUN ["go", "get", "github.com/micromdm/scep/..."]
 ---> Running in 961bae3bb455
# github.com/micromdm/scep/scep
src/github.com/micromdm/scep/scep/scep.go:318:17: p7.EncryptionAlgorithm undefined (type *pkcs7.PKCS7 has no field or method EncryptionAlgorithm)
src/github.com/micromdm/scep/scep/scep.go:449:26: too many arguments in call to pkcs7.Encrypt
src/github.com/micromdm/scep/scep/scep.go:449:53: undefined: pkcs7.WithEncryptionAlgorithm
src/github.com/micromdm/scep/scep/scep.go:542:26: too many arguments in call to pkcs7.Encrypt
src/github.com/micromdm/scep/scep/scep.go:542:54: undefined: pkcs7.WithEncryptionAlgorithm
The command 'go get github.com/micromdm/scep/...' returned a non-zero code: 2

关于如何实现下载包并cd进入结果目录的任何想法?

4

3 回答 3

2

首先,在 docker 中,您正在构建一个容器,因此尝试 cd 进入您的 PC 中的目录是不可能的。使用 WORKDIR 命令,也建议将 git clone 到存储库中并在那里创建 Dockerfile,然后将其中的所有内容复制到 docker 内的目录中,例如:

COPY . /app
WORKDIR /app/where_you_want_to_cd
RUN whatever_you_want_to_run
于 2019-10-14T17:34:06.567 回答
1

事实证明,生成的代码不是位于 中/go/src,而是位于/go/pkg.

于 2019-10-14T18:42:55.913 回答
1

您可以将此路径设置为您的 WORKDIR,后续命令将很容易。

docker builder reference中,每个 RUN 命令都是独立运行的。这样做RUN cd对下一个 RUN 命令没有任何影响。

以下 Dockerfile 有效:

FROM golang:latest

ENV GO111MODULE on
RUN go get github.com/micromdm/scep
WORKDIR /go/src/github.com/micromdm/scep

您需要在这里,GO111MODULE因为go.mod该模块中的未定义 go 版本,因此您需要自己激活它。

于 2019-10-14T18:08:12.287 回答