Hello and welcome back.
In this lesson we're going to learn about environment variables in containers.
If you remember we spoke about environment variables into darker file in the image section and we learn
how to create environment and variables for our images.
So now let's just recap on that.
Let's go to our doctor images folder and let's take a look at any of these Doctor files.
We are going to use any of this and we can do is go ahead in that for example a new environment variable
here and we can say var 1 and the is going to be 1 2 3 4 for example.
Now if we go ahead and build this image if we just type Doctor build dash T.
And for example and then you just parse your context and you hit enter then your image will be built.
Well we're gonna need to rebuild it again so it may take a while okay.
It already finished and once your image is it then you can do is take a look at your image which is
an F and if you type darker images it grab f then you're going to see your image here which was build
13 seconds ago.
And if you take a look at the dark profile then you will see that in this image we should have one environment
variable called bar one and the should be one two three four.
So now let's take a look at that we're running containers by typing Docker Pierce and let's remove the
Jenkins container because we're not using it let's just remove that container and now that it's removed
we're going to create a new container using the image that we created.
So organize a darker run that D dash dash.
Name.
And we're going to use the M image in the latest version because this is how we build that image.
And if you hit enter the net container using the image that we delete build will be created.
So if you tap Docker P.S. you will see your end container up and running seven seconds ago.
Now that you know that you can go inside of a container let's go inside of that container.
And please remember that these container is a completely different file system from your doctor host.
So if you take a look at the environment variables using the EMF command in Linux and you grab for bar
one then you are going to see that we have one environment variable with that value.
But if you go outside of the container and you execute exactly these commands then you are not going
to see anything because these variable is available only inside of the container and not outside of
the container.
So if we want to take a look at that variable we need to go inside of the container and if we do echo
var one then you are going to see that here we have the value.
Please remember that these value in the container is being taken from the dog profile.
Please remember that in these Docker file we set an M instruction that defines an environment variable
called bar one and a value for that verbal is one two three four.
And although we created a container and now that we're inside of that container you see that the variable
is survey level inside of a container and that's super good.
Now the question is you already knew these you know how to create environment variables for images but
how do we create an environment of variables for a container.
Well that's a good question.
Let's try to look for these but I will say it could test.
Of course we have nothing here because that level doesn't exist.
But what if I want to create only a variable called test in the runtime execution only the container.
But without using the docker file how would I do it.
Well that's XY that of this container and let's remove that container which is called environment I
think.
Yes let's remove that and now let's create a new container here.
And here is the new thing that I want to tell you and it is that in that dock run command we have an
option which is dash e and it stands for a dash dash environment.
And basically if you type dash e you can just parse one environment variable for a container and if
you see this is outside of the Ducker file it doesn't really matter if you have environment variables
in your Ducker file or if you don't have because if you pass these dash e instruction and you pass for
example test equals two four three two one and you hit enter then a new container will be created and
then you container will have the environment variables that are defined the image and now it will have
these new environment variables that we just define here.
So now if we just take this hash and if we go inside of the container remember that you can use the
hash or name if you go inside of the container and knife you type environment grep cast you're going
to see that the environment variables that we set here is a base level instead of the container we felt
the need of a darker file but if you type f and you grab for a bar one which is that one that it's been
taken from the docker file it's indeed heater.
So basically you can have in that container variables coming from your Ducker file or you kind of have
bubbles coming from the creation of a container by passing dish flag which stands for environment and
then here you pass the name of the variable into value that you want and that's going to be a base level
in the runtime of the container but not what happens if you define a variable with the name var one
which is in the docker file what's going to happen.
Well let's just take a look at that.
Let's remove the container that would just create it and let's create a new one.
And now we're going to create this environment verbal cue and you can parse more than one if you parse
in order dash e dash E and you can say var 1 and you're going to give it an order value let's say over.
Right.
Value you can say so now you hit enter then what's going to happen here is that you're going to create
a new container is 13 from the environment image that you've created and you are parse in two environment
variables.
The first one is test for 3 to 1.
And the second one is vert one of it.
Right.
Value set.
Now if we go inside of the container and you type Bosch and if you grep for the VAR 1 then you are going
to see that these ball you all the right.
The value that was in the docker file.
So if you have a variable in your darker file and then you parse a variable with the same name in the
darker wrong command then these value will override the volume that is on your darker file.
So please keep that in mind because that may be useful or that may cause your problems.
And if we take a look at the order bar level which is test then you're going to see that those variables
are there.
So in this video you'll learn how to use environment variables in containers how to use the environment
variables from your Docker file how to create new environment variables for a new container and how
to override existing environment variables with a different value.
Now you may be wondering why this is useful for no worries.
We're going to use it in the next lesson.