How to make a build arg mandatory during Docker build?

docker build args
docker build-arg not working
docker build context path
one or more build-args were not consumed
dockerfile arg
docker build --no-cache
docker build with multiple build arg
docker build env file

Is there any way to make a build argument mandatory during docker build? The expected behaviour would be for the build to fail if the argument is missing.

For example, for the following Dockerfile:

FROM ubuntu


RUN ...

I would like the build to fail at ARG MY_VARIABLE when built with docker build -t my-tag . and pass when built with docker build -t my-tag --build-arg MY_VARIABLE=my_value ..

Is there any way to achieve that behaviour? Setting a default value doesn't really do the trick in my case.

(I'm running Docker 1.11.1 on darwin/amd64.)

EDIT: One way of doing that I can think of is to run a command that fails when MY_VARIABLE is empty, e.g.:

FROM ubuntu

RUN test -n "$MY_VARIABLE"

RUN ...

but it doesn't seem to be a very idiomatic solution to the problem at hand.

I tested with RUN test -n <ARGvariablename> what @konradstrack mentioned in the original (edit) post... that seems do the job of mandating the variable to be passed as the build time argument for the docker build command:

FROM ubuntu

RUN test -n "$MY_VARIABLE"

Understanding Docker Build Args, Environment Variables and , How can you set variables during build time? Who gets what environment vars, and what can you do to change them? How to override defaults or set your own in� Dockerfiles have been able to use ARGs to allow passing in parameters during a docker build using the CLI argument --build-arg for some time. But until recently (Docker's 17.05 release, to be precise), you weren't able to use an ARG to specify all or part of your Dockerfile's mandatory FROM command.

You can also use shell parameter expansion to achieve this.

Let's say your mandatory build argument is called MANDATORY_BUILD_ARGUMENT, and you want it to be set and non-empty, your Dockerfile could look like this:

    FROM debian:stretch-slim
    MAINTAINER Evel Knievel <evel@kniev.el>


    RUN \
    # Check for mandatory build arguments
        : "${MANDATORY_BUILD_ARGUMENT:?Build argument needs to be set and non-empty.}" \

    # Install libraries
    &&  apt-get update \
    &&  apt-get install -y \
            cowsay \
            fortune \

    # Cleanup
    &&  apt-get clean \
    &&  rm -rf \
            /var/lib/apt/lists/* \
            /var/tmp/* \
            /tmp/* \

    CMD ["/bin/bash", "-c", "/usr/games/fortune | /usr/games/cowsay"]

Of course, you would also want to use the build-argument for something, unlike I did, but still, I recommend building this Dockerfile and taking it for a test-run :)

Using Docker Build Arguments In CI/CD, One of the great things about Docker builder is that it allows you to pass arguments that can be consumed in the context of the Dockerfile. Once our Dockerfile is ready we will use the docker build command to actually build the image. The first thing we should do is to create a new directory to host our project. For the sake of this tutorial we will build an image containing the Apache web server, so we will name the root directory of the project "dockerized-apache":

You could do something like this...

 FROM ubuntu:14.04
 ONBUILD RUN if [ -z "$MY_VARIABLE" ]; then echo "NOT SET - ERROR"; exit 1; else : ; fi

Then docker build -t my_variable_base .

Then build your images based on this...

FROM my_variable_base

It's not super clean, but at least it abstracts the 'bleh' stuff away to the base image.

Making use of Docker build arguments | by Ahmed ElGamil, The build's context is the set of files at a specified location PATH or URL . Do not use your root directory, / , as the PATH as it causes the build to transfer the entire to a new image if necessary, before finally outputting the ID of your new image. Note however, that whitespace in instruction arguments, such as the� * Add an ARG command before FROM statement in Dockerfile.user for sonic-slave and sonic-slave-stretch. ARG variable defaults to latest if slave_base_tag_ref not specified in * Check build host for minimum Docker version supporting ARG before FROM.

Another simple way:

RUN test -n "$MY_VARIABLE" || (echo "MY_VARIABLE  not set" && false)

Dockerfile reference, These values persist in the built image. set at build-time using the --build-arg flag: Also, these values don't persist in the intermediate or final images like ENV values do. Docker has the ability to build images by piping Dockerfile through stdin with a local or remote build context. Piping a Dockerfile through stdin can be useful to perform one-off builds without writing a Dockerfile to disk, or in situations where the Dockerfile is generated, and should not persist afterwards.

I cannot comment yet because I do not have 50 reputation, but I would like to add onto @Jan Nash's solution because I had a little difficulty getting it to work with my image.

If you copy/paste @Jan Nash's solution, it will work and spit out the error message that the build argument is not specified.

What I want to add

When I tried getting it to work on a CentOS 7 image (centos:7), Docker ran the RUN command without erroring out.


Ensure that you're executing the RUN command with the bash shell.

RUN ["/bin/bash", "-c", ": ${MYUID:?Build argument needs to be set and not null.}"]

I hope that helps for future incoming people. Otherwise, I believe @Jan Nash's solution is just brilliant.

docker build, Dockerfile s have been able to use ARG s to allow passing in parameters during a docker build using the CLI argument --build-arg for some time. an ARG to specify all or part of your Dockerfile 's mandatory FROM command� The Docker Quickstart training module teaches you how to: Set up your Docker environment (on this page) Build and run your image. Share images on Docker Hub. Docker concepts. Docker is a platform for developers and sysadmins to build, run, and share applications with containers.

Use an ARG in Dockerfile FROM for dynamic image specification , Runtime arguments are passed in when you docker run or start your container: To do this we could specify a build argument in the following way: supply, values that differ per build but are required to build our application: In your Dockerfile, if you specify ARG before the FROM instruction, ARG is not available in the build instructions under FROM. If you need an argument to be available in both places, also specify it under the FROM instruction. Refer to the understand how ARGS and FROM interact section in the documentation for usage details.

Arguments and variables in Docker, Dockerfile - automate the steps of creating a Docker image When the Dockerfile has been created, call the docker build command, using committing the result to a new image if necessary, before finally outputting the ID of the new image. The ARG instruction defines a variable that users can pass at build-time to the� And it'll make images made in this way undistributable because it contains proxy accounts and passwords in docker history. This patch hashes certain build args (currently only http_proxy, https_proxy and ftp_proxy), so we can keep these information private and don't have side effects as the approach in #18702.

Dockerfile(5) — — Debian experimental — Debian , FROM can appear multiple times within a single Dockerfile in order to create multiple images. If CMD is used to provide default arguments for the ENTRYPOINT instruction, "<dest>"] (this form is required for paths containing whitespace) Build-time variable values are visible to any user of the image with the docker� Local + Docker. The build process is much faster if KVM acceleration is available and easier to debug if VNC is available (see Windows installation progress). Below are instructions how to build system QEMU image on the host then pass it to Docker build context to finalize Docker image. The process should take around 45-60 mins.

  • It is a little odd that it will fail if an arg is passed that isn't defined but not the other way around, but that seems to be the way it works. You could run a "" script as late-stage step in your dockerfile that checks for a range of conditions and handles them appropriately. Depending on the complexity, it may be easier to manage as a separate file. If it's just one or two ARGs, your in-line method may in fact be the best.
  • @konradstrack have you considered accepting an answer? :)
  • That doesn't work when using ONBUID, only when I remove them.