The Docker Environment variables can be declared during the (1) Docker image creation inside the Dockerfile and (2) During the docker container run time.
The Dockerfile ENV
syntax is shown as follows:
ENV VAR_NAME value
We will look at the Dockerfile syntax in the following snippet:
# Exporting the Environment Variables ENV MAVEN_HOME /usr/share/maven/ ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/ # Added as per Java team ENV JAVA_OPTS -Xms256m -Xmx768m
We can even reference the ENV variable after the declaration at the next statement.
RUN ${JAVA_HOME}/bin/java -version
Conclusion:
Setting the ENV is a lot better than Hard coding the values into the Dockerfile, as it makes maintaining the Dockerfile a simpler task.
Its a good practice for storing the path names, version of packages which are set to be modified over a longer course of time.