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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛