在Docker中,无法将cd插入以“ go get”安装的目录吗?

时间:2019-10-14 16:50:45

标签: docker go go-modules go-get

我正在尝试构建一个Docker容器,该容器可下载https://github.com/micromdm/scep的源代码,并使用go get命令和cd s到结果目录中,以便执行一些后续操作命令(即make depsmake 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插入结果目录的任何想法吗?

3 个答案:

答案 0 :(得分:2)

首先,在docker中,您正在构建一个容器,因此尝试将cd插入PC中的目录是不可能的。使用WORKDIR命令,还将id推荐为git clone到存储库中,然后创建Dockerfile,然后将其中的所有内容复制到Docker内部的目录中,例如:

COPY . /app
WORKDIR /app/where_you_want_to_cd
RUN whatever_you_want_to_run

答案 1 :(得分: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版本,因此您需要自己激活它。

答案 2 :(得分:1)

事实证明,生成的代码不在/go/src中,而在/go/pkg中。