Search code examples
bashdockerdockerfile

How can I evaluate and assign default value to ARG in dockerfile?


Basically I want to assign a dynamically calculated default value for ARG in my Dockerfile:

FROM node:20-alpine

ARG RELEASE_TAG=$(git describe --tags --always)

# Other commands...

So, far it doesn't interpolate the value of $(git describe --tags --always) and simply assigns this value to the RELEASE_TAG variable.

Is there any way I can achieve this?


Solution

  • You can achieve the expected effect by putting it in RUN :

    FROM ubuntu
    
    WORKDIR /app
    
    COPY . .
    
    RUN apt-get update && apt-get install -y git
    RUN RELEASE_TAG=$(git describe --tags --always) &&\
        echo npm run build -- --version=$RELEASE_TAG
    
    CMD sleep inf