Docker Community Forums

Share and learn in the Docker community.

Building image with different parameters


(Gidis) #1

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


(Jeff Anderson) #2

Hello,

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

$ docker run -e FOO=bar busybox env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=b128e8fc7e0f
FOO=bar
HOME=/root

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: https://github.com/docker/docker/blob/master/docs/reference/builder.md#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.

Cheers!