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
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.
After experimennts I've came to the solution:
All dependencies are vendored with dep
. It's a quite easy and very useful tool - I recommend start using it.
All packages (and my own projects's too) are located by their full name - $GOPATH/src/github.com/username/project/package/etc
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.