Hello and welcome to this lecture.
My name is Mumshad Mannambath.
In this section we will talk about Command and Arguments in a Pod Definition file.
This is not listed as a required topic in the certification curriculum but I think it's important to
explain as it is a topic that is usually overlooked.
Let us first refresh our memory on commands in containers and docker.
We will then translate this into pods in the next lecture.
In this lecture we will look at commands arguments and entry points in Docker.
Let's start with a simple scenario.
Say you were to run a docker container from an Ubuntu image.
When you run the “docker run ubuntu” command, it runs an instance of Ubuntu image and exits immediately.
If you were to list the running containers you wouldn't see the container running.
If you list all containers including those that are stopped you will see that the new container you
ran is in an exited state.
Now why is that unlike virtual machines.
Containers are not meant to host an operating system.
Containers are meant to run a specific task or process such as to host an instance of a web server or
application server or a database or simply to carry out some kind of computation or analysis once the
task is complete.
The container exits a container only lives as long as the process inside it is alive.
If the web service inside the container is stopped or crashes the container exits.
So who defines what process is run within the container.
If you look at the docker file for popular Docker images like NGINX you will see an instruction
called CMD which stands for command that defines the program that will be run within the container when
it starts. For the NGINX image
it is the nginx command, for the mysql image
it is the mysqld command.
What we tried to do earlier was to run a container with a plain Ubuntu Operating System
Let us look at the docker file for this image you will see that it uses bash as the default command.
Now bash is not really a process like a web server or database server.
It is a shell that listens for inputs from a terminal if it cannot find a terminal it exits.
When we ran the Ubuntu container earlier Docker created a container from the Ubuntu image and launched
the bash program by default Docker does not attach a terminal to a container when it is run.
And so the bash program does not find the terminal and so it exits since the process that was started
when the container was created finished the container exits as well.
So how do you specify a different command to start the container.
One option is to append a command to the docker run command and that way it overrides the default command
specified within the image.
In this case I run the docker run ubuntu command with the “sleep 5” command as the added option.
This way when the container starts it runs the sleep program, waits for 5 seconds and then exits.
But how do you make that change permanent.
Say you want the image to always run the sleep command when it starts.
You would then create your own image from the base Ubuntu image and specify a new command.
There are different ways of specifying the command either the command simply as is in a shell form or
Or in a JSON array format like this.
But remember, when you specify in a JSON array format, the first element in the array should be the executable.
In this case the sleep program do not specify the command and parameters together like this the command
and its parameters should be separate elements in the list.
So I now build by new image using the docker build command, and name it as ubuntu-sleeper.
I could now simply run the docker Ubuntu sleeper command and get the same results.
It always sleeps for five seconds and exits.
But what if I wish to change the number of seconds it sleeps currently.
It is hard coded to five seconds as we learned before.
One option is to run the docker run command with the new command appended to it.
In this case sleep 10 and so the command that will be run at startup will be sleep 10 but it doesn't
look very good.
The name of the image Ubuntu sleeper in itself implies that the container will sleep.
So we shouldn't have to specify the sleep command again.
Instead we would like it to be something like this Docker run Ubuntu sleeper 10 we only want to pass
in the number of seconds the containers should sleep and sleep command should be invoked automatically
and that is where the entry point instruction comes into play.
The entry point instruction is like the command instruction as in you can specify the program that will
be run when the container starts and whatever you specify on the command line.
In this case 10 will get appended to the entry point so the command that will be run when the container
starts is sleep 10.
So that's the difference between the two.
In case of the CMD instruction the command line parameters passed will get replaced entirely, whereas
in case of entry point the command line parameters will get appended.
Now, in the second case what if I run the ubuntu-sleeper without appending the number of
seconds then the command at startup will be just sleep and you get the error that the operant is missing.
So how do you configure a default value for the command.
If one was not specified in the command line that's where you would use both entry point as well as
the command instruction.
In this case the command instruction will be appended to the entry point instruction so at startup the
command would be sleep 5.
If you didn't specify any parameters in the command line if you did then that will override the command
instruction.
And remember for this to happen you should always specify the entry point and command instructions in
a JSON format. Finally,
What if you really really want to modify the entry point during runtime say from sleep to an imaginary
sleep 2.0 command.
Well in that case you can override it by using the entry point option in the docker run command.
The final command at startup would then be sleep 2.0 10.
Well that's it for this lecture and I will see you in the next.