So let's create our first container.
And let's get our hands dirty,
even though I will say right away that at this point,
we're not going to understand everything here.
We're just going to write some code,
and bring up a container,
and we're going to understand all the details
a little bit later in the next core section.
I just wanna immediately get something up and running,
we also have this example to validate
that everything is working as it should.
So here's the example, you'll find it attached.
It's a very simple Node.js application.
And by the way,
I just wanna highlight, it's just an example.
You don't need to know Node.js for this course.
You can follow along, even if you don't know it,
because we're not going to write any Node.js code.
You can use Docker for any programming language
and any project and application
written with any technologies of your choice.
What you learn in this course will apply.
It's just an example,
because I have to pick some programming language
for the example in the end.
So this is a Node.js application with some basic dummy code.
And in the end,
this will start up a web server on port 3000,
which will listen to get requests on no particular path.
And we'll then send back some dummy HTML.
I also got this dummy database connection code,
which doesn't really connect,
but just set a timer of one second,
until these servers launched in the end.
And I'm having this here so that we have
a top level await example,
which is a Node.js feature
for working with asynchronous code,
which only works if you use Node.js version 14.3 or higher.
So this is the Node.js code.
If we would want to run it locally,
without Docker and without containers,
we would have to visit nodejs.org and then download that
latest version of Node.js,
whichever version that is when you're watching this.
Walk through the installer that gives you,
and then open up a new terminal,
and then run this app mjs file.
By running node app.mjs.
And actually before we do that,
we would have to run another command,
npm install, which installs all dependencies
listed here in packaged.json,
which are simply third-party packages
this app mjs file needs to work correctly.
Again, if that all doesn't tell you too much,
that's no problem here.
These are the steps we just would have to execute,
if we would want to run this code here
locally on our machine.
However, the idea with Docker was a different one.
We want to run this code in a container.
And for that, we first of all,
need to create a so-called image,
because containers are always based on images.
And you're going to learn more about that
and this relation between image and container
in the next course section.
For the moment to create such an image,
we simply create a Docker file,
so a file which is simply named Docker file
without any extension.
And in here, we now describe to Docker,
how our container in the end should be set up.
And we do this by adding a couple of instructions.
Now, since we are going to dive
into images and containers in greater detail
in the next section,
I don't want to waste your time
with a bunch of instructions,
which we don't fully understand yet.
So instead attached to find a finished Docker file,
which in the end just has a couple of instructions
that we want to use Node.js as a base image
so that we want to have Node.js available
inside of our container,
that we have a certain directory
in the container file system.
Every container has its own file system.
So that we want to have a special directory in there
in which you wanna work.
That we then copy the package.json file,
into our working directory.
Then we run the npm install command
to install all the dependencies our application needs.
Then we copy the rest of the code here.
Then we expose port 3000 to the outside world.
Because that's the port our application is listening on
and we wanna be able to reach that port
from outside of the container,
not just from inside the container.
And then we execute app.mjs with the node command,
which is available because
we're running in a node environment.
Again, this was a quick walk through.
The details will follow in the next core section.
Now with the Docker file created, we open up a terminal.
For example, here,
I'm using the terminal integrated into visual studio code,
the editor I'm using here.
And with Docker setup,
which we have at this point, we run Docker build .
and this builds the image the Docker file.
It finds in the directory in which you run this.
And therefore I'm using this integrated terminal here
because this automatically launches
in this project directory.
So any commands I execute here,
run inside of this directory.
So now this will build an image based on this Docker file,
which is the first thing we need to do.
So simply hit enter here.
If you're getting an error at this point,
make sure you got Docker installed and started.
Start Docker desktop if you installed that
and make sure Docker is running in the background.
And therefore at some point, this should then work.
And what this now does is, it grabs this node environment,
which already exists,
it downloads it from the cloud,
from Docker Hub to be precise, but more on that later.
And then it will set up an image
for a container to be launched
with all these setup steps being executed
inside of the image.
So it will give us an image
which has prepared to be started as a container.
So this now walks through all these steps
and after some time it should be done.
You get us successfully built and then some ID
for this image output.
As a side note on Windows,
your output looks slightly different.
There you find the idea of the image which was built here.
Now we can use this ID here,
and then run a container based on this image
with the docker run command.
Like this, docker run, and then the image ID.
However, actually, since our container here has a port
to which you wanna communicate,
we actually need to publish that port on the container,
which we want to run.
And we do it as by adding the -p flag
on docker run here.
And we then publish port 3000 on port 3000.
Which means we can use our local host, on our local system
to reach the application running on port 3000
instead of the container.
Because by default, there is no connection
between container and our host operating system.
If we wanna send HTTP request for example,
to an application running in a container,
we need to open up the port on the container
to which we wanna communicate.
Otherwise it's a locked network in the container and we
can't reach it from outside.
With that however we can hit enter,
and this now we'll run this container
and you can tell that it's running
by the fact that you now can't enter any more commands here,
but that instead, this command is stuck.
It's stuck because we have a running web server.
Now you can visit local host 3000,
and you should see "Hi there!", there.
And that's our first Dockerized application,
even though of course, at this point,
we haven't written the stalker file ourselves
and we don't fully understand what's happening there.
But we're going to learn all of that
throughout the next sections.
To now stop this container,
we can open up a new terminal,
by clicking on this plus here,
and then you can run docker ps,
which will list all running containers
and then grab this name of this container,
which was started, it's an automatic your assigned name
and run dockers stop, and then this name,
and this will now stop this container and shut it down.
This can also take a couple of seconds,
but thereafter this container is not running anymore.
And therefore once this did stop,
if you reload local host 3000,
you can't reach the site anymore.
Although in that terminal, where you ran docker run,
you now are out of this running process again,
and you can enter more commands.
And that's it, that's this very first basic example,
not too fancy.
And of course not a lot of work from our side.
But it shows us that docker was installed successfully,
that it works and that we kind of did create
such a containerized application.
Because we definitely did not install Node.js on our system.
We did not run npm install in this project folder,
to install all third-party dependencies
and still we were able to bring up that web server
and visited on local host 3000.
And that was possible because of Docker.