I can't seem to figure out how to provide a build time variable using the docker-compose file. I have done quite a bit of research and still can't figure the way out. What I'm trying to do is to burn my latest commit hash as a value to the version variable in my binary executable which is used by my server. I'm using windows as a side note. Here's my dockerfile
# Build Stage
FROM golang:1.18-alpine3.16 AS builder
WORKDIR /app
COPY . .
ARG VERSION
ENV VERSION=$VERSION
RUN GOOS=linux GOARCH=amd64 go build -ldflags='-s -X main.version=$VERSION' -o main
./cmd/api
RUN apk add curl
RUN curl -L https://github.com/golang-
migrate/migrate/releases/download/v4.15.2/migrate.linux-amd64.tar.gz | tar xvz
# Run Stage
FROM alpine:3.16
WORKDIR /app
COPY --from=builder /app/main .
COPY --from=builder /app/migrate ./migrate
COPY app.env .
COPY start.sh .
COPY wait-for.sh .
COPY /migrations ./migration
EXPOSE 3001
CMD [ "/app/main" ]
ENTRYPOINT [ "/app/start.sh" ]
The version variable should be having my latest commit hash. Here's my docker-compose file
version: "3.9"
services:
postgres:
build:
context: .
dockerfile: db.dockerfile
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=blogpost
ports:
- "542:5432"
api:
build:
context: .
dockerfile: app.dockerfile
args:
- VERSION=git describe --always --dirty <<< Trying to pass value here
ports:
- "3001:3001"
environment:
- DB_DSN=postgres://root:secret@postgres:5432/blogpost?sslmode=disable
depends_on:
- postgres
entrypoint: [ "/app/wait-for.sh", "postgres:5432", "--", "/app/start.sh" ]
command: [ "/app/main" ]
I have tried this too
args:
- VERSION=$$(shell git describe --always --dirty)
In the end, the value for my version variable ends up being "$VERSION". Please guide me on this