2

I am trying to get a Go app inside a docker container. This is my first bigger Go and Docker project. The go program runs just fine as long as I am running it on my local machine, now I want to run it on EC2 within a docker container. My docker file looks like this:

FROM golang:latest 
RUN mkdir /tir
ADD . /tir 
WORKDIR /tir
RUN go build -o main . 
CMD ["/app/main"]

But I get the following error for every private dependencies:

main.go:17:2: cannot find package "github.com/ser/model" in any of:
    /usr/local/go/src/github.com/ser/model (from $GOROOT)
    /go/src/github.com/ser/model (from $GOPATH)

When I insert RUN go get ./.. before RUN go build -o main ., I get the following error for every package:

fatal: could not read Username for 'https://github.com': terminal prompts disabled

package github.com/ser/endpoints: exit status 128

I have tried a couple of solutions, but none worked. I always end up having the above errors. Since this is my first docker + golang project, are there any ready to use dockerfiles for an golang app with public as well as private dependencies?

UPDATE: I deinstalled go, and copied every file in one by one and used dep -ensure after every file. Now it works, thanks :D

jjuser19jj
  • 1,637
  • 3
  • 20
  • 38
  • 1
    Possible duplicate of [What's the proper way to "go get" a private repository?](https://stackoverflow.com/questions/27500861/whats-the-proper-way-to-go-get-a-private-repository) – Jonathan Hall Nov 16 '18 at 16:26
  • refer this link https://stackoverflow.com/questions/55058056/running-dep-ensure-vendor-only-inside-docker-hangs-not-able-to-pull-private-rep/55761788#55761788 – Javeed Shakeel Apr 19 '19 at 12:36

1 Answers1

1

Your dependencies are probably stored in GOPATH/src/<import-path> and you manage them with go get tool.

Consider vendoring and tools like dep or modules

As a result your dependencies will be included into source control and project will be much more portable.

You also can improve the way how you create Docker image.

Current implementation uses one container that includes whole GO toolchain. Code is copied inside that that container, container compiles and hosts the code. Only later is necessary for production.

Better option will be to use 2 containers:

  1. Go Tools for compilation
  2. Lightweight container that only host binaries
# Debian image with the latest version of Go installed
# and a workspace (GOPATH) configured at /go.
FROM golang:1.11 as builder
WORKDIR /go/src/github.com/space/project/

# Copy the local package files to the container's workspace.
ADD . /go/src/github.com/space/project/

# Build the service inside the container.
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM centurylink/ca-certs
EXPOSE 8080

# Copy app
COPY --from=builder /go/src/github.com/space/project/app   /
ENTRYPOINT ["/app"]
Dmitry Harnitski
  • 5,838
  • 1
  • 28
  • 43
  • I tried to use dep, but got the following issue `1 transitively valid internal packages 14 external packages imported from 11 projects (0) ✓ select (root) (1) ← no more versions of github.com/aristotle/auth0 to try; begin backtrack ✗ solving failed Solver wall times by segment: b-list-versions: 39.248856824s other: 39.136849ms select-root: 29.564742ms new-atom: 954.931µs b-deduce-proj-root: 26.362µs b-source-exists: 490ns TOTAL: 39.318540198s` ideas ? – jjuser19jj Nov 16 '18 at 21:12
  • github.com/aristotle/auth0 is not public repository. How did you use it before adding `dep`? – Dmitry Harnitski Nov 16 '18 at 21:17
  • I just imported it via the path und used in my project like any other package. I have to say I tried to remove all non public packages from my project, but I still get the same error. Fx also for gorilla/mux – jjuser19jj Nov 17 '18 at 06:51
  • With vendoring, all your dependencies should be stored in `/vendor` folder inside your project local directory and should be committed into source control. – Dmitry Harnitski Nov 17 '18 at 12:37
  • I deinstalled go, and copied every file in one by one and used dep -ensure after every file. Now it works, thanks :D – jjuser19jj Nov 18 '18 at 11:20
  • 1
    @user1990524 add this comment as `update: ` to your question to help others and mark answer as correct – Dmitry Harnitski Nov 18 '18 at 13:56