I have just demonstrated you how you are able to use variables when you need to insert, for example,
path in the Docker run command.
And notice that if you use variables then it is important in which folder you are located.
If you use specifically variable because it will actually get path to current folder and place it inside
of the Docker run command.
Great.
Now let me explain you actually what happens when you run container and how you are able to run container
in background using dash option.
Okay, let's get started with Hello world container and let's run it once again.
Docker run.
Hello world Enter.
You will see this result in terminal but container exits and I have explained you before that basically
container exits as soon as there are no processes running inside of the container.
And in this example, there was process that has printed this text to this terminal and afterwards this
process was terminated and Docker terminates container afterwards as well.
Let's try to run another container, for example, Alpine.
Let's clear our terminal and Docker run Alpine.
Same happens here.
Docker runs container.
It creates process, but this process terminates and afterwards Docker terminates container as well.
But which process actually starts in this specific container?
Let me enter Docker history Alpine.
And here at the top you will see command bin.
In this case, I'll explain you what this command actually means a bit later.
But now you should understand and remember that with this command you are able to find at the top specific
command that is executed as soon as new container is created based on specific image.
In this case, image name is Alpine.
So if you create new container with Alpine image, it runs command bin.
It is actually shell.
But this process, this process has standard in and standard output and it weights that someone or some
other process will connect to its standard in and standard output.
And in such case, this process will be kept running and will accept some characters via standard input
and may produce some output via standard output STD out.
But if we simply run container in such way, we don't connect to standard input of this process.
And that's why this process terminates automatically because there is no standard input connection or
standard output connection.
That's what happens under the hood.
But you are able to connect to standard input of this process.
If you'll add two flags, Dash I and Dash T, basically you could add only dash flag.
Let me show you that Docker run dash I Alpine.
And now I'm connected to standard in of this process bin.
And if I'll enter, for example, LZ, I will see list of folders inside of the container.
And if I'll go to another tab and enter Docker.
PS I'll see list of containers that are currently up and running.
And again I see here same command as I have shown you just in Docker history output.
It is command that was automatically executed when container was created.
And for this specific image alpine image, this command is been.
But let me now try to exit from this connection to standard input of Shell inside of the container as
usually let's try to use control C combination and you see it doesn't work.
The reason for that is that we are connected now directly to specific process and it does not accept
such termination commands.
You are also able to try to stop container here using stop command like Docker stop and here maybe is
or ID of the container or its name.
For example this one let me copy name paste here, press enter and you will see that the docker is trying
to stop container.
Let's wait a bit.
It actually sends sigterm signal to process.
But in such case, C is running with process ID one and it doesn't accept any such kind of commands.
And when Docker is not able to stop container during 10s, it will try to kill it.
It is actually hard stop of the container and in such case we stop command.
It will try to perform, let's say soft stop.
So finally in such case in 10s Docker has stopped this container.
And of course here in this step we have disconnected from standard input of the.
Great.
That's what happens when you try to connect to specific process that actually keeps container running.
But as soon as process ends, container exits as well.
Let's try something else.
Let's clear terminal here and let's try to run Nginx server.
Docker run Nginx.
And here you see that we are actually connected to standard input of nginx process.
We could verify that here.
Let's go back to this step, clear it and Docker PS and we see that Nginx container is running and here
you see basically command that was executed.
And here is actually the name of process that is running inside of the container.
This process name is Nginx.
And actually here on this step, we are able to get output from this process.
And actually in previous lectures we have seen that it produces some logs when clients connect to this
container.
Actually, let me show you that quickly.
Let me terminate this container.
Actually here I'm able to use control C and let's use port mapping.
Docker run 80 here, we'll be 80 Nginx.
Press enter.
And let's now try to connect to port 80.
80.
And connection was successful and here Nginx has served default content.
Let's go back to this output and here we see actually some text that was produced into standard output
by nginx process inside of the container.
Here is this process.
Great, but how you are able actually to run container in background.
You are able to use dash option and in such case those logs will be actually created and saved in background
into log file and you will be able to observe them later on.
But with option you will get back to terminal because container will be running in background.
Let me show you that.
Let me terminate this one clear terminal and here run same container but with option dash D without
any arguments.
And you'll simply see ID of the container that was just created.
We could verify that here in this step Docker.
PS You see here start characters of this ID and you see that now this container is still running because
there is running process nginx process, but we have not connected to that process here.
That's what actually this option does.
With this option in place, container is started in background and you will get back to your terminal
window, but container will be still kept running in background.
Let me try now to connect to our web page here, Refresh page, go back here and of course, in this
window will not see any logs because again, container is running in background, but you are able to
read those logs using Docker logs command and use some characters from the ID of specific container
that is up and running now.
And I could use, for example, characters nine two that are starting characters of this ID here.
And let's press enter and you see log that tells us that there was connection to web server and here
is response code not modified.
That's how you are able to get logs that were produced by specific process inside of the container.
And again, this container enginex container is still up and running here because there is a single
process inside of that container.
Now you are able, if you want to stop this container using Docker stop command, let me clear terminal
again Docker PS and get some characters from ID or use this name and use Docker stop command and type
here for example, nine two and now you see that this container was stopped much faster than previous
container because previously that container was not listening to any commands.
And that's why finally in 10s process was killed automatically.
In such case, this process enginex process listens to such commands and Docker was able to terminate
this container in soft, let's say, manner.
That's what option does, and that's how you're able to run such containers as, for example, Nginx
in background.
It will not interrupt your current terminal window and you will be able to perform any other actions,
create other containers and so on.
Great.
That's all for this lecture.
And in the next one let me demonstrate you how you are able to run multiple containers simultaneously
using same image.
Let's do that next.
Bye bye.