红酒泡绿茶 2018-01-03 15:21
浏览 78

在Docker构建期间无法`获取`依赖项

I'm fairly new to both Docker and go, so this might be something obvious, but my google searches haven't found anything.

I'm trying to build a simple go program with docker, but I'm havign trouble with dependencies.

go file:

package main

import (
    "fmt"
    "log"
    "html"
    "net/http"

    "github.com/gorilla/mux"
)

func hello(writer http.ResponseWriter, r *http.Request) {
    path := mux.Vars(r)["rest"]
    fmt.Fprintf(writer, "Hello, %q", html.EscapeString(path))
}

func main() {
    router := mux.NewRouter().StrictSlash(true)

    router.HandleFunc("/{rest:.*}", hello)

    log.Println("Listening...")
    log.Fatal(http.ListenAndServe(":8080", router))
}

Docker file:

FROM golang:latest
RUN mkdir /app
ADD ./HelloWorld.go /app/
WORKDIR /app
RUN go get ./*.go
RUN go build -o main .
CMD ["/app/main"]

Error:

Sending build context to Docker daemon  6.482MB
Step 1/7 : FROM golang:latest
 ---> 138bd936fa29
...
Step 5/7 : RUN go get ./*.go
 ---> Running in 1e29844961a2
HelloWorld.go:9:5: 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)
The command '/bin/sh -c go get ./*.go' returned a non-zero code: 1
  • 写回答

2条回答 默认 最新

  • dourang8305 2018-01-05 10:12
    关注

    You may use my Dockerfile as a base. First stage produces working image. That’s enough for many cases. https://github.com/lisitsky/go-site-search-string/blob/light-docker/Dockerfile

    If you want to shrink image size from ~800MB to about ~10-20MB use second stage too.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分