1
00:00:02,130 --> 00:00:05,070
With that, let's leave the world of volumes

2
00:00:05,070 --> 00:00:08,680
and images and copying stuff around and folders and files,

3
00:00:08,680 --> 00:00:11,950
and instead, let's dive into the second,

4
00:00:11,950 --> 00:00:14,710
arguably a bit shorter and easier concept,

5
00:00:14,710 --> 00:00:16,670
I wanna discuss in this module.

6
00:00:16,670 --> 00:00:20,430
And that would be arguments and environment variables,

7
00:00:20,430 --> 00:00:23,240
and that's no typo in the title here.

8
00:00:23,240 --> 00:00:25,810
Instead, I'm talking about two options

9
00:00:25,810 --> 00:00:27,810
you can set in your Dockerfile

10
00:00:27,810 --> 00:00:31,880
and on your docker build and docker run commands

11
00:00:31,880 --> 00:00:35,290
because Docker supports build-time arguments

12
00:00:35,290 --> 00:00:38,390
and runtime environment variables.

13
00:00:38,390 --> 00:00:40,020
Now what do I mean by that?

14
00:00:40,020 --> 00:00:44,590
Arguments allow you to set flexible bits of data,

15
00:00:44,590 --> 00:00:46,110
variables you could say,

16
00:00:46,110 --> 00:00:49,980
in your Dockerfile which you can use in there

17
00:00:49,980 --> 00:00:52,500
to pluck different values

18
00:00:52,500 --> 00:00:54,830
into certain Dockerfile instructions

19
00:00:54,830 --> 00:00:57,650
based on arguments that are provided

20
00:00:57,650 --> 00:01:02,650
with the --build-arg option when you run docker build.

21
00:01:02,980 --> 00:01:06,520
And we'll see this in action in a couple of seconds.

22
00:01:06,520 --> 00:01:08,940
Environment variables on the other hand

23
00:01:08,940 --> 00:01:12,360
are available inside of a Dockerfile like arg,

24
00:01:12,360 --> 00:01:15,730
but args are available in your entire application code

25
00:01:15,730 --> 00:01:17,680
in your running application

26
00:01:17,680 --> 00:01:21,160
and you can set them with the env option

27
00:01:21,160 --> 00:01:23,030
inside of a Dockerfile,

28
00:01:23,030 --> 00:01:25,490
telling Docker that you're expecting

29
00:01:25,490 --> 00:01:28,150
this environment variable to be existent

30
00:01:28,150 --> 00:01:30,590
and then provide concrete values

31
00:01:30,590 --> 00:01:34,200
with the --env option on docker run.

32
00:01:34,200 --> 00:01:37,200
And args and environment variables

33
00:01:37,200 --> 00:01:40,880
allow you to create more flexible images and containers

34
00:01:40,880 --> 00:01:44,170
because you don't have to hard-code everything

35
00:01:44,170 --> 00:01:46,380
into these containers and images.

36
00:01:46,380 --> 00:01:49,900
Instead, you can set it dynamically when you build an image

37
00:01:49,900 --> 00:01:52,570
or even only when you run a container.

38
00:01:52,570 --> 00:01:54,590
Now that might still be a bit abstract,

39
00:01:54,590 --> 00:01:56,053
so let's see it in action.

40
00:01:57,010 --> 00:01:59,520
Now one example I wanna start with here

41
00:01:59,520 --> 00:02:03,390
would be in server JS, the port on which we're listening.

42
00:02:03,390 --> 00:02:04,770
This is port 80,

43
00:02:04,770 --> 00:02:08,169
and that's the default port for HTTP requests,

44
00:02:08,169 --> 00:02:11,360
but during development or for whatever reason,

45
00:02:11,360 --> 00:02:12,950
we might wanna change this,

46
00:02:12,950 --> 00:02:14,150
or we might even have

47
00:02:14,150 --> 00:02:17,660
bigger blocks of code in our application code,

48
00:02:17,660 --> 00:02:20,130
which should run differently

49
00:02:20,130 --> 00:02:24,570
based on input provided from outside,

50
00:02:24,570 --> 00:02:26,630
which still might sound very cryptic.

51
00:02:26,630 --> 00:02:29,710
But let's simply start and it'll make more sense.

52
00:02:29,710 --> 00:02:32,980
Node embraces the concept of environment variables

53
00:02:32,980 --> 00:02:35,950
and you access them in your node code

54
00:02:35,950 --> 00:02:39,730
on a globally available process object.

55
00:02:39,730 --> 00:02:42,740
On that object, you have an env key,

56
00:02:42,740 --> 00:02:45,970
and in there, you can access environment variables

57
00:02:45,970 --> 00:02:48,370
that have been set for the environment

58
00:02:48,370 --> 00:02:51,230
in which this code executes.

59
00:02:51,230 --> 00:02:53,770
And now that's the part you can do with Docker.

60
00:02:53,770 --> 00:02:57,420
You can set such globally available environment variables.

61
00:02:57,420 --> 00:03:00,810
And most programming languages and tools

62
00:03:00,810 --> 00:03:04,770
have some kind of support for such environment variables.

63
00:03:04,770 --> 00:03:06,720
So in this case, for example,

64
00:03:06,720 --> 00:03:10,640
we could expect to get a PORT environment variable,

65
00:03:10,640 --> 00:03:13,310
and typically they're written all uppercase,

66
00:03:13,310 --> 00:03:16,180
which then contains the actual PORT number

67
00:03:16,180 --> 00:03:18,690
that should be used to listen on.

68
00:03:18,690 --> 00:03:20,140
Now if I saved this,

69
00:03:20,140 --> 00:03:22,040
at the moment it wouldn't work though,

70
00:03:22,040 --> 00:03:26,350
because at the moment, this environment variable is not set.

71
00:03:26,350 --> 00:03:29,490
We can set it or announce it

72
00:03:29,490 --> 00:03:31,970
inside of the Dockerfile though.

73
00:03:31,970 --> 00:03:36,130
There we could specify it, maybe here,

74
00:03:36,130 --> 00:03:40,180
by adding the env instruction.

75
00:03:40,180 --> 00:03:41,470
And after env,

76
00:03:41,470 --> 00:03:43,750
you provide the name of the environment variable

77
00:03:43,750 --> 00:03:44,810
which you wanna announce.

78
00:03:44,810 --> 00:03:46,780
In this case, PORT.

79
00:03:46,780 --> 00:03:49,150
As a second argument,

80
00:03:49,150 --> 00:03:52,670
you will set a default value for this environment variable,

81
00:03:52,670 --> 00:03:53,503
for example, 80.

82
00:03:54,760 --> 00:03:58,530
And with that, I ensure that this PORT environment variable

83
00:03:58,530 --> 00:04:02,040
is available in this entire application environment

84
00:04:02,040 --> 00:04:04,693
and that it has a default value of 80.

85
00:04:05,770 --> 00:04:06,603
We can now actually

86
00:04:06,603 --> 00:04:10,870
also use this environment variable here on expose.

87
00:04:10,870 --> 00:04:14,690
Do not hard-code 80 here, but expose PORT.

88
00:04:14,690 --> 00:04:15,810
This is now possible

89
00:04:15,810 --> 00:04:18,870
because it is a registered environment variable here.

90
00:04:18,870 --> 00:04:21,959
However, the syntax is actually not like this.

91
00:04:21,959 --> 00:04:24,640
Instead, you need to add a dollar sign in front of it,

92
00:04:24,640 --> 00:04:27,560
telling Docker that the thing after the dollar sign

93
00:04:27,560 --> 00:04:29,620
is the name of an environment variable.

94
00:04:29,620 --> 00:04:30,453
And with that,

95
00:04:30,453 --> 00:04:32,800
Docker will look for this PORT environment variable

96
00:04:32,800 --> 00:04:35,663
and pluck the value stored in there, in here.

97
00:04:36,870 --> 00:04:38,290
Now with that,

98
00:04:38,290 --> 00:04:42,210
we can first of all rebuild our image.

99
00:04:42,210 --> 00:04:44,737
And I'm going to give it a tag of env.

100
00:04:46,230 --> 00:04:48,330
So build that image.

101
00:04:48,330 --> 00:04:49,870
And thereafter, of course,

102
00:04:49,870 --> 00:04:53,720
also run our container again with the docker run command

103
00:04:53,720 --> 00:04:55,130
to be used all the time,

104
00:04:55,130 --> 00:04:58,933
this time with the env tag here after the image though.

105
00:05:00,520 --> 00:05:03,820
Now I'm still publishing the internal PORT 80 here

106
00:05:03,820 --> 00:05:06,600
because I know that this is the default for this container.

107
00:05:06,600 --> 00:05:08,530
So this should still work.

108
00:05:08,530 --> 00:05:09,723
If I now hit enter,

109
00:05:10,990 --> 00:05:13,870
I'm getting an error that this container is still running,

110
00:05:13,870 --> 00:05:16,130
so let me quickly stop that first.

111
00:05:16,130 --> 00:05:18,670
Then repeat this and hit enter

112
00:05:18,670 --> 00:05:20,363
and everything works as before.

113
00:05:21,280 --> 00:05:23,660
Now the advantage of using environment variables

114
00:05:23,660 --> 00:05:26,080
is that we now can configure this.

115
00:05:26,080 --> 00:05:28,200
When we run this container though,

116
00:05:28,200 --> 00:05:31,130
we're not limited to this hard-coded value.

117
00:05:31,130 --> 00:05:33,270
We just have a default value here,

118
00:05:33,270 --> 00:05:35,390
but we don't have to stick to that.

119
00:05:35,390 --> 00:05:38,620
So again, I can stop this container

120
00:05:38,620 --> 00:05:43,620
and then run this again, but now with an extra option

121
00:05:43,640 --> 00:05:44,900
which I set here.

122
00:05:44,900 --> 00:05:46,500
Let's say here.

123
00:05:46,500 --> 00:05:49,460
You can add the --env option,

124
00:05:49,460 --> 00:05:50,293
and after that,

125
00:05:50,293 --> 00:05:54,110
you can add an environment variable key value pair.

126
00:05:54,110 --> 00:05:56,710
And we can set PORT

127
00:05:56,710 --> 00:06:00,463
equal to 8,000, let's say, like this.

128
00:06:01,760 --> 00:06:04,360
And you can add multiple environment variables here

129
00:06:04,360 --> 00:06:07,203
if you want to, but I'm just setting this.

130
00:06:08,220 --> 00:06:09,490
And of course, this also means

131
00:06:09,490 --> 00:06:12,800
that we now need to expose the internal PORT 8,000

132
00:06:12,800 --> 00:06:15,510
because since we set the environment variable to that,

133
00:06:15,510 --> 00:06:20,510
this 8,000 PORT will be used here and also in the server JS.

134
00:06:21,730 --> 00:06:24,310
But now we don't need to rebuild the image

135
00:06:24,310 --> 00:06:26,070
just to change the PORT.

136
00:06:26,070 --> 00:06:27,840
Thanks to the environment variable,

137
00:06:27,840 --> 00:06:30,770
we can just do it like this in the run command.

138
00:06:30,770 --> 00:06:34,330
And if I start that and reload, it still works,

139
00:06:34,330 --> 00:06:37,863
which means that setting this new PORT was successful.

140
00:06:38,820 --> 00:06:40,200
Now you've got a couple of options

141
00:06:40,200 --> 00:06:42,850
when it comes to setting them on the docker run command.

142
00:06:42,850 --> 00:06:44,620
I showed you --env.

143
00:06:44,620 --> 00:06:47,730
You can also shorten this to just dash E if you want to.

144
00:06:47,730 --> 00:06:49,470
That's also supported.

145
00:06:49,470 --> 00:06:51,780
And if you have multiple environment variables,

146
00:06:51,780 --> 00:06:55,740
you'll simply add multiple dash Es with the key value pairs,

147
00:06:55,740 --> 00:06:57,960
just as we did it for volumes, basically.

148
00:06:57,960 --> 00:07:00,793
There we also had multiple dash V flex.

149
00:07:01,710 --> 00:07:03,840
You can also specify,

150
00:07:03,840 --> 00:07:08,350
if I stop this container, you can also specify a file

151
00:07:08,350 --> 00:07:10,280
that contains your environment variable

152
00:07:10,280 --> 00:07:11,720
if you prefer this.

153
00:07:11,720 --> 00:07:14,670
Often such a file is named .env,

154
00:07:14,670 --> 00:07:16,280
but you don't have to name it like this,

155
00:07:16,280 --> 00:07:17,530
but here I will do that.

156
00:07:17,530 --> 00:07:19,720
And then in this file, you could set up

157
00:07:19,720 --> 00:07:22,320
your environment variable key value pair,

158
00:07:22,320 --> 00:07:24,563
so PORT set to 8,000.

159
00:07:25,450 --> 00:07:28,020
And then you could also run this container

160
00:07:28,020 --> 00:07:31,330
and instead of adding the environment variable here,

161
00:07:31,330 --> 00:07:36,227
you could also just use the --env-file option now,

162
00:07:38,060 --> 00:07:41,030
instead of just --env or -e

163
00:07:41,030 --> 00:07:42,860
and point at that file

164
00:07:42,860 --> 00:07:44,670
that contains your environment variables.

165
00:07:44,670 --> 00:07:46,840
In this case, that's the .env file.

166
00:07:46,840 --> 00:07:49,540
It's in the same folder as I'm currently navigated in

167
00:07:49,540 --> 00:07:50,800
in my terminal here.

168
00:07:50,800 --> 00:07:53,450
And then therefore you should start with dot slash,

169
00:07:53,450 --> 00:07:56,120
which means the .env file in the folder

170
00:07:56,120 --> 00:07:59,330
in which I'm currently in here in my terminal.

171
00:07:59,330 --> 00:08:02,963
And with that, the values will be read in from that file.

172
00:08:04,300 --> 00:08:06,990
And hence this still works if we reload here

173
00:08:06,990 --> 00:08:10,710
because now PORT is still set to 8,000.

174
00:08:10,710 --> 00:08:13,460
The advantage of using such a file could be

175
00:08:13,460 --> 00:08:16,160
that you can always rerun the same command

176
00:08:16,160 --> 00:08:18,930
and just switch your values here in the file,

177
00:08:18,930 --> 00:08:22,920
instead of having to do that in the docker run command.

178
00:08:22,920 --> 00:08:25,050
That's why you might wanna use such a file,

179
00:08:25,050 --> 00:08:27,090
but ultimately it's of course up to you

180
00:08:27,090 --> 00:08:29,280
which approach you prefer.

181
00:08:29,280 --> 00:08:30,220
But it should be clear

182
00:08:30,220 --> 00:08:32,799
why environment variables can be helpful.

183
00:08:32,799 --> 00:08:35,690
They help us run one and the same container

184
00:08:35,690 --> 00:08:39,539
based on one and the same image in different modes,

185
00:08:39,539 --> 00:08:42,000
in different configurations, you could say.

186
00:08:42,000 --> 00:08:45,883
So let me now stop feedback app again, and let's continue.

