2018-11-16 16:15
浏览 467


I am trying to get a Go app inside a docker container. This is my first bigger Go and Docker project. The go program runs just fine as long as I am running it on my local machine, now I want to run it on EC2 within a docker container. My docker file looks like this:

FROM golang:latest 
RUN mkdir /tir
ADD . /tir 
RUN go build -o main . 
CMD ["/app/main"]

But I get the following error for every private dependencies:

main.go:17:2: cannot find package "" in any of:
    /usr/local/go/src/ (from $GOROOT)
    /go/src/ (from $GOPATH)

When I insert RUN go get ./.. before RUN go build -o main ., I get the following error for every package:

fatal: could not read Username for '': terminal prompts disabled

package exit status 128

I have tried a couple of solutions, but none worked. I always end up having the above errors. Since this is my first docker + golang project, are there any ready to use dockerfiles for an golang app with public as well as private dependencies?

UPDATE: I deinstalled go, and copied every file in one by one and used dep -ensure after every file. Now it works, thanks :D

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在Docker容器中获取Go应用。 这是我的第一个更大的Go and Docker项目。 只要我在本地计算机上运行go程序,它就可以正常运行,现在我想在docker容器中的EC2上运行它。 我的docker文件如下所示:

  FROM golang:latest 
RUN mkdir / tir 
ADD。  / tir 
WORKDIR / tir 
RUN转到build -o main。  
CMD [“ / app / main”] 


 < 代码> main.go:17:2:在以下任何位置都找不到软件包“”:
 /usr/local/go/src/来自$ GOROOT)\  n /go/src/来自$ GOPATH)

当我插入 RUN时,获取./.. RUN运行build -o main之前。,对于每个软件包,我都会收到以下错误:

 致命:无法读取'https的用户名 ://':终端提示已禁用


我尝试了几种解决方案,但没有一个奏效。 我总是最终遇到上述错误。 既然这是我的第一个docker + golang项目,那么有没有准备好将dockerfiles用于具有公共和私有依赖关系的golang应用程序?

UPDATE: 我卸载了go,并复制了每个 文件一个接一个地使用,并在每个文件之后使用dep-确保。 现在可以了,谢谢:D

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douping3891 2018-11-16 17:50

    Your dependencies are probably stored in GOPATH/src/<import-path> and you manage them with go get tool.

    Consider vendoring and tools like dep or modules

    As a result your dependencies will be included into source control and project will be much more portable.

    You also can improve the way how you create Docker image.

    Current implementation uses one container that includes whole GO toolchain. Code is copied inside that that container, container compiles and hosts the code. Only later is necessary for production.

    Better option will be to use 2 containers:

    1. Go Tools for compilation
    2. Lightweight container that only host binaries
    # Debian image with the latest version of Go installed
    # and a workspace (GOPATH) configured at /go.
    FROM golang:1.11 as builder
    WORKDIR /go/src/
    # Copy the local package files to the container's workspace.
    ADD . /go/src/
    # Build the service inside the container.
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
    FROM centurylink/ca-certs
    EXPOSE 8080
    # Copy app
    COPY --from=builder /go/src/   /
    ENTRYPOINT ["/app"]
    解决 无用
    打赏 举报

相关推荐 更多相似问题