[Intro Music]
In the previous lecture, I
explained to you what happened when we
have created a new hello-world
container from hello-world image.
And basically executable file
called 'hello' was executed; and
responsibility of this file is
simply to bring text to the
terminal. That's it. And afterwards
container was stopped. Now, let me
explain you what container actually
is. On this diagram, you see image
layers here. And you already know
that every image is read-only. All
'fs' layers in the image are read-
only. You're not able to modify
them. When you create a new
container from a specific image,
Docker creates a very small
writeable layer, and you are able to
read from that layer, and write to
that layer, create new files and
folders. And every container has
its own writeable layer. In this
example, there are two different
containers, Container A and
Container B created from the same
image. Let me quickly do that
actually. Let me go to terminal, and
create one more hello-world
container. 'docker run hello-world',
and I see same result. And if I'll
enter 'docker ps -a' command now,
I will see that there are now two
different stopped containers, and
those two different containers used
same hello-world image, here and
here. But the command that was
executed inside of every of those
containers was the same. It was
launch of the 'hello' file. Alright,
let's proceed. After creation of the
container, Docker actually launches
specific process, specific
process inside of the container.
And as long as at least one process
is running inside of the container,
container is considered as running.
But as soon as process ends,
container is stopped. Let me
explain you all of this using
another diagram. On this diagram
there is Docker Host. It is a
Linux computer. There is Docker
Engine installed inside. Also every
Linux machine has a Linux Kernel,
and such resources as CPU, RAM,
network and hard drive. And let's
suppose that we have already pulled
some image from Docker Hub. Here
was this image located on the hard
drive of this Docker Host. And this
image, let's assume has two
different 'fs' layers, 'image fs 1',
and 'image fs 2'. And again, those
image layers are located on hard
drive of this Docker Host. When you
enter 'docker run', some image command,
Docker will create additional
writeable layer for new container.
That is the first action. And this
writeable layer will be created here
on the hard drive of this Docker
Host. Next you know that every
image has instructions, which are
executed when new container is
created. And in case with
hello-world image this command was
run 'hello' executable file. And
that means that new process on this
Docker Host is created for a
particular new container, like this.
New process is created. This
process does some job. And
afterwards if this process ends,
Docker removes this container from
memory. And in such case, when
there are no processes running
inside of the container, container
will be stopped by Docker. But
those files, writeable 'fs' layer
for this container remains on the
hard drive. But if you remove a
particular stopped container, then
Docker of course removes this
writeable 'fs' layer as well. It
means that you are able to create
multiple containers like so. Create
Container 1 with additional
writeable 'fs' layer, but notice
that we don't need to create those
layers because those layers are
already present on hard drive. And
this container will actually reuse
those 'fs' layers. Any processes
running inside of this container
will reuse those 'fs' layers.
Alright, writeable 'fs' layer was
created, process was launched. And
now let's suppose that this process
is a long running process, like
database process; or for example,
we execute some scripts, perform
some long running actions, and so
on; and in such case when process
is running, container is kept
running, and there is process in
memory of this Docker Host. And of
course, Linux Kernel manages all
requests to resources like CPU, RAM,
network from this process. All
requests come via Linux Kernel.
Afterwards, when this container is
running, let's suppose that you
want to create a new container
using same image. In such case
again, Docker creates new writeable
'fs' layer for new container.
Those layers remain here. This
Container 2 will simply reuse
those two 'fs' layers. And next it
creates new process for new
container. And again, this process
communicates with resources like
CPU, RAM, network and hard drive
via common Linux Kernel. Container 1
and Container 2 communicate via
common Linux Kernel. But
processes are completely
independent. They are different,
but running on the same Docker
Host. Alright, this is what happens
when you create new containers
based on a specific image. And key
point here is that 'fs' layers of
basic image that was used for
creation of those containers are
reused, they are not copied to any
new container. Every new container
gets just new writeable 'fs' layer,
that's it. And when process is
stopped in specific container,
Docker stops container as well and
removes it from memory. But files
of this container still remain on
the hard drive until you enter
'docker rm' command, and remove
specific stopped container. As you
see from this diagram, Docker
utilizes the resources of Docker
Host very efficiently. First it
reuses files on the hard drive.
There is no need to copy a file
system layers from specific image
when you create new containers. For
every new container, Docker creates
just new writeable 'fs' layer,
all remaining underlying 'fs'
layers are reused from this image.
And next Docker does not keep a
container running if there are no
processes running inside of the
container. It immediately stops
container when there are no
processes running. Alright, let's
now go to terminal and let's remove
containers that we have created
before. Basically picture in our
case is like this, there are no
containers running, but there are
still two containers on hard drive,
but those containers are stopped.
Let's go to terminal. And here
again, list containers that were
stopped. There are no containers
running. And now let's use 'docker
rm' command in order to remove
stopped container. And let's first
remove this container. Let's take
its ID, and let's remove it like
so. And let's remove second
container, but let me use alternative
version of this command, 'docker
container rm'; and you're also able
to use a randomly generated name in
order to perform some actions with
container. Let me copy this name
and paste here as argument for 'rm'
command; and second container was
removed as well. And now there are
no containers that are currently
running or stopped. Great. But I
still have image, 'docker images',
on the list, hello-world image and
busybox image. It means that now
picture is following. There are no
'fs' layers for containers, but there
is still image on hard drive with
corresponding 'fs' layers. Basically,
there are two different images at
the moment. And you're able of
course, remove images as well using
'docker rmi' command. And here you
could type either IMAGE ID or NAME
of the image in order to remove it.
Let's remove busybox image. Deleted.
And let's remove hello-world image.
hello-world. hello-world image was
removed as well. And now if I'll
have a look at the list of images,
I will see empty list. And that
means that at the moment, there
are no images on hard drive, on
local Docker Host. That's how
picture looks like at the moment.
Alright guys, I hope you enjoyed
this lecture. And basically that's
all for this section, where I
have made small introduction into
images and containers. And next let
me briefly summarize what we have
covered in this section. I'll see
you next. Bye-Bye.
[no audio]