1
00:00:02,240 --> 00:00:04,170
Now that were environment variables,

2
00:00:04,170 --> 00:00:05,790
which we set up at runtime

3
00:00:05,790 --> 00:00:07,810
with the Docker run command,

4
00:00:07,810 --> 00:00:10,410
and they can be very useful as you saw.

5
00:00:10,410 --> 00:00:12,560
Now what are build-time arguments?

6
00:00:12,560 --> 00:00:16,170
With those, we can actually plug in different values

7
00:00:16,170 --> 00:00:17,680
into our Dockerfile,

8
00:00:17,680 --> 00:00:20,740
or into our image when we build that image

9
00:00:20,740 --> 00:00:23,680
without having to hard code these values

10
00:00:23,680 --> 00:00:24,680
into the Dockerfile.

11
00:00:26,520 --> 00:00:29,810
And again, one example in this example here

12
00:00:29,810 --> 00:00:32,210
could be our port number.

13
00:00:32,210 --> 00:00:34,870
Yes, we're now using an environment variable

14
00:00:34,870 --> 00:00:37,230
and therefore this is quite flexible.

15
00:00:37,230 --> 00:00:41,730
But we actually still have one, not really a problem.

16
00:00:41,730 --> 00:00:44,670
But one area of improvement, I would say.

17
00:00:44,670 --> 00:00:48,840
Our default value is still hard coded to 80.

18
00:00:48,840 --> 00:00:50,060
That might make sense

19
00:00:50,060 --> 00:00:52,130
and might not be a problem at all.

20
00:00:52,130 --> 00:00:55,210
But maybe we want to make this default value

21
00:00:55,210 --> 00:00:56,650
flexible as well.

22
00:00:56,650 --> 00:00:58,570
So that when we build the image,

23
00:00:58,570 --> 00:01:01,300
we can actually build the image based on

24
00:01:01,300 --> 00:01:05,519
one and the same unchanged the Dockerfile multiple times

25
00:01:05,519 --> 00:01:08,590
with different default values.

26
00:01:08,590 --> 00:01:10,250
And that's something we can achieve

27
00:01:10,250 --> 00:01:12,740
with build-time arguments.

28
00:01:12,740 --> 00:01:14,710
For that, we could add an argument.

29
00:01:14,710 --> 00:01:17,410
And again, you can add this anywhere, basically,

30
00:01:17,410 --> 00:01:21,700
I will now add it here and simply give that argument a name.

31
00:01:21,700 --> 00:01:24,777
And I will name it, "default_port."

32
00:01:27,050 --> 00:01:30,610
You can also assign a default value here again,

33
00:01:30,610 --> 00:01:33,110
this time with the equal sign, for example,

34
00:01:33,110 --> 00:01:36,350
like this to again, set it to 80.

35
00:01:36,350 --> 00:01:40,770
Now, this argument cannot be used in your code here,

36
00:01:40,770 --> 00:01:43,360
it's not available there, you can only use it

37
00:01:43,360 --> 00:01:44,760
in your Dockerfile.

38
00:01:44,760 --> 00:01:48,250
And even there, you can't use it on every instruction.

39
00:01:48,250 --> 00:01:50,520
You cannot use it on command, for example,

40
00:01:50,520 --> 00:01:53,430
because that's a runtime command in the end,

41
00:01:53,430 --> 00:01:56,400
which is executed when the container starts.

42
00:01:56,400 --> 00:02:00,260
But you can use your arg on all the other instructions.

43
00:02:00,260 --> 00:02:03,470
And therefore we can also use it on the env instruction,

44
00:02:03,470 --> 00:02:06,130
for example, to set the default value

45
00:02:06,130 --> 00:02:07,890
for this environment variable

46
00:02:07,890 --> 00:02:10,902
to default_port like this.

47
00:02:13,050 --> 00:02:17,060
Again, with a $ sign in front of it to tell Docker

48
00:02:17,060 --> 00:02:19,270
that we're referring to some argument

49
00:02:19,270 --> 00:02:22,400
or environment variable with this word here.

50
00:02:22,400 --> 00:02:24,930
So now with that, it's kind of inception.

51
00:02:24,930 --> 00:02:25,870
I'm aware of that.

52
00:02:25,870 --> 00:02:27,060
But now with that,

53
00:02:27,060 --> 00:02:30,460
we can set a dynamic argument which then is set

54
00:02:30,460 --> 00:02:34,370
as a default value for the dynamic environment variable.

55
00:02:34,370 --> 00:02:39,180
Yes, but now we can build our image here

56
00:02:40,130 --> 00:02:44,160
with a name of feedback-node and then a tag

57
00:02:44,160 --> 00:02:48,063
of let's say, web-app.

58
00:02:49,150 --> 00:02:51,470
And they offer we wanna use port 80.

59
00:02:51,470 --> 00:02:53,780
And hence, we don't have to specify anything,

60
00:02:53,780 --> 00:02:57,120
because that's actually the default for our argument here.

61
00:02:57,120 --> 00:02:59,720
So we can just build it like this.

62
00:02:59,720 --> 00:03:03,830
But there after, we can build this image again, now,

63
00:03:03,830 --> 00:03:07,090
let's say for the dev tag for development,

64
00:03:07,090 --> 00:03:10,630
and now here, we can set a concrete value for default port

65
00:03:10,630 --> 00:03:13,290
by adding --build-arg,

66
00:03:13,290 --> 00:03:17,270
and then setting default port equal to 8000.

67
00:03:17,270 --> 00:03:19,300
Like this, for example.

68
00:03:19,300 --> 00:03:22,330
And now we're again building an image

69
00:03:22,330 --> 00:03:25,790
on a different tag with a different arg value.

70
00:03:25,790 --> 00:03:28,830
There after this value is locked in.

71
00:03:28,830 --> 00:03:32,730
But now we got two images based on the same Dockerfile,

72
00:03:32,730 --> 00:03:35,110
where we didn't need to change any code.

73
00:03:35,110 --> 00:03:37,970
And yet, we're using a different port number,

74
00:03:37,970 --> 00:03:39,500
which is then used as a default

75
00:03:39,500 --> 00:03:41,680
for the environment variable.

76
00:03:41,680 --> 00:03:43,530
And that's just one example.

77
00:03:43,530 --> 00:03:45,680
But it can be useful to be able

78
00:03:45,680 --> 00:03:49,260
to lock certain values in when you build an image

79
00:03:49,260 --> 00:03:51,870
and to be able to build different images

80
00:03:51,870 --> 00:03:54,000
in a flexible way without having

81
00:03:54,000 --> 00:03:56,490
to change the Dockerfile all the time.

82
00:03:56,490 --> 00:03:58,973
So for now, hit Enter, this is being built.

83
00:04:00,620 --> 00:04:03,900
Enter after we have this image available as well.

84
00:04:03,900 --> 00:04:07,420
Now one note about where you specify args and envs.

85
00:04:07,420 --> 00:04:08,870
As you might have noticed,

86
00:04:08,870 --> 00:04:11,880
it always rerun npm install for me here.

87
00:04:11,880 --> 00:04:14,690
You might of course wanna place arg and env

88
00:04:14,690 --> 00:04:17,570
in a place where you then need it thereafter,

89
00:04:17,570 --> 00:04:19,560
and not place it right at the beginning

90
00:04:19,560 --> 00:04:22,670
of your Dockerfile because these instructions

91
00:04:22,670 --> 00:04:24,620
just like all other instructions,

92
00:04:24,620 --> 00:04:27,040
add layers to your Dockerfile.

93
00:04:27,040 --> 00:04:29,710
And hence, when something changes about them,

94
00:04:29,710 --> 00:04:33,620
all subsequent layers are rebuilt, are re-executed.

95
00:04:33,620 --> 00:04:37,200
So all subsequent instructions are re-executed.

96
00:04:37,200 --> 00:04:40,730
And I don't wanna execute npm install again all the time.

97
00:04:40,730 --> 00:04:45,170
Just because I passed in a different default port argument.

98
00:04:45,170 --> 00:04:47,180
It's not relevant to this command.

99
00:04:47,180 --> 00:04:50,530
So this command shouldn't rerun just because I did that.

100
00:04:50,530 --> 00:04:54,760
That's just an extra note on where to place these things.

101
00:04:54,760 --> 00:04:58,840
But with that, that's it for arguments and environments.

102
00:04:58,840 --> 00:05:00,440
That's how we can use them.

103
00:05:00,440 --> 00:05:04,040
And why they might be useful in certain applications.

104
00:05:04,040 --> 00:05:05,630
We're going to see more of that,

105
00:05:05,630 --> 00:05:07,333
throughout the course, of course.

