2016-10-31 2 views
5

私はベンダーでドッカーコンテナーを走ろうとしています。 これは私がそれは私のローカルマシンで働いている、LIBSをvendoringため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!") 
} 

ですドッキングウィンドウ:

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) 

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

私は次のエラーを持っています

私は何が欠けているのか分かりません。

答えて

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?

、あなたが上を行っていると仮定すると、そのエラーを修正する...

A Gopherのは、それ自体に必要有効な$GOPATHをビルドすることができます。これはあなたのDockerfileにはありません。

ここ
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!からの出力を出力し、最後の行。

ので、それは動作します:あなたはあなたがあなたのアプリケーションコードのルートに./vendorと呼ばれるローカルディレクトリを持っていることを意味Vendoringを、使用している述べ

  1. ADD . /go/src/appの場合は、ローカルの./vendorもアプリケーションコードにコピーしています。
  2. Goビルドツールがパッケージを見つけるために必要とする適切な$GOPATHセットアップ構造にファイルをコピーしました(この場合、ソースコードのルートフォルダ内の./vendorディレクトリ)。応答のための
+0

Thxを、私はあなたのDockerfileを使用しようとしましたが、私はドッキングウィンドウrunコマンドを実行すると、それはGOPATHが設定されていないように見えるのです。 'ociランタイムエラー:exec:" $ GOPATH/src/app/myapp ":stat $ GOPATH/src/app/myapp:そのようなファイルまたはディレクトリはありません。 – sbouaked

+0

Dockerfile($ GOPATH isn ' CMDに利用可能)。また、完全な実例も追加されました(今では私はラップトップと一緒に働いています)。 – eduncan911

関連する問題