我有一个使用Golang的应用程序,我想基于this tutorial
对它进行“ dockerize”
这是项目结构:
Dockerfile:
FROM golang:1.12.7-alpine3.9
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN go build -o main .
CMD ["/app/main"]
当我尝试运行此命令时:
docker build -t pln-monitoring .
它显示此错误:
Step 5/6 : RUN go build -o main .
---> Running in 5e3c84f1435d
main.go:12:2: cannot find package "github.com/geshtng/pln-monitoring/middleware" in any of:
/usr/local/go/src/github.com/geshtng/pln-monitoring/middleware (from $GOROOT)
/go/src/github.com/geshtng/pln-monitoring/middleware (from $GOPATH)
main.go:13:2: cannot find package "github.com/geshtng/pln-monitoring/pkg/common/config" in any of:
/usr/local/go/src/github.com/geshtng/pln-monitoring/pkg/common/config (from $GOROOT)
/go/src/github.com/geshtng/pln-monitoring/pkg/common/config (from $GOPATH)
答案 0 :(得分:2)
之所以失败,是因为docker内部无法使用golang程序使用的依赖项来成功构建它。
您正在使用dep进行依赖项管理,即用于本地外部库,但尚未在docker中安装它们。
您需要通过在容器中安装第一个dep工具使其在docker中可用。 dep工具将确保dep ensure
此外,理想情况下,您还应按以下所述将存储库放入$ GOPATH中
dockerfile将需要以下命令:
FROM golang:1.12
RUN mkdir /go/src/app
RUN go get -u github.com/golang/dep/cmd/dep
ADD ./main.go /go/src/app
COPY ./Gopkg.toml /go/src/app
WORKDIR /go/src/app
RUN dep ensure
RUN go build
CMD ["./app"]
阅读有关dep
here