2

In my Golang/gin project i have a dockerfile. This docker file looks like this

FROM golang:latest

RUN mkdir -p /go/src/myAppName
ADD . /go/src/myAppName
WORKDIR /go/src/myAppName

ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

RUN go get -d -v ./...
RUN go install -v ./...
RUN go get github.com/pilu/fresh

EXPOSE 8080

CMD ["fresh"]

When I run the command docker build . --tag=myAppName:dev in the root directory of the project where the Dockerfile is located, it runs fine untill it wants to install the packages with the go get -d -v ./... command.

My project has a lot of different packages located in the project itself, so when it tries to import those it tells me the following:

package myAppName/app: unrecognised import path "myAppName/app" (import path does not begin with hostname).

I have read this question and promptly set the $GOPATH in my Dockerfile.

I also read this question and tried building the project the manual way without fresh and their method of not setting the GOPATH at all but doing it through the WORKDIR command.

Finally I tried using the go-wrapper commands, but these seem to not be available when i try to use them, resulting in the error command not found: go-wrapper

Unfortunately neither of these work, thus my question.

Any help or pointers in the right direction are much appreciated.

duck
  • 1,674
  • 2
  • 16
  • 26
  • 1
    Ask yourself: "What does `go get ./...` do? Then ask why you would want this at all? Then remove it. `go get` is **not** about "installing packages", it is about downloading external packages from e.g. github via `git clone`. – Volker Apr 12 '18 at 08:07
  • I did question that, but since i have imports for github packages nested in the project, i need to run through the entire project to check for imports – duck Apr 12 '18 at 08:11
  • Then still `go get ./...` is wrong. If the packages you have lying on disk import external dependencies then you should download (go get) these dependencies only. But doing this during docker build is a bad idea. Vendor them (commit to your project SCR) or use a dependency tool like dep (or vgo if feeling nerdy). But don't try go get: There is absolutely no way go accomplish what you need. – Volker Apr 12 '18 at 08:23
  • i will look into how to vendor them as it does seem like a good option here, thanks. – duck Apr 12 '18 at 08:36

1 Answers1

0

After experimennts I've came to the solution:

  1. All dependencies are vendored with dep. It's a quite easy and very useful tool - I recommend start using it.

  2. All packages (and my own projects's too) are located by their full name - $GOPATH/src/github.com/username/project/package/etc

  3. Then you may use simple Dockerfile like this:

    FROM golang:latest AS builder
    
    RUN go version
    
    COPY . "/go/src/github.com/user/project"
    WORKDIR "/go/src/github.com/user/project"
    
    RUN set -x && \
      apt-get update && \
      go get github.com/golang/dep/cmd/dep && \
      which dep && \
      date
    
    #RUN go get -v -t  .   # <- alternative way if you really don't want to use vendoring
    RUN set -x && \
      dep ensure -v  && \
      echo "vendor:" && \
      dep status
    
    RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build  -o /run-file
    
    CMD ["/run-file"]
    
    EXPOSE 8000
    
Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
  • Does this also work if my repository is private ? Do I need to add something if it is ? – duck Apr 12 '18 at 09:14
  • I am also getting that dep is not a command using the docker file you provided – duck Apr 12 '18 at 09:48
  • @duck, that works for my private repo if authorization is well established. (However I use not Github.com, but conceptually it's the same). Sorry, I've forgotten to add how I add dep (I do it in a separate Dockerfile, but it's also possible in-place). UPDATED – Eugene Lisitsky Apr 12 '18 at 09:51
  • I got dep to install through a simple `RUN` command. but authorisation is still an issue as dep hangs on fetching a private repo. any idea on how i can establish its authorisation ? – duck Apr 12 '18 at 10:42
  • Where’s your code located? At what githostong? GitHub? Gitlab? Your own server? AFAIK dep uses `git clone` under the hood. So you just need to authorize git access from building machine. By IP, token or some other way – Eugene Lisitsky Apr 13 '18 at 05:36
  • I managed to fix it by passing my github accountname and password as parameters to the docker file using `ARGS` and then setting them like so `RUN echo "machine github.com\n\tlogin $GITHUB_USER\n\tpassword $GITHUB_PASS" >> ~/.netrc`. Only problem I have now, is that golang tries to import packages that i defined inside the project from the wrong vendor folder. But i have made a separate question for that. – duck Apr 13 '18 at 09:53