Dockerfile: how to update and pass build args between RUN commands or stages?

  docker, environment-variables

I am trying to write a Dockerfile that has two build args:

  • REPO
  • TAG

The repo is a GitHub URL and the TAG is a release version.
I would like to have only the REPO as required, while the TAG should be optional. However, I want to update its value with the latest release tag available and use it in other RUN commands or even better other build stages.

This is my docker-compose.yml

version: "3.5"
services:
  cve_search:
    image: cve_search
    build:
      context: .
      dockerfile: docker/cve-search/Dockerfile
      args:
        - REPO=${REPO:-https://github.com/cve-search/cve-search.git}
        - TAG=${TAG:-v4.0}
    environment:
      - REPO=${REPO:-https://github.com/cve-search/cve-search.git}
      - TAG=${TAG:-v4.0}

and this is my Dockerfile:

FROM python:alpine AS builder

# The repository url
ARG REPO
# The release tag
ARG TAG

# Print input args
# Repo 1: https://github.com/cve-search/cve-search.git
RUN echo Repo 1: $REPO
# Tag 1: v4.0
RUN echo Tag 1: $TAG

# Simulate new Tag version available from script
RUN TAG=v4.1.0 &&
    export TAG

# Test new tag
# Tag 2: v4.0
# How to fix it and print
# Tag 2: v4.1.0
# ?
RUN echo Tag 2: $TAG

The command to build the image is:

docker-compose build --no-cache

How do I make the last echo command to print the updated version of the TAG variable?

Source: Docker Questions

LEAVE A COMMENT