Hegep provider with docker

I am trying to start a docker container with a provider. This is my Dockerfile

FROM golang:alpine EXPOSE 8080 RUN mkdir /app ADD . /app/ WORKDIR /app RUN go build -o myapp . CMD ["/app/myapp"] 

and my main.go

 package main import ( "fmt" "log" "net/http" "github.com/gorilla/mux" ) func main() { r := mux.NewRouter() r.HandleFunc("/", Hello) http.Handle("/", r) fmt.Println("Starting up on 8080") log.Fatal(http.ListenAndServe(":8080", nil)) } func Hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintln(w, "Hello world!") } 

I use godep for vending libs, it works on my local machine, but when I try to run it from docker with:

 docker build -t myapp-img . docker run -p 8080:8080 --name myapp-cnt myapp-img 

I have the following error:

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

I do not understand what is missing.

+5
source share
1 answer

The error is correct. He tells you everything Gopher wants.

I am going to assume that you copied Gorilla Mux to the directory of your application / provider on your local computer, for example:

 ./main.go # this is your myapp code you are coping ./vendor/github.com/gorilla/mux # for vendoring, this must exist 

If you want to know more about the provider, see my popular answer here:

How to use a provider in Go 1.6?

Now, to fix this error, assuming you did it above ...

Before you can build, Gopher must install a valid $GOPATH . This is not in your Docker file.

 FROM golang:1.7-alpine EXPOSE 8080 # setup GOPATH and friends # # TECHNICALLY, you don't have to do these three cmds as the # golang:alpine image actually uses this same directory structure and # already has $GOPATH set to this same structure. You could just # remove these two lines and everything below should continue to work. # # But, I like to do it anyways to ensure my proper build # path in case I experiment with different Docker build images or in # case the #latest image changes structure (you should really use # a tag to lock down what version of Go you are using - note that I # locked you to the docker image golang:1.7-alpine above, since that is # the current latest you were using, with bug fixes). # RUN mkdir -p /go/src \ && mkdir -p /go/bin \ && mkdir -p /go/pkg ENV GOPATH=/go ENV PATH=$GOPATH/bin:$PATH # now copy your app to the proper build path RUN mkdir -p $GOPATH/src/app ADD . $GOPATH/src/app # should be able to build now WORKDIR $GOPATH/src/app RUN go build -o myapp . CMD ["/go/src/app/myapp"] 

Here he works ...

 $ tree . ├── Dockerfile ├── main.go └── vendor └── mydep └── runme.go 

Source file of my application:

 $ cat main.go package main import ( "fmt" "mydep" ) func main() { fmt.Println(mydep.RunMe()) } 

My dependency in my vendor/ folder:

 $ cat vendor/mydep/runme.go package mydep // RunMe returns a string that it worked! func RunMe() string { return "Dependency Worked!" } 

Now create and run the image:

 $ docker build --rm -t test . && docker run --rm -it test (snip) Step 8 : WORKDIR $GOPATH/src/app ---> Using cache ---> 954ed8e87ae0 Step 9 : RUN go build -o myapp . ---> Using cache ---> b4b613f0a939 Step 10 : CMD /go/src/app/myapp ---> Using cache ---> 3524025080df Successfully built 3524025080df Dependency Worked! 

Pay attention to the last line that displays the result from the console, Dependency Worked! .

This works because:

  • You stated that you are using Vendoring, which means that you have a local directory named ./vendor in the root code of your application.
  • when you ADD . /go/src/app ADD . /go/src/app , you will also copy the local ./vendor application code.
  • you copied your files to the correct $GOPATH configuration structure needed by the Go build tools to find the packages (in this case, the ./vendor directory in the root folder of the source code).
+4
source

Source: https://habr.com/ru/post/1259032/


All Articles