Moazzem Hossen
Moazzem Hossen

Reputation: 2516

Evaluate bash expression in Dockerfile

I'm trying read application version from a file VERSION such that echo -n 1.0.0 > VERSION, and store the version number in an environment variable, lets say, VERSION. My Dockerfile

FROM debian
WORKDIR /app
COPY VERSION .
ENV VERSION $(cat VERSION)
# I'd like to use the version number in later steps like
RUN apt update && apt install -y curl
RUN curl path/to/executable-${VERSION}

env | grep VERSION returns:

VERSION=$(cat VERSION)

I want

VERSION=1.0.0

Upvotes: 3

Views: 1648

Answers (2)

ShayK
ShayK

Reputation: 429

ENV does not interpolate variables.

How about this:

FROM debian
WORKDIR /app
COPY VERSION version-file
RUN echo "export VERSION=$(cat version-file)" >> /root/.bashrc
RUN apt update && apt install -y curl
RUN curl path/to/executable-${VERSION}

This uses a RUN step to add an export command to .bashrc file. the export command adds the VERSION env var.

Upvotes: 2

alex067
alex067

Reputation: 3301

You can provide the version number as a build argument

FROM debian
ARG VERSION
WORKDIR /app
ENV VERSION=$VERSION
# I'd like to use the version number in later steps like
RUN apt update && apt install -y curl
RUN curl path/to/executable-${VERSION}

Then you can build it with:

docker build --build-arg $VERSION .

Upvotes: 1

Related Questions