Docker Community Forums

Share and learn in the Docker community.

Referencing container env vars in CMD

I am trying to write a default-parameters CMD that references a container environment variable. (The container specifies an entrypoint, so I cannot use the exec form, and I am using AWS Fargate, so I cannot use the shell form.)

I see from the documentation: [1]

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "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: CMD [ "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.

I want to pass the container environment variable into entrypoint option -d, which, following the documentation, leads me to something like this:
["-d", "sh", "-c", "echo $FOOBAR"]. But, of course, this doesn’t work because it doesn’t know to evaluate the sh command, but instead treats sh as a string that I am assigning to option -d.

Thank you, in advance, for any suggestions.

[1] Dockerfile reference | Docker Documentation

I’m afraid you’ll have to copy the original entrypoint (if it’s a script), and adjust that to do the substitution? Or create an entrypoint to do that and call the original binary.