Welcome to this demo component of the course
where we're going to be exploring
how to use environment variables with Docker.
Now, I'm going to explain how they can be used step by step,
but before we do that,
there is a setup step that I need you to run.
And in a catch 22 way, the setup step
is going to be using environment variables itself.
So attached to this video is instructions
for the environment variables component of the course
and the first command in that video is this command.
And I just want you to go ahead and run it
and I'm going to explain
all of the components of this command
as we explore environment variables together.
For now though, just go ahead and run this command.
You'll probably see that it can't find
the image phpmyadmin locally and so it's gonna go ahead
and download this Docker Image and then spin up a Container.
And then we can go ahead and clear the screen.
We don't need to do anything else
with that particular Container.
We are going to be using it very shortly
to interact with the other Container
we'll be using in this component of the course.
Now to illustrate how environment variables work
we are going to be exploring
how they can be used with a database Container,
specifically the MariaDB database Engine.
Now to do that the first thing that we're going to do
is run this command.
Again, these are all contained in the instructions
attached to this video.
Now, this command is going to download a specific version
of the MariaDB Docker Image.
So go ahead and run that command
and it will take a few moments to complete.
There's a number of file system layers,
but when that command complete
you'll have a local copy at the MariaDB Docker Image.
And I'm gonna clear the screen and run this command,
which is going to inspect this Docker Image
and allow us to explore the Metadata.
So press enter.
You'll notice this env section and this env section
contains a number of variables.
So these are environment variables.
Specifically, we have PATH, MariaDB underscore major
and MariaDB underscore version.
So when we run a Container from this image,
there's going to be a variable in the Container
in this example called PATH,
which will have this as a value.
And in this particular example, this is the variable
that's used whenever you run a command
to locate the command to execute
and it will try all of these different folders in order.
What we are going to do as we run a Container
from this image is specify
a number of other environment variables.
And these are important because they'll be used
to configure the MariaDB database Engine.
So I'll clear the screen to make it easy to see.
Now, environment variables are key value pairs.
You specify the name of the variable and a value.
Now, convention when using the Docker command
is that environment variable names
are specified in all caps.
And then you have the value.
So take this command for example.
So this is a Docker command, this is Docker run
and then double hyphen name db.
So we're spinning up a Container with the name db.
And then at the end here we can see
that we are using the MariaDB Docker Image
and this specific version.
However, in the middle, what you'll be able to see
is that we have a number of these
and these are how we can specify environment variables.
We specify hyphen E and then the environment variable name,
in this case MySQL underscore Root underscore password,
and then an equals and then the value
for that environment variable.
And these are passed into the Container when it's run.
So in this example, we're specifying
four different environment variables.
The first is MySQL underscore Root underscore password,
which specifies the Root password for this database Engine.
Another that gets specified is MySQL underscore database.
Now, this is special for this specific Container image,
because when we specify this environment variable
with this particular Container image,
then as the Container image is used to spin up a Container
an additional database will be created.
In this case Wordpress.
And this is all configured
as part of running up a Container.
We're also specifying MySQL underscore user.
And again, this is specific to this Container image
and this creates a new MariaDB user called Wordpress.
And then lastly, we have MySQL underscore password,
which sets the password for this user.
Now, obviously using Wordpress
for everything here is not secure,
but this is just a demonstration, so it's fine.
Lastly, we have this, which is double hyphen
and then Default hyphen authentication hyphen
plugin equals MySQL underscore native underscore password.
And this is just a way of specifying
an additional argument to the Docker Container,
so additional Configuration information.
Now at this point, let's just go ahead and run this command.
And this is going to run a Container
specifying all of these environment variables.
And if we go ahead and run a Docker space ps space hyphen a
and press enter, we'll be able to see
that we have this running Container.
So this Container id, it's using the MariaDB image,
this specific version of it, and it's got the name of db.
Now, the Ports that this exposes is 3306
and that's the standard MariaDB database port.
Now, I want you to go ahead and copy down this Container ID
and note it down somewhere safe
and just note it down as the MariaDB id.
And then I want you to run this command.
So Docker space inspect
and then this Container ID and press enter.
And this is going to retrieve all of the Metadata
for this particular Container.
Now, the reason we are doing this
is because I want you to note down
this particular item of information.
So this is the internal IP address
that the MariaDB Container is using.
So in my case, 172.17.0.3.
So this is the IP address which will be used
to access the MariaDB database server.
Now, if you remember at the start
of this component of the course,
we actually launched another Container,
the phpMyAdmin Container.
So this is an application that can be used
to manage database instances.
So I want you to go ahead and open a web browser.
And then in the address bar at the top
I want you to open http://localhost:8081
and then press enter.
Now this is going to take you to the phpMyAdmin screen
and this is where you can log in.
Now this is being loaded from the Container
that we just ran at the start of this part of the course.
This is just running a standard installation of phpMyAdmin.
It's nothing special, but we're going to use it
to connect to the MariaDB Container
that we've just launched together.
So in the server box, I want you to go ahead
and type in the IP address that
you just copied into your clipboard.
So the internal IP address at the MariaDB Container.
And then we need to log in
and we're going to use the username of Root
and then we're going to use the password of somewordpress.
Remember, we specified these pieces
of Configuration using environment variables
when we ran the Container.
And for MySQL underscore Root underscore password
we set that to somewordpress.
And so that's what we're going to use.
So go back to phpMyAdmin
and type somewordpress and then click on login.
And that will log you into phpMyAdmin.
And specifically, what I want you to do
is to click on user accounts
and verify that you can see the Wordpress user.
Remember, this was the user created
via one of those environment variables
that we specified when running the Container.
Additionally, on the left here,
make sure you can see Wordpress,
because this is the database that was created
via being specified as an environment variable.
Now, moving back to our terminal,
and I'll just clear the screen to make it easier to see.
I'm gonna go ahead and run this command,
which is going to allow us to go into the Container
via a bash shell.
So enter that command and press enter.
And this is going to place you
at the terminal of the Container.
And if we run a df space hyphen k and press enter.
This is going to list all of the drives and volumes
that are mounted within this Container.
And notice how there are
no externally-mapped drives or mounts.
All the Storage that this Container uses
is within the Container.
And this means the Storage is linked
to the life cycle of the Container.
If the Container is deleted, then so is the Storage.
Now, why this is important, if we use this command
and go into this folder and then do an ls space hyphen la
these are the MariaDB database files.
These are the files which actually store the data
of the MariaDB database Engine
and all of this is stored within the Container.
It means that if we delete the Container
then all of the data within this Container is also deleted.
And that's a limitation that we are going to fix
in the next demo component of the course
where we're going to look at configuring separate Storage
for our Container using named volumes and bind mounts.
For now, you need to understand the limitation
in that anything inside this Container is Ephemeral.
If we delete the Container, the Storage within it
won't persist past the lifetime of that Container.
And that presents some unique challenges
when you're attempting to containerize applications
which have database servers.
At this point though, we can go ahead
and exit this Container and clear the screen
and now we'll just clear up
all of the infrastructure we've used
in this demo component of the course.
To do that, we're going to use a Docker space stop space db
and this will stop the database Container
and then a Docker space rm space db
to delete the database Container.
And we can also go ahead and run
a Docker space stop space phpmyadmin
and that will stop the phpMyAdmin Container.
And then likewise, we need to go ahead
and remove that Container, so Docker space rm space
and then phpmyadmin and press enter.
And that means your Docker Host is in the same state
as it was at the start of this demo component of the course.
Now remember, the limitation that we've highlighted
is that any Storage for the Container
is currently stored within the Container
and it's linked to the lifecycle of that Container.
And that's something that we are going to resolve
in the next demo component of the course.
At this point though, go ahead
and when you're ready you can move on.