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 ARG MY_VARIABLE ENV MY_VARIABLE $MY_VARIABLE 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 ARG MY_VARIABLE RUN test -n "$MY_VARIABLE" ENV MY_VARIABLE $MY_VARIABLE RUN ...
but it doesn't seem to be a very idiomatic solution to the problem at hand.
If you want to pass multiple build arguments with docker build command you have to pass each argument with separate — build-arg. docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .
Running containers can't access values of ARG variables. This also applies to CMD and ENTRYPOINT instructions which just tell what the container should run by default.
ENV is for future running containers. ARG for building your Docker image. ¶ ENV is mainly meant to provide default values for your future environment variables.
With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...
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 ARG MY_VARIABLE RUN test -n "$MY_VARIABLE" ENV MY_VARIABLE $MY_VARIABLE
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 <[email protected]> ARG MANDATORY_BUILD_ARGUMENT 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 :)
As mentioned in @Jeffrey Wen's answer, to make sure that this errors out on a centos:7
image (and possibly others, I admittedly haven't tested this on other images than stretch-slim
):
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.}"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With