Use docker run command to pass arguments to CMD in Dockerfile

Make sure your Dockerfile declares an environment variable with ENV:

ENV environment default_env_value
ENV cluster default_cluster_value

The ENV <key> <value> form can be replaced inline.

Then you can pass an environment variable with docker run. Note that each variable requires a specific -e flag to run.

docker run -p 9000:9000 -e environment=dev -e cluster=0 -d me/app

Or you can set them through your compose file:

node:
  environment:
    - environment=dev
    - cluster=0

Your Dockerfile CMD can use that environment variable, but, as mentioned in issue 5509, you need to do so in a sh -c form:

CMD ["sh", "-c", "node server.js ${cluster} ${environment}"]

The explanation is that the shell is responsible for expanding environment variables, not Docker. When you use the JSON syntax, you’re explicitly requesting that your command bypass the shell and be executed directly.

Same idea with Builder RUN (applies to CMD as well):

Unlike the shell form, the exec form does not invoke a command shell.
This means that normal shell processing does not happen.

For example, RUN [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: RUN [ "sh", "-c", "echo $HOME" ].

When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

Leave a Comment