dtbi27903 2019-02-21 17:49
浏览 481
已采纳

Docker在go.mod文件中找不到依赖项

I'm working in a project with GoLang and Docker and using go.mod to maintain the dependencies. The project works fine when i build without docker, but when i try to build the docker image, this error appears:

internal/server/server.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)
cmd/root.go:7:2: cannot find package "github.com/spf13/cobra" in any of:
        /usr/local/go/src/github.com/spf13/cobra (from $GOROOT)
        /go/src/github.com/spf13/cobra (from $GOPATH)
make: *** [Makefile:30: build] Error 1

Dockerfile

# Build Stage
FROM lacion/alpine-golang-buildimage:1.9.7 AS build-stage

LABEL app="build-status-hub"
LABEL REPO="https://github.com/MSLacerda/status-hub"

ENV PROJPATH=/go/src/github.com/MSLacerda/status-hub

# Because of https://github.com/docker/docker/issues/14914
ENV PATH=$PATH:$GOROOT/bin:$GOPATH/bin

ADD . /go/src/github.com/MSLacerda/status-hub
WORKDIR /go/src/github.com/MSLacerda/status-hub

RUN make build

The Dockerfile actually has more instructions, but i placed here just only the part when the console shows the error.

The Makefile

build:
    @echo "building ${BIN_NAME} ${VERSION}"
    @echo "GOPATH=${GOPATH}"
    go build -ldflags "-X github.com/MSLacerda/status-hub/version.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/MSLacerda/status-hub/version.BuildDate=${BUILD_DATE}" -o bin/${BIN_NAME}

go.mod

module github.com/MSLacerda/status-hub

require (
    github.com/Sirupsen/logrus v1.0.6
    github.com/gorilla/mux v1.7.0
    github.com/spf13/cobra v0.0.3
    github.com/spf13/viper v1.2.0
    golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2 // indirect
)
  • 写回答

1条回答 默认 最新

  • doukuanghuan7582 2019-02-21 18:41
    关注

    What is happing?

    When you are trying to build the app the required deps are not installed in the docker image

    Why it occurs in the docker image and not in my local dev env?

    In your local dev env you install/get the required deps running commands like the following example.

    $ go get -u github.com/go-swagger/go-swagger/cmd/swagger
    

    But each container/image will be an empty/clean env. You need to tell for the docker container that is required to first install the deps.

    Solution

    • Use Makefiles to create task/commands for your project. See more over it here

    • Create a make file command to install the deps and build the project

    Makefile Example

    .PHONY: setup
    setup: @echo Installing dependencies:
        go get -u github.com/go-swagger/go-swagger/cmd/swagger
    
    .PHONY: build
    build: setup
        go build -o $(BINARY) $(APP_FILE)
    
    • Then the command make build need be called to create your image instead of the go build used currently.

    • Instead of using the go get commands for each dependency use the go dep manager to do it. Following the example.

    Makefile

    .PHONY: setup
    setup: @echo Installing dependencies:
        dep ensure
    

    See more about it here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装