Building image with different parameters

What is the best way to build a Docker image with dynamic parameters that can change


When you run a container, you can populate the environment with whatever you need to:

$ docker run -e FOO=bar busybox env

If you are asking about how to parameterize the build step at build time, there is actually a new feature in 1.9 that does this. It’s called ARG:

That means I can take my Dockerfile:

FROM busybox:latest
ARG foo=bar
ENV bing=$foo
CMD echo $bing

now I can build the image: docker build -t darg . and run it: docker run darg and get bar as the output. If I build it differently: docker build --build-arg foo=baz -t darg . and run it the same way, I’ll get baz as the output.

The CMD line doesn’t directly interpret an ARG, so that’s why there is an intermediate ENV option that does consume the ARG.