1
00:00:01,260 --> 00:00:03,420
Hello and welcome to this lecture.

2
00:00:03,420 --> 00:00:05,410
My name is Mumshad Mannambath.

3
00:00:05,730 --> 00:00:11,340
In this section we will talk about Command and Arguments in a Pod Definition file.

4
00:00:11,370 --> 00:00:16,590
This is not listed as a required topic in the certification curriculum but I think it's important to

5
00:00:16,590 --> 00:00:20,720
explain as it is a topic that is usually overlooked.

6
00:00:20,730 --> 00:00:25,330
Let us first refresh our memory on commands in containers and docker.

7
00:00:25,380 --> 00:00:29,230
We will then translate this into pods in the next lecture.

8
00:00:29,400 --> 00:00:35,010
In this lecture we will look at commands arguments and entry points in Docker.

9
00:00:35,010 --> 00:00:37,280
Let's start with a simple scenario.

10
00:00:37,320 --> 00:00:41,220
Say you were to run a docker container from an Ubuntu image.

11
00:00:41,220 --> 00:00:47,670
When you run the “docker run ubuntu” command, it runs an instance of Ubuntu image and exits immediately.

12
00:00:48,300 --> 00:00:52,830
If you were to list the running containers you wouldn't see the container running.

13
00:00:53,010 --> 00:00:58,620
If you list all containers including those that are stopped you will see that the new container you

14
00:00:58,620 --> 00:01:01,390
ran is in an exited state.

15
00:01:01,470 --> 00:01:05,470
Now why is that unlike virtual machines.

16
00:01:05,470 --> 00:01:09,130
Containers are not meant to host an operating system.

17
00:01:09,130 --> 00:01:16,270
Containers are meant to run a specific task or process such as to host an instance of a web server or

18
00:01:16,270 --> 00:01:23,730
application server or a database or simply to carry out some kind of computation or analysis once the

19
00:01:23,730 --> 00:01:25,040
task is complete.

20
00:01:25,110 --> 00:01:32,730
The container exits a container only lives as long as the process inside it is alive.

21
00:01:32,730 --> 00:01:38,520
If the web service inside the container is stopped or crashes the container exits.

22
00:01:38,520 --> 00:01:42,600
So who defines what process is run within the container.

23
00:01:42,600 --> 00:01:48,660
If you look at the docker file for popular Docker images like NGINX you will see an instruction

24
00:01:48,660 --> 00:01:55,170
called CMD which stands for command that defines the program that will be run within the container when

25
00:01:55,170 --> 00:01:57,980
it starts. For the NGINX image

26
00:01:57,990 --> 00:02:01,680
it is the nginx command, for the mysql image

27
00:02:01,740 --> 00:02:04,120
it is the mysqld command.

28
00:02:04,170 --> 00:02:10,830
What we tried to do earlier was to run a container with a plain Ubuntu Operating System

29
00:02:10,830 --> 00:02:18,940
Let us look at the docker file for this image you will see that it uses bash as the default command.

30
00:02:18,940 --> 00:02:24,210
Now bash is not really a process like a web server or database server.

31
00:02:24,210 --> 00:02:31,650
It is a shell that listens for inputs from a terminal if it cannot find a terminal it exits.

32
00:02:31,650 --> 00:02:38,250
When we ran the Ubuntu container earlier Docker created a container from the Ubuntu image and launched

33
00:02:38,280 --> 00:02:45,480
the bash program by default Docker does not attach a terminal to a container when it is run.

34
00:02:45,720 --> 00:02:53,340
And so the bash program does not find the terminal and so it exits since the process that was started

35
00:02:53,340 --> 00:02:58,920
when the container was created finished the container exits as well.

36
00:02:58,920 --> 00:03:03,680
So how do you specify a different command to start the container.

37
00:03:03,690 --> 00:03:10,980
One option is to append a command to the docker run command and that way it overrides the default command

38
00:03:10,980 --> 00:03:13,260
specified within the image.

39
00:03:13,260 --> 00:03:20,530
In this case I run the docker run ubuntu command with the “sleep 5” command as the added option.

40
00:03:20,700 --> 00:03:27,960
This way when the container starts it runs the sleep program, waits for 5 seconds and then exits.

41
00:03:27,960 --> 00:03:30,840
But how do you make that change permanent.

42
00:03:30,870 --> 00:03:35,260
Say you want the image to always run the sleep command when it starts.

43
00:03:35,460 --> 00:03:42,180
You would then create your own image from the base Ubuntu image and specify a new command.

44
00:03:42,240 --> 00:03:49,140
There are different ways of specifying the command either the command simply as is in a shell form or

45
00:03:49,170 --> 00:03:52,080
Or in a JSON array format like this.

46
00:03:52,080 --> 00:03:59,990
But remember, when you specify in a JSON array format, the first element in the array should be the executable.

47
00:04:00,000 --> 00:04:08,100
In this case the sleep program do not specify the command and parameters together like this the command

48
00:04:08,190 --> 00:04:12,580
and its parameters should be separate elements in the list.

49
00:04:12,720 --> 00:04:20,240
So I now build by new image using the docker build command, and name it as ubuntu-sleeper.

50
00:04:20,250 --> 00:04:26,270
I could now simply run the docker Ubuntu sleeper command and get the same results.

51
00:04:26,430 --> 00:04:30,780
It always sleeps for five seconds and exits.

52
00:04:30,780 --> 00:04:35,420
But what if I wish to change the number of seconds it sleeps currently.

53
00:04:35,640 --> 00:04:39,830
It is hard coded to five seconds as we learned before.

54
00:04:39,840 --> 00:04:45,160
One option is to run the docker run command with the new command appended to it.

55
00:04:45,240 --> 00:04:52,320
In this case sleep 10 and so the command that will be run at startup will be sleep 10 but it doesn't

56
00:04:52,320 --> 00:04:53,650
look very good.

57
00:04:53,670 --> 00:04:58,870
The name of the image Ubuntu sleeper in itself implies that the container will sleep.

58
00:04:58,980 --> 00:05:02,410
So we shouldn't have to specify the sleep command again.

59
00:05:02,430 --> 00:05:10,640
Instead we would like it to be something like this Docker run Ubuntu sleeper 10 we only want to pass

60
00:05:10,640 --> 00:05:16,460
in the number of seconds the containers should sleep and sleep command should be invoked automatically

61
00:05:17,180 --> 00:05:21,420
and that is where the entry point instruction comes into play.

62
00:05:21,530 --> 00:05:27,620
The entry point instruction is like the command instruction as in you can specify the program that will

63
00:05:27,620 --> 00:05:32,960
be run when the container starts and whatever you specify on the command line.

64
00:05:32,960 --> 00:05:39,860
In this case 10 will get appended to the entry point so the command that will be run when the container

65
00:05:39,860 --> 00:05:42,760
starts is sleep 10.

66
00:05:42,800 --> 00:05:45,250
So that's the difference between the two.

67
00:05:45,470 --> 00:05:52,460
In case of the CMD instruction the command line parameters passed will get replaced entirely, whereas

68
00:05:52,490 --> 00:05:57,770
in case of entry point the command line parameters will get appended.

69
00:05:57,770 --> 00:06:04,220
Now, in the second case what if I run the ubuntu-sleeper without appending the number of

70
00:06:04,220 --> 00:06:13,680
seconds then the command at startup will be just sleep and you get the error that the operant is missing.

71
00:06:13,760 --> 00:06:17,300
So how do you configure a default value for the command.

72
00:06:17,300 --> 00:06:24,260
If one was not specified in the command line that's where you would use both entry point as well as

73
00:06:24,260 --> 00:06:26,180
the command instruction.

74
00:06:26,180 --> 00:06:32,810
In this case the command instruction will be appended to the entry point instruction so at startup the

75
00:06:32,810 --> 00:06:34,760
command would be sleep 5.

76
00:06:34,910 --> 00:06:41,330
If you didn't specify any parameters in the command line if you did then that will override the command

77
00:06:41,330 --> 00:06:42,530
instruction.

78
00:06:42,530 --> 00:06:48,740
And remember for this to happen you should always specify the entry point and command instructions in

79
00:06:48,740 --> 00:06:52,030
a JSON format. Finally,

80
00:06:52,110 --> 00:07:00,120
What if you really really want to modify the entry point during runtime say from sleep to an imaginary

81
00:07:00,120 --> 00:07:02,380
sleep 2.0 command.

82
00:07:02,460 --> 00:07:09,600
Well in that case you can override it by using the entry point option in the docker run command.

83
00:07:09,600 --> 00:07:15,770
The final command at startup would then be sleep 2.0 10.

84
00:07:15,820 --> 00:07:19,930
Well that's it for this lecture and I will see you in the next.
