Docker Environment Variables Examples-DockerFiles

Docker Environment Variables Examples-DockerFiles

Docker environment variables can be passed in to docker run command itself with the “-e” tag. “-e” tag takes the environment variable into the container and affects the runtime initiation of the container with the configuration provided. Below is the example which shows how to pass environment variables to the docker container.

sudo docker run -it -v /mongodata/:/data/db  -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=iamyourpassword -p 27017:27017 --name mongodb -d mongo

The above command used to start the mongodb container.

Env variable is passed using tag “-e”. Here, mongodb is initiated by applying an env variable namely,
“MONGO_INITDB_ROOT_USERNAME” and “MONGO_INITDB_ROOT_PASSWORD”. These specifies when the mongodb starts, it will have root user-name as “root” and the password for the root user will be “iamyourpassword”.

The rest part of the commands show;

“–name” tag means you can give the unique name to the container.

“-v ” specifies the volume mounting of the container and host system

“-p ” specifies the port binding functionality.

“-d ” specifies to run the container in daemon mode.

Congratulations, That’s all you need to know about the Docker Environment variable.

 

Leave a Reply

Your email address will not be published. Required fields are marked *