[Intro Music]
I have just demonstrated to you how
you're 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 'PWD' 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're able to run
container in background using '-d'
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
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, 'CMD /bin/
sh', 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 a new container
is created based on specific image,
in this case the image name is
'alpine'. So, if you create a new
container with 'alpine' image, it
runs command '/bin/sh'. It is actually a
shell. But this process, this process
has standard in, 'stdin' and standard
output, 'stdout'; and it waits that someone
or some other process will connect
to each standard in and standard
output. And in such case, this
process will be kept running, and
will accept the some characters
via standard input, and may produce
some output via standard output, 'stdout'.
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 out connection. That's
what happens under the hood. But
you are able to connect to standard
input of this process if you add
two flags, '-i' and '-t'.
Basically you could add only '-i' flag,
let me show you that. 'docker
run -i alpine', and now I am
connected to standard in of this
process '/bin/sh', and if I enter for
example, 'ls', I will see a list of
folders inside of the container.
And if I go to another tab and
enter 'docker ps', I will see a 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 a container was
created. And for this specific
image, alpine image, this command is
'/bin/sh'. But let me now try to exit
from this connection to standard
input of 'sh' shell inside of the
container. As usually let's try to
use Ctrl+C combination, and you see
it doesn't work. The reason for
that is that we are connected now
directly to specific 'sh' 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
either ID of the container or its
name, for example this one. Let me
copy the name, paste here, press
Enter, and you'll see that Docker is
trying to stop container. Let's
wait a bit. It actually sends SIGTERM
signal to 'process'. But in
such case, 'sh' is running with
process id 1, and it doesn't
accept any such kind of commands.
And when Docker is not able to stop
container during 10 seconds, it
will try to kill it, it has
actually hard stopped the
container, and in such case with
'stop' command, it will try to
perform, let's say, soft stop. So
finally, in such case in 10
seconds, Docker has stopped this
container. And of course here in
this step, we have disconnected
from standard input of the 'sh'.
Great. This is 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
was actually 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 the 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 Ctrl+
C, and let's use port mapping. 'docker
run -p 8080:', here will be '80
nginx'. Press Enter, and let's
now try to connect to port 80, and
connection was successful, and here
nginx has set up 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're able actually
to run container in background? You
are able to use '-d' 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 '-d' 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 '-d' without any
arguments, '-d', 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 was running process,
nginx process, but we cannot
connect it to that process here.
That's what actually this '-d'
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 I will not
see any logs, because again
container was 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
'92', that are starting
characters of this ID here, and
let's press Enter, and you see a
log that tells us that there was
connection to web server, and here
was a response code, 'Not Modified'.
That's how you're able to get logs
that were produced by a specific
process inside of the container.
And again, this container, nginx
container is still up and running
here because there was 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 '92', 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 10 seconds
process was killed automatically.
In such case this process, nginx
process listens to such commands,
and Docker was able to terminate
this container in soft, let's say
manner. That's what '-d'
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're able to
run multiple containers
simultaneously using same image.
Let's do the next. Bye-Bye.
[Outro Music]
[no audio]
[Clock Ticking]
[no audio]