Build from specific tag without changing Dockerfile?

In a largish application, our CI builds a base image that is then used as the parent image for the image containing the application itself. The base image can change in a particular feature branch, so it’s important that the base image for that branch is used to build the application image for that branch. Because there can be multiple concurrent builds on different branches underway, using latest isn’t really what we want.

We’ve been addressing this use case by passing the Dockerfile through sed to add a tag that identifies the feature branch to the FROM directive. So when building the application image, we replace FROM namespace/base with FROM namespace/base:branchTag.

This works, but it seems like something that would be a common enough need that there should be a way to do this without modifying the Dockerfile; i.e. it seems like I ought to be able to specify a tag for the FROM image on the docker build command line. Am I missing something here?

Presumably the original author is no longer waiting for a reply, but since the question has generic value, representing a situation I myself recently encountered, I will provide a solution.

The Dockerfile may open with an ARG command, which may be interpolated in subsequent statements in the file, and the value passed using the --bulid-arg parameter to docker build.

As an example, consider the following Dockefile:

ARG tag=latest
FROM namespace/base:${tag}

You may then override the tag latest by providing --build-arg tag=branchTag in your call to docker build.