-: In the last section, we set up some configuration inside
of our Docker Compose file for Postgres, Redis
and our server.
We have one last thing to do,
we need to make sure that we provide
some environment variables off to the server service.
So inside of our Docker Compose file,
here's the server right here.
We're gonna add on another section
of configuration to specify some number
of configuration variables.
The configuration that we're going to pass in here,
is gonna customize the way in which our server behaves
when it is started up as a container.
Just in case you didn't go through all the configuration
with me a couple videos ago,
if you look in the server directory
and find the keys.js file, you'll find a bunch
of references to something called processs.env.
These are all environment variables.
And so essentially, when the server starts up
it's gonna look at its environment
and it's gonna try to pull some information
from the environment
and it's then gonna use that information
to customize the way in which it behaves.
In our case, the server relies upon these environment
variables for deciding how it's gonna connect
to its instance of Redis, and how it's gonna connect
to the Postgres server as well.
Now, we've not yet gone through the process of setting
up any environment variables through Docker Compose,
so let's look at a diagram really quickly just
to get a better idea of how to do this.
Okay, so here's how we specify environment variables
in a Docker Compose file.
And I'm showing you some very fine grained details
about the different syntax that we're gonna use.
So on the left hand side, you'll see the syntax
that you and I are going to use most frequently.
We specify the name of the variable,
then an equal right here, and then the value that we want
to set it to.
This is gonna set up a variable
inside the container at runtime of the container.
So what I mean by runtime right here, is you'll recall
that when we build an image, it's a two step process,
or I should say when we build and run an image
it's really a two step process.
The first step is when we build the image,
that's the kind of preparation part.
That's where we create a new image.
And then at some point in the future when we actually
run the container, that's the second part,
that's when we actually take the image
and create an instance of the container out of it.
When we set up an environment variable inside
of a Docker Compose file, we are setting up an environment
variable that is applied at run time.
So only when the container is started up.
So when you specify an environment variable inside
of a Docker Compose file, that information is not
being encoded inside the image.
The image doesn't get created and have some memory
of this environment variable,
it's only when a container is created, a environment
variable is set up inside of the container.
You'll see another syntax over here for setting
up an environment variable on the right hand side.
You can specify just the variable name by itself without
any equal sign or any value for it.
If you specify just the variable name, then the value for
this variable is going to be taken from your computer.
So if you have some environment variables set up
on your machine, like let's say some secret API key
or something like that,
that might be when you want to use this variable name syntax
by just itself without specifying any value,
because then you don't have to specify
or hard code in any specific value for the API key,
excuse me, for the environment variable,
it'll be taken from your machine at runtime
and it's not gonna be saved along with the source code
of your project or the Docker Compose file
or anything like that.
So in our case, we want to always use the same set
of variable names and values inside of our
Docker Compose file,
and we are not adding any environment variables directly
to our machine.
That's why we're gonna use this syntax
on the left hand side.
So let's try it out right now.
I'm gonna find my server configuration section
and then on the same tab level
as build and volumes, I'll specify environment like so.
Now really quickly, in case English is a second language
for you, or even if it's your first,
environment is a tricky word.
So I gotta ask you, please pay very close
attention to the spelling on this word right here.
It has an N right in the middle right there.
So environment, M-E-N-T.
So inside of here, we're going to add an array
of environment variables.
And remember we specify an array with a single
dash like so.
Every record in this array will get its own individual dash.
The first environment variable that we're gonna
specify, is the Redis host, which is kind of like
the URL that our server should reach out to to access Redis.
So for this, I'll specify the key Redis_host,
then the equals, and then the value on the right hand side.
Now in our case, I'm gonna...
This is kind of a gimme for the value of Redis host
right here.
Remember that anytime that we access a service
that has been defined inside of a Docker Compose file,
we just specify the name of the service.
We don't have to do any additional like, "Oh yeah,
go over to this particular IP or anything like that
that is hosting the service."
We just put the name of the service in.
So in our case, the name of the Redis host
is going to be simply Redis,
'cause that's the name of the service that was created
for hosting our Redis service.
We'll say Redis host is simply Redis like so.
The next environment variable that we need to specify,
is the Redis port.
Now this one is a little bit more challenging.
For this, you might look at the documentation
for Redis on Docker Hub.
Let's do that right now very quickly.
If you go back over to Docker Hub
and go to the explorer tab, you can scroll on down to
Redis right here.
And then in the full description,
you can scroll down a little bit
and you'll notice that to start a Redis image,
you want to connect to the default port of 6379.
Now this can be configured on the Redis image, but for us
there's no reason in configuring it,
we're just gonna leave it as 6379.
So back inside of my Docker Compose file,
I'm gonna specify another environment variable
of Redis_port equals 6379.
So in both these cases right here, we used the first syntax
of defining an environment variable.
We put the variable name in, an equal sign,
and then the value that we want to set it to.
And we used this syntax over here
because we did not want to attempt to pull the value
from my shell or my computer essentially,
'cause I do not have any environment variables to find
on my computer of Redis host or Redis port.
Okay, so that's our two first environment variables.
Now we've got a handful of others that we're gonna
go through rather quickly.
So the other ones we have to define are PG user, host,
database, password and port.
Now as a quick tip here, if you are making use
of Adam or VS code, I think this hotkey works
with other editors as well, but this is...
Those are the only two editors where I know
for sure this hotkey works.
In order to get these key names
over to the Docker Compose file very quickly,
you can highlight env. right there
and then press command D several times
and that will do a multi select on that text selection.
You can then press right
on your arrow key and then option shift right arrow
and that will select just the keyword like so.
You can then do a command C,
go back over to the Docker Compose file
and then do a command V to paste all that stuff in.
And so that's just saving us from having to do
a little bit of typing.
So now I'll add in all of my little dashes here.
And actually, technically you can use these same command D
multi slack talk key here as well if you want to.
And then we'll add in equal signs
on the right hand side of each of these.
All right, so for each of these we're gonna do
a little bit more research on the Postgres documentation
back on Docker Hub.
So I'm gonna go back over to Docker Hub,
I'll find the explore tab.
And again, I'll search for Postgres on this list.
Here it is right here.
And like I said, just a moment ago, if you scroll
down a pretty good amount, you'll see some documentation
on the default values for each of those variables.
So for Postgres password, somewhere in here
it will tell you the default password.
Actually, I'm not sure that actually tells you
what the default is.
It might be a little bit higher up here.
Oh, come on, I don't wanna look forever.
Where is it?
Okay, it doesn't actually tell us the default password
but I'll just tell you right now
the default password for Postgres is Postgres password.
We're gonna connect to a default database of Postgres.
The default user that we'll connect to is Postgres,
the host which is very similar to the Redis host that
we defined up here, is also Postgres because that's
the name of our Postgres service that we defined right here.
And then finally the port, and that's something
you can reference the documentation for back over here,
you'll notice expose 5432.
That's the default port for Postgres.
And so we'll do 5432 for that one as well.
Okay, so that's it.
So hopefully you're starting to learn here that
getting some information from these very common images
that you'll probably use, such as Postgres,
or MySQL or Redis, you have to look at the documentation
for the repository to get some information about
the default values here to make use of with that image.
So, let's save this Docker Compose file
and we're gonna very quickly test this out at our terminal.
I know this is a really long section
but we're almost done with this.
So, I'll go back over to my tunnel,
I'm gonna stop everything with Control+C,
and I'll do a Docker Compose up.
Now that's going to rebuild the server image from scratch,
The (indistinct) you recall
for the server does not take very long
so we'll just wait a couple of seconds here.
There it goes.
And then it's gonna start to create all those
different services and eventually we see listening up here.
Perfect.
So that's looking pretty good.
Let's take a quick pause right here
and we'll continue in the next section.