Docker Community Forums

Share and learn in the Docker community.

Passing variables to an automated build - DOCKER_TAG

I have a project with auto-builds set up for each major branch.

Now I would like to be able to trigger an auto-build that will overlay a branch with code from a pull request.

The following page mentions a variable DOCKER_TAG that is passed to a build.

Is there a way to access this variable within my Dockerfile?

Here is what I hope to do.

# Assign the env variable DOCKER_TAG to an arg of the same name
ARG DOCKER_TAG=${DOCKER_TAG}

...

# if DOCKER_TAG is in the format prNNNN then merge code for that PR on top of the current branch
RUN PRNUM=`echo ${DOCKER_TAG}| egrep "^pr([0-9]+)$" | sed -e s/pr//` && \
    if [ -n "$PRNUM" ]; \ 
    then echo "Merging $PRNUM"; \
      curl -o /tmp/pr.patch -L https://github.com/DSpace/DSpace/pull/$PRNUM.diff; \
      git apply /tmp/pr.patch; \
    fi

Is this possible? Can you offer any advice on this issue?

I have also posted the issue here: https://stackoverflow.com/questions/55347752/dockerhub-autobuild-for-a-github-pull-request-using-the-docker-tag-variable

1 Like

I found a solution that seems to work. I created a build hook names hooks/build and pass the variable explicitly.

#!/bin/bash
docker build --build-arg DOCKER_TAG=$DOCKER_TAG -f $DOCKERFILE_PATH -t $IMAGE_NAME .

See https://docs.docker.com/docker-hub/builds/advanced/#custom-build-phase-hooks