I mentioned that containers are based on images.
And actually, there will be two ways
of creating or getting an image
so that we then can run a container.
The first way is that we use an already existing image.
For example, because a colleague of ours already built it,
or also very common, because we use
one of the official pre-built images
or one of the images shared by the community.
And a great source for that would be Docker Hub,
which you can simply Google to find hub.docker.com.
And there you can log in
but you don't need to do this right now.
Instead, here in the search bar,
we can, for example, search for node.
And what we'll find is the official node Docker image,
which we could use to build a node application container,
a container which will later run a node application.
Now this node image which you find on Docker Hub,
can be used by anyone and it is distributed and created
and maintained by the official node team.
Now we will use such official images a lot throughout
this course and in general when working with Docker,
but we can especially use it right away to get started
with images and containers here.
And all we need to do for that is open up
the command prompt or terminal on your system
and then navigate in any folder of your choice
and run docker run node.
This command here will use this node image
which we find on Docker Hub and it will utilize it
to create a so called container based on this image,
because as I mentioned, containers are really just
the running instances of images.
Images contain the setup code the environment,
in this case, the node image contains the node installation.
And then we can run the image to run the application
or in this case, to simply run the node interactive shell.
Now if you hit Enter, this will give you an error
that it doesn't find this image locally,
which makes sense because it's on Docker Hub,
and then it'll automatically pull it from Docker Hub.
So now this downloads the latest node image from Docker Hub
and once it downloaded it locally onto our machine,
it will run this image as a container.
So let's wait for this download to finish here
and thereafter, you will see nothing special here.
It's done and we can enter more commands.
So did we now run the container?
Did we now create a container based on an image?
Well, yes, we did.
But this container isn't really doing much.
Node is of course just a software you could say
and indeed we can execute node to get an interactive shell
where we can insert command.
But by default, and that's important,
a container is isolated from the surrounding environment
and just because there might be some interactive shell
running inside of a container
does not mean that this shell is exposed to us as a user.
Nonetheless, this container was created.
And you can tell by running Docker PS dash A.
PS stands for processes and with the dash A flag,
this will show you all the processes all the containers
Docker created for us.
And if you hit enter, you will see that a minute ago
I actually created a container.
And to make this a bit easier to read
I'll zoom out a bit and rerun this.
And you'll see we created a container with this ID.
The image was the node image.
It was created two minutes ago,
it exited so it's not running anymore
and it also received a automatically generated name.
We'll dive into names and into configuring containers
in general in greater detail later.
But what we see is that something happened
but that it's not running anymore
because as I said a container runs in isolation.
And even though we executed node as a image
or as a container based on the node image,
this alone doesn't do much.
Because the interactive shell exposed by node
is not automatically exposed by the container to us.
That is something we can change though.
If we repeat the command from before, docker run node,
and we now add an extra flag in front of node,
the dash IT flag, then we will actually tell Docker
that we wanna expose an interactive session
from inside the container to our hosting machine.
And hence if we now hit enter,
we actually are in that interactive node terminal
where we can run basic node commands.
For example, one plus one,
but we could also use node API's in here.
But that's of course not the focus of this session.
Now the important thing about this here is that
node here is now running inside of that created container
and it's just exposed to us by adding this extra flag
so we can interact with that container
and with node running in the container.
Node is not running on our machine here
and I can prove this.
Please note that here we're interacting with node 14.9,
which is the version that at the moment I was recording this
was pulled into this image,
and therefore is being used in this container.
Now if I quit this process with Control + C pressed twice
now the container will shut down,
when I quit this and I run node dash V here
like this on my system so not inside of the container,
I see 14.7 as a version.
So locally on my system I got a different version installed,
then the version we interacted with here,
which proves that the version we did interact with
must be the version from inside the container.
And we don't need node to be installed on our system at all
in order to be able to run this node container
and interact with it.
And that is really how you work with containers.
At least these are some first steps
of working with containers,
and this also shows us what images and containers are.
Images are used behind the scenes
to hold all the logic and all the code a container needs,
and then we create instances of an image
with the Run command.
This creates then the concrete containers
which are based on an image.
And if we now run Docker PS dash A again,
we see that we get two containers now.
Both are not running anymore, they have been exited.
But we have more than one container based on the same image.
Both containers are based on the same image.
And yes, they are not running anymore
but we could absolutely have two containers which are based
on the same image up and running at the same point of time,
simply by opening up multiple terminals
and then while repeating this docker run command.
This would absolutely be possible.
And that's the idea behind images and containers.
Images contain the code, the setup, the meet, you could say
and containers are then the running instances
of those images.