-3

I have a Go app. Some of its dependencies are in a private Github repo and another part of dependencies are local packages in my app folder. The app compiles and works on my computer without a problem (when I simply compile it without docker). I am using the below Dockerfile.

FROM ubuntu as intermediate

# install git
RUN apt-get update
RUN apt-get install -y git


RUN mkdir /root/.ssh/
COPY github_rsa.ppk /root/.ssh/github_rsa.ppk
RUN chmod 700 /root/.ssh/github_rsa.ppk

RUN eval $(ssh-agent) && \
    ssh-add /root/.ssh/github_rsa.ppk && \
    ssh-keyscan -H github.com >> /etc/ssh/ssh_known_hosts && \
    git clone git@github.myusername/shared.git


FROM golang:latest
ENV GOPATH=/go

RUN echo $GOPATH

ADD . /go/src/SCMicroServer
WORKDIR /go/src/SCMicroServer
COPY --from=intermediate /shared /go/src/github.com/myusername/shared
RUN go get /go/src/SCMicroServer
RUN go install SCMicroServer
ENTRYPOINT /go/src/SCMicroServer
EXPOSE 8080

First build section related to Git is working fine, It works until this line: RUN go get /go/src/SCMicroServer in second section. I receive this error in mentioned step:

package SCMicroServer/controllers/package1: unrecognized import path "SCMicroServer/controllers/package1" (import path does not begin with hostname)
The command '/bin/sh -c go get /go/src/SCMicroServer' returned a non-zero code: 1

"SCMicroServer/controllers/package1" is one of the local packages in my app folder (or its subfolders) and I have many more in my local folder. I am setting GOPATH env variable in my Dockerfile, so I am not sure what I am missing.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Mazdak
  • 771
  • 2
  • 11
  • 24
  • 3
    It looks like you're using relative import paths. [Don't do that](https://stackoverflow.com/questions/38517593/relative-imports-in-go). – Jonathan Hall Nov 18 '19 at 13:51
  • `go get` works on package names, not filesystem paths. They may be related, but are not the same thing. – JimB Nov 18 '19 at 14:10
  • I checked the link, I am using importing the same way recommended in your link: import ("SCMicroServer/controllers/package1") @Flimzy – Mazdak Nov 18 '19 at 14:11
  • @JimB When I change that line to "RUN go get SCMicroServer", I still get the same error. – Mazdak Nov 18 '19 at 14:13
  • @Mazdak: outside of the module, go needs a hostname to locate a package for `go get`. You're not trying to fetch a remote copy of the package, so if you want to build `SCMicroServer`, work from within that directory. – JimB Nov 18 '19 at 14:15
  • @Mazdak: That's NOT what the link says to do. You're still using relative imports. – Jonathan Hall Nov 18 '19 at 14:56

1 Answers1

0

I found the answer, it was not really Dockerfile problem, I referenced my package 2 times in 2 different way in my main file:

package1 "SCMicroServer/controllers/package1"
"SCMicroServer/controllers/package1"

After I removed the second one, I stopped receiving the error.

Mazdak
  • 771
  • 2
  • 11
  • 24