WEBVTT

00:01.260 --> 00:03.420
Hello and welcome to this lecture.

00:03.420 --> 00:05.410
My name is Mumshad Mannambath.

00:05.730 --> 00:11.340
In this section we will talk about Command and Arguments in a Pod Definition file.

00:11.370 --> 00:16.590
This is not listed as a required topic in the certification curriculum but I think it's important to

00:16.590 --> 00:20.720
explain as it is a topic that is usually overlooked.

00:20.730 --> 00:25.330
Let us first refresh our memory on commands in containers and docker.

00:25.380 --> 00:29.230
We will then translate this into pods in the next lecture.

00:29.400 --> 00:35.010
In this lecture we will look at commands arguments and entry points in Docker.

00:35.010 --> 00:37.280
Let's start with a simple scenario.

00:37.320 --> 00:41.220
Say you were to run a docker container from an Ubuntu image.

00:41.220 --> 00:47.670
When you run the “docker run ubuntu” command, it runs an instance of Ubuntu image and exits immediately.

00:48.300 --> 00:52.830
If you were to list the running containers you wouldn't see the container running.

00:53.010 --> 00:58.620
If you list all containers including those that are stopped you will see that the new container you

00:58.620 --> 01:01.390
ran is in an exited state.

01:01.470 --> 01:05.470
Now why is that unlike virtual machines.

01:05.470 --> 01:09.130
Containers are not meant to host an operating system.

01:09.130 --> 01:16.270
Containers are meant to run a specific task or process such as to host an instance of a web server or

01:16.270 --> 01:23.730
application server or a database or simply to carry out some kind of computation or analysis once the

01:23.730 --> 01:25.040
task is complete.

01:25.110 --> 01:32.730
The container exits a container only lives as long as the process inside it is alive.

01:32.730 --> 01:38.520
If the web service inside the container is stopped or crashes the container exits.

01:38.520 --> 01:42.600
So who defines what process is run within the container.

01:42.600 --> 01:48.660
If you look at the docker file for popular Docker images like NGINX you will see an instruction

01:48.660 --> 01:55.170
called CMD which stands for command that defines the program that will be run within the container when

01:55.170 --> 01:57.980
it starts. For the NGINX image

01:57.990 --> 02:01.680
it is the nginx command, for the mysql image

02:01.740 --> 02:04.120
it is the mysqld command.

02:04.170 --> 02:10.830
What we tried to do earlier was to run a container with a plain Ubuntu Operating System

02:10.830 --> 02:18.940
Let us look at the docker file for this image you will see that it uses bash as the default command.

02:18.940 --> 02:24.210
Now bash is not really a process like a web server or database server.

02:24.210 --> 02:31.650
It is a shell that listens for inputs from a terminal if it cannot find a terminal it exits.

02:31.650 --> 02:38.250
When we ran the Ubuntu container earlier Docker created a container from the Ubuntu image and launched

02:38.280 --> 02:45.480
the bash program by default Docker does not attach a terminal to a container when it is run.

02:45.720 --> 02:53.340
And so the bash program does not find the terminal and so it exits since the process that was started

02:53.340 --> 02:58.920
when the container was created finished the container exits as well.

02:58.920 --> 03:03.680
So how do you specify a different command to start the container.

03:03.690 --> 03:10.980
One option is to append a command to the docker run command and that way it overrides the default command

03:10.980 --> 03:13.260
specified within the image.

03:13.260 --> 03:20.530
In this case I run the docker run ubuntu command with the “sleep 5” command as the added option.

03:20.700 --> 03:27.960
This way when the container starts it runs the sleep program, waits for 5 seconds and then exits.

03:27.960 --> 03:30.840
But how do you make that change permanent.

03:30.870 --> 03:35.260
Say you want the image to always run the sleep command when it starts.

03:35.460 --> 03:42.180
You would then create your own image from the base Ubuntu image and specify a new command.

03:42.240 --> 03:49.140
There are different ways of specifying the command either the command simply as is in a shell form or

03:49.170 --> 03:52.080
Or in a JSON array format like this.

03:52.080 --> 03:59.990
But remember, when you specify in a JSON array format, the first element in the array should be the executable.

04:00.000 --> 04:08.100
In this case the sleep program do not specify the command and parameters together like this the command

04:08.190 --> 04:12.580
and its parameters should be separate elements in the list.

04:12.720 --> 04:20.240
So I now build by new image using the docker build command, and name it as ubuntu-sleeper.

04:20.250 --> 04:26.270
I could now simply run the docker Ubuntu sleeper command and get the same results.

04:26.430 --> 04:30.780
It always sleeps for five seconds and exits.

04:30.780 --> 04:35.420
But what if I wish to change the number of seconds it sleeps currently.

04:35.640 --> 04:39.830
It is hard coded to five seconds as we learned before.

04:39.840 --> 04:45.160
One option is to run the docker run command with the new command appended to it.

04:45.240 --> 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

04:52.320 --> 04:53.650
look very good.

04:53.670 --> 04:58.870
The name of the image Ubuntu sleeper in itself implies that the container will sleep.

04:58.980 --> 05:02.410
So we shouldn't have to specify the sleep command again.

05:02.430 --> 05:10.640
Instead we would like it to be something like this Docker run Ubuntu sleeper 10 we only want to pass

05:10.640 --> 05:16.460
in the number of seconds the containers should sleep and sleep command should be invoked automatically

05:17.180 --> 05:21.420
and that is where the entry point instruction comes into play.

05:21.530 --> 05:27.620
The entry point instruction is like the command instruction as in you can specify the program that will

05:27.620 --> 05:32.960
be run when the container starts and whatever you specify on the command line.

05:32.960 --> 05:39.860
In this case 10 will get appended to the entry point so the command that will be run when the container

05:39.860 --> 05:42.760
starts is sleep 10.

05:42.800 --> 05:45.250
So that's the difference between the two.

05:45.470 --> 05:52.460
In case of the CMD instruction the command line parameters passed will get replaced entirely, whereas

05:52.490 --> 05:57.770
in case of entry point the command line parameters will get appended.

05:57.770 --> 06:04.220
Now, in the second case what if I run the ubuntu-sleeper without appending the number of

06:04.220 --> 06:13.680
seconds then the command at startup will be just sleep and you get the error that the operant is missing.

06:13.760 --> 06:17.300
So how do you configure a default value for the command.

06:17.300 --> 06:24.260
If one was not specified in the command line that's where you would use both entry point as well as

06:24.260 --> 06:26.180
the command instruction.

06:26.180 --> 06:32.810
In this case the command instruction will be appended to the entry point instruction so at startup the

06:32.810 --> 06:34.760
command would be sleep 5.

06:34.910 --> 06:41.330
If you didn't specify any parameters in the command line if you did then that will override the command

06:41.330 --> 06:42.530
instruction.

06:42.530 --> 06:48.740
And remember for this to happen you should always specify the entry point and command instructions in

06:48.740 --> 06:52.030
a JSON format. Finally,

06:52.110 --> 07:00.120
What if you really really want to modify the entry point during runtime say from sleep to an imaginary

07:00.120 --> 07:02.380
sleep 2.0 command.

07:02.460 --> 07:09.600
Well in that case you can override it by using the entry point option in the docker run command.

07:09.600 --> 07:15.770
The final command at startup would then be sleep 2.0 10.

07:15.820 --> 07:19.930
Well that's it for this lecture and I will see you in the next.

