How can I use a variable inside a Dockerfile CMD?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding how to use a variable within a CMD instruction in a Dockerfile is crucial for creating flexible and modular Docker images. Here, we delve into how this can be achieved, explaining the use of environment variables, ARG instructions, and shell expansion commands. By the end of the article, you will have a comprehensive understanding of how to efficiently incorporate variables in the CMD instruction of a Dockerfile.
Incorporating Variables in CMD Instructions
The CMD instruction in a Dockerfile is used to provide default arguments for an ENTRYPOINT command or to define what command should be run by default when running a container. Using variables within CMD can help to make these Docker images more dynamic and adaptable to different environments or staging setups.
Using Environment Variables
One of the ways to use a variable in a Dockerfile is through environment variables. The ENV instruction allows you to set environment variables that are available within the Docker container.
In the example above, we define an environment variable MY_VAR and then use it in the CMD instruction. This allows the container to print the value of MY_VAR when it starts.
However, it is important to note that when using CMD with shell form (e.g., CMD ["executable", "param1", "param2"]), the shell does not do variable substitution. You need to use the exec form for variable substitution to automatically take place.
Specifying Variables at Build Time
To pass variables at build time, you can make use of the ARG instruction. This allows you to define variables that can be replaced during the build but are not inherited in the runtime environment:
When building the Docker image, you can pass the argument using --build-arg:
This approach allows flexibility while building the image, where values can be changed on different builds.
Using Shell Expansion
Shell expansion can be used in shell form of CMD or ENTRYPOINT to perform operations directly based on the environment variables or ARGs defined:
Here, the shell form of CMD (/bin/sh -c ...) allows us to leverage shell expansion to substitute the variable at runtime.
Table Summary
Here's a summary of the key points discussed:
| Concept | Dockerfile Syntax Example | Description |
| Environment Variable | ENV VAR_NAME="value"
CMD echo $VAR_NAME | Persistent throughout container runtime; used in CMD with exec form. |
| Build Argument | ARG ARG_NAME
ENV VAR_NAME=$ARG_NAME | Used at build time; scoped to the Dockerfile stage and can be replaced during build. |
| Shell Expansion | CMD ["/bin/sh", "-c", "command with $VAR"] | Uses shell form to enable runtime variable substitution in commands. |
Additional Considerations
- Use Correct CMD Form: Remember, for variable substitution within
CMD, you must use shell form if expecting variable expansion at runtime. - Persistent Environment Variables: Environment variables defined with
ENVpersist in the container and can be accessed by any subsequent instructions. - Security Concerns: Avoid putting sensitive information directly in environment variables within Dockerfiles as these can be exposed unintentionally. Use Docker Secrets for managing sensitive data.
- Debugging: Use
docker inspect [container_id]or/usr/bin/envwithin a shell inside the container to inspect environment variables.
Understanding how to manipulate variables within Dockerfiles enhances your ability to create flexible, maintainable, and well-optimized Docker images suitable for varied deployment environments.

