☰ See All Chapters |
Docker ENV command
The ENV command sets environment variables within the image both when it is built and when it is executed. These variables can be overridden when you launch your image. An environment variable is a name-value pair, which can be accessed by any script or application that runs from the image.
How to set ENV Values
Set ENV values form dockerfile
Syntax of ENV command in dockerfile
ENV <vaiablename> <variablevalue> OR ENV <vaiablename>=<variablevalue> <vaiablename>: Name of the variable <variablevalue>: Value of the variable |
Example
ENV DEBUG_LVL ERROR ENV LOG_DIR /app/log/ |
ENV variable can also be references as ARG variables form inside dockerfile.
FROM openjdk:13-jdk-alpine ARG SOURCE_FILE=target/*.jar ENV JAR_FILE app.jar COPY ${SOURCE_FILE} ${JAR_FILE} ENTRYPOINT ["java","-jar","/app.jar"] |
How to provide values for ENV variables to containers
While executing the docker run command provide the name of the variable using the -e flag. This will make Docker access the current value in the host environment and pass it on to the container.
docker run -p 8080:8080 springbootapp –e DEBUG_LVL LOG_DIR |
Set ENV values form docker run command
Using the -e flag set the ENV variables from run command one by one as shown below:
docker run -p 8080:8080 springbootapp –e ENV DEBUG_LVL=ERROR ENV LOG_DIR=/app/log/ |
Variable name and value should be separated by = and not by space. Values set from docker run command overrides the values set from dockerfile.
Set ENV values form from a file (env-file)
You can create a file with key value pairs representing environment values. Later you can specify this file in docker run command.
The contents of environment file looks like below:
DEBUG_LVL=ERROR LOG_DIR=/app/log/ |
Now you can specify the environment file name to docker run command as below:
docker run -p 8080:8080 springbootapp --env-file=env_file_name |
Values set from file through --env-file flag overrides the values set from dockerfile.
All Chapters