2

I'm trying to build a Docker container which downloads the source of https://github.com/micromdm/scep and using the go get command and cds into the resulting directory in order to perform some follow-up commands (namely, make deps, make build, and COPYing the relevant resulting binary into the container's PATH).

So far I've tried this Dockerfile:

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

However, if I try to run this I get the following error:

> 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

If I try the command without setting the GO111MODULE environment variable to on, I get his error:

> 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

Following https://github.com/ponzu-cms/ponzu/issues/204, I also tried running it with a ... at the end,

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

but this results in

> 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

Any ideas about how I could achieve downloading the package and cding into the resulting directory?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • What is the reason you cd into this directory? Any reason you are not defining WORKDIR? – sheldonzy Oct 14 '19 at 17:08
  • 1
    `RUN cd ...anything...` doesn't do anything useful (unless it's part of a compound command; `RUN cd ...anything... && do_something_else`, for example, can be useful, since it puts `do_something_else` in the `anything` directory, even though it doesn't change the working directory of a separate/subsequent `RUN`). Each `RUN` command happens inside a new and unique shell that isn't used for anything else, so `cd` -- which changes the state of the *current shell* -- has no lasting effect. – Charles Duffy Oct 14 '19 at 17:38
  • @CharlesDuffy Thanks, I am aware that `RUN cd ...` doesn't do anything useful and in its original form this command is paired with `&&` and subsequent commands, I just left it in this simplified form to show only what was needed to reproduce the error message. – Kurt Peek Oct 14 '19 at 18:39

3 Answers3

4

First of all, in docker you are building a container, so trying to cd into a directory in your pc its not possible. Use the WORKDIR command, also id recomend to git clone into a repository and there create the Dockerfile, and then COPY everything in there to a directory inside the docker for example:

COPY . /app
WORKDIR /app/where_you_want_to_cd
RUN whatever_you_want_to_run
paltaa
  • 2,985
  • 13
  • 28
2

You can set this path to be your WORKDIR, and the follow up command would be easy.

From the docker builder reference, each RUN command is run independently. So doing RUN cd does not have any effect on the next RUN command.

The following Dockerfile works:

FROM golang:latest

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

You need here the GO111MODULE since in the go.mod of that module isn't defined the go version, so you need to activate it yourself.

sheldonzy
  • 5,505
  • 9
  • 48
  • 86
1

It turns out that the resulting code is located not in /go/src, but in /go/pkg.

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526