3

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
1
  • you can not set value for ENV using subshell as it key pair only, but you can try suggested answer. or if you still want from file then try something like RUN curl path/to/executable-$(cat VERSION) Commented Nov 27, 2019 at 18:36

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

looks like an easy workaround for my case. build-arg seems a better option though
1

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 .

3 Comments

Thanks for the answer. CI tool builds the image. I'll see if I can pass as argument.
which CI tool are you using?
concourse registry-image resource. github.com/vito/oci-build-task. It takes BUILD_ARG

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.