Environment variables
Environment variables
Environment variables example of using an environment variable in a Dockerfile:
In this example, the environment variable MY_VAR is set to the value “Hello World”. This variable is then used in the RUN command to create an output.txt file with the contents “Hello World”.
To build the Docker image using this Dockerfile, you can use the following command:
You can also set the value of the environment variable when you build the image using the --build-arg
flag:
This will set the value of MY_VAR to “Goodbye” when the image is built.
It is also a good idea to use default values for environment variables in your Dockerfile. This way, if the variable is not set when the image is built, it will use the default value. You can do this by using the ARG directive in your Dockerfile and then using the := operator to set the default value:
In this example, if the value of MY_VAR is not set when the image is built, it will default to the value “Hello World”.
Last updated 8 days ago.