Search code examples
dockergitlab-cisentry

Dockerfile env variable


I want to create env variable inside Dockerfile.app which will be used by Sentry to version my build

This is what I have inside that file:

ENV REACT_APP_RELEASE_VERSION=$(git rev-parse --short HEAD)-$(date -u '+%Y-%m-%dT%H:%M:%SZ')

Its working fine in bash:

REACT_APP_RELEASE_VERSION=2a06954-2020-07-01T07:41:49Z

but while pipeline building my project, it throws error:

Error response from daemon: failed to parse Dockerfile.app: Syntax error - can't find = in "rev-parse". Must be of the form: name=value

Any ideas?


Solution

  • You can not perform variable expansion in Dockerfile ENV, as you can use it in bash but not in Dockerfile ENV.

    To deal with you can perform git operation on the host and pass the plain value to Dockerfile env or ARG during build time.

    Run time:

    docker run -it --rm -e REACT_APP_RELEASE_VERSION=$(git rev-parse --short HEAD)-$(date -u '+%Y-%m-%dT%H:%M:%SZ') my_image
    
    

    Now you will be able to use REACT_APP_RELEASE_VERSION its value in your application.

    Build time:

    docker build `--build-arg` REACT_APP_RELEASE_VERSION=$(git rev-parse --short HEAD)-$(date -u '+%Y-%m-%dT%H:%M:%SZ') -t my_image .
    

    and update Dockerfile

    ARG REACT_APP_RELEASE_VERSION
    ENV REACT_APP_RELEASE_VERSION=${REACT_APP_RELEASE_VERSION}