Saturday, 9 May 2020

Docker: ENV statement: Set environment variables

ENV statement is used to set environment variables to a value.

Syntax
ENV <key> <value>
ENV <key>=<value> ...

ENV <key> <value>
This syntax is used to set value to a single variable.

Example
ENV product_name Versioning Control

ENV <key>=<value> ...
This syntax is used to define multiple variables.

Example
ENV product_name="Versioning Control" version=1.2.3

Step 1: Create a directory 'myDockerImage'.

Step 2: Create ‘Dockerfile’ in the directory 'myDockerImage'.
Dockerfile
FROM ubuntu

CMD echo "Welcome to Docker Application Development"

ENV product_name="Versioning Control" version=1.2.3

Step 3: Go to the directory where Dockerfile is located and execute below command.

docker image build -t welcomedocker .
$docker image build -t welcomedocker .

Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/3 : CMD echo "Welcome to Docker Application Development"
 ---> Running in eeb2e9fe74ad
Removing intermediate container eeb2e9fe74ad
 ---> cb04a6a6b66b
Step 3/3 : ENV product_name="Versioning Control" version=1.2.3
 ---> Running in a617d9f9fb31
Removing intermediate container a617d9f9fb31
 ---> 0e9cc7d92630
Successfully built 0e9cc7d92630
Successfully tagged welcomedocker:latest

Step 4: Execute the command 'docker image ls', you can see all the images.
$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
welcomedocker       latest              0e9cc7d92630        32 seconds ago      88.9MB
busybox             latest              e4db68de4ff2        4 days ago          1.22MB
jboss/wildfly       latest              4e440230e01d        8 days ago          748MB

Step 5: Execute below command to get the environment variable product_name details.

docker run welcomedocker env | grep product_name

$docker run welcomedocker env | grep product_name
product_name=Versioning Control





Previous                                                    Next                                                    Home

No comments:

Post a Comment