dongzantai7570 2018-12-20 00:44 采纳率: 0%
浏览 350
已采纳

运行在dockerfile中构建main.go

I'm trying to run go build hello.go inside a dockerfile, as

FROM golang

COPY hello.go /go/src/hello.go

RUN cd src/

RUN go build hello.go

RUN ./hello

It all goes well until the go build command, then it the following error message appears:

stat hello.go: no such file or directory
The command '/bin/sh -c go build hello.go' returned a non-zero code: 1

However, if I comment the last two commands on the dockerfile and run the created image, I can run the go build command with no problems.

Why is this happening?

  • 写回答

1条回答 默认 最新

  • duanbenzan4050 2018-12-20 00:56
    关注

    Every RUN command starts a new shell with a clean environment and a new default working directory. In particular, RUN cd ... as an isolated step does nothing.

    If you do actually need to change directories in a Dockerfile, either combine the two steps into one or use a WORKDIR directive to make the change more globally.

    In the specific case of a Go repository, since there's a standard directory layout that's at least very strongly encouraged, I'd run with it:

    FROM golang
    WORKDIR /go/src/github.com/me/myprogram
    COPY . ./
    RUN go install .
    CMD ["/go/bin/myprogram"]
    

    (In general Go deals with whole directories or "packages" of related files, and not necessarily individual .go files.)

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog