How to use variable inside a Dockerfile CMD

There are certain situations where the build argument need to use as variable in Dockerfile CMD. This article explains the way to accomplish this requirement.

An example Dockerfile snippet is given below for reference.

ARG value
ENV envValue=$value
CMD ["echo", "$envValue"]

Here the argument from docker build command will be assigned to an environment variable. Because ARG is only available during the build of a Docker image (RUN etc), not after the image is created and containers are started from it (ENTRYPOINT, CMD).

Then we need to specify the value as argument while building the Docker image.

docker build -t tagName --build-arg value="VAL"

Where VAL need to be replaced with the required value of variable.

That's all…