I have a Dockerfile where I'm building the image from go source and I need to pass the -ldflags as an argument to the go build command. Below is a snippet of the Dockerfile:
// from statement and other stuff
...
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
which I changed to following:
// from statement and other stuff
...
ARG GO_FLAGS
# Build
# Below line throws error
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ${GO_FLAGS} -a -o manager main.go
# Below works though
# RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X <package-path>.BuildVersion=0.1.1-1" -a -o manager main.go
and I'm building docker image using:
docker build --build-arg GO_FLAGS='-ldflags "-X '<package-path>.BuildVersion=0.1.1-1'"' -t controller:test .
However, I'm getting below error:
[builder 15/15] RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X <package-path>.BuildVersion=0.1.1-1" -a -o manager main.go:
0.607 invalid value "\"-X" for flag -ldflags: missing =<value> in <pattern>=<value>
0.607 usage: go build [-o output] [build flags] [packages]
0.607 Run 'go help build' for details.
------
Dockerfile:42
--------------------
40 |
41 | # Build
42 | >>> RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ${GO_FLAGS} -a -o manager main.go
I initially thought that this could be due to quotes not being handled properly by the shell. However, I've tried using multiple ways of quoting(single/double) and still see the same issue. I'm relatively new to docker and go so I'm not able to figure out what's causing this issue.
So, I want to know if there is a way to pass the build flags as argument to the Dockerfile?