drg5577 2019-01-24 12:22
浏览 672
已采纳

在dockerfile中的go build命令中注入或内插多个ENV变量

I'm using a dockerfile to build go code, and I'm trying to pass 3 options in -ldflags option. Two of these flags comes from ENV variables, and I have to inject them in -ldflags content, thru string interpolation or concatenation, but I don't know how.

The objective is to inject git revision hash and current timestamp in two variables in main.go

It can be done by creating a file from dockerfile with "echo" command, but I want to be sure it's not possible with simple variables interpolation/concatenating

ENV GIT_REVISION $( git rev-parse --short HEAD )

ENV COMPILATION_TIMESTAMP $( date +%Y%m%dT%H:%M:%S )

RUN go get -d -v

// This one works:
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o myprogram .

// This one, with those variables, fails:
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags "-extldflags '-static' -X main.compiledOn=${COMPILATION_TIMESTAMP} -X main.gitRevisionHash=${GIT_REVISION}" -o myprogram .
  • 写回答

2条回答 默认 最新

  • dongshu9458 2019-01-25 04:25
    关注

    Unfortunately none of the current Docker builder command support environment variable replacement. Your best bet would be to write a shell script, where environment variable replacement is a first class citizen. Then, when you call RUN ./script you'll be able to catch the ENV values from the previous layers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置