2016-10-31 18 views
5

के साथ godep विक्रेता विक्रेता मैं विक्रेता के साथ एक डॉकर कंटेनर चलाने की कोशिश कर रहा हूं। यह मेरा Dockerfileडॉकर

FROM golang:alpine 
EXPOSE 8080 

RUN mkdir /app 
ADD . /app/ 
WORKDIR /app 
RUN go build -o myapp . 
CMD ["/app/myapp"] 

और मेरे 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!") 
} 

मैं libs vendoring के लिए godep उपयोग कर रहा हूँ, यह मेरी स्थानीय मशीन में काम कर रहा है, लेकिन जब मैं के साथ इसे चलाने के लिए कोशिश कर रहा हूँ डोकर साथ:

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

मैं निम्नलिखित त्रुटि है:

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) 

मुझे समझ में नहीं आता कि क्या गुम है।

उत्तर

4

त्रुटि सही है। यह आपको एक महत्वाकांक्षी गोफर की जरूरतों को बता रहा है।

मैं, ग्रहण करने के लिए तो जैसे कि आप अपने स्थानीय मशीन में अपने ऐप के/विक्रेता निर्देशिका के लिए गोरिल्ला Mux की नकल की है जा रहा हूँ:

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

आप vendoring बारे में अधिक जानने के लिए, यहाँ मेरी लोकप्रिय जवाब देखने चाहते हैं :

How should I use vendor in Go 1.6?

अब, यह सोचते हैं कि आप ऊपर किया है कि त्रुटि को ठीक करने के लिए ...

एक गोफर की जरूरत से निर्माण करने से पहले एक मान्य $GOPATH टी। यह आपके डॉकरफाइल से गायब है।

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"] 

यहाँ यह काम कर रहा है ...

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

मेरे ऐप्लिकेशन के स्रोत फ़ाइल:

$ cat main.go 
package main 

import (
     "fmt" 

     "mydep" 
) 

func main() { 
     fmt.Println(mydep.RunMe()) 
} 

मेरी vendor/ फ़ोल्डर में मेरे निर्भरता:

$ cat vendor/mydep/runme.go 
package mydep 

// RunMe returns a string that it worked! 
func RunMe() string { 
     return "Dependency Worked!" 
} 

अब , छवि बनाएं और चलाएं:

$ 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! 

कंसोल से आउटपुट प्रिंट करने वाली अंतिम पंक्ति नोट करें, Dependency Worked!

यह काम करता है क्योंकि:

  1. आप ने कहा आप vendoring उपयोग कर रहे हैं, जिसका अर्थ है आप एक स्थानीय निर्देशिका आपके आवेदन कोड के रूट में ./vendor कहा जाता है।
  2. जब आप ADD . /go/src/app, तो आप अपने आवेदन कोड में ./vendor स्थानीय भी कॉपी कर रहे हैं।
  3. आपने पैकेजों को खोजने के लिए गो बिल्ड टूल्स द्वारा आवश्यक $GOPATH सेटअप संरचना में अपनी फ़ाइलों की प्रतिलिपि बनाई है (और इस मामले में, ./vendor निर्देशिका आपके स्रोत कोड के रूट फ़ोल्डर में)।
+0

प्रतिक्रिया के लिए Thx, मैंने आपके डॉकरफ़ाइल का उपयोग करने की कोशिश की, लेकिन जब मैं डॉकर रन कमांड चलाता हूं, तो ऐसा लगता है कि GOPATH सेट नहीं है। 'डेमॉन से त्रुटि प्रतिक्रिया: ओसीआई रनटाइम त्रुटि: निष्पादन: "$ GOPATH/src/app/myapp": stat $ GOPATH/src/app/myapp: ऐसी कोई फ़ाइल या निर्देशिका नहीं। – sbouaked

+0

डॉकरफ़ाइल ($ GOPATH isn' सीएमडी के लिए उपलब्ध नहीं)। एक पूर्ण कामकाजी उदाहरण भी जोड़ा (अब मैं लैपटॉप के साथ काम पर हूं)। – eduncan911