skip to Main Content

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?

2

Answers


  1. You can use the --build-arg argument with docker build

    export RELEASE_TAG=$(git describe --tags --always)
    docker build --build-arg RELEASE_TAG=$RELEASE_TAG -t your_image_name .
    

    And inside your Dockerfile:

    FROM node:20-alpine
    
    ARG RELEASE_TAG
    

    That way, RELEASE_TAG will now have the value of git describe --tags --always output.

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search