Now for that I open my Terminal here
and I wanna start simple.
I will run the node image just like this
and keep in mind that node
is an official image from Docker Hub.
Now, if i execute this, it just starts.
And immediately it stops.
Because this is an image which we need to start
in interactive mode so that it expects some input from us.
And then it starts and we can execute
some basic commands in node here.
We can use it as a calculator
but we can also run other node commands here.
It's not the primary thing I wanna do here.
I just wanna highlight
that we can start this node container like that.
It is something we did earlier
in the course at the beginning.
Now we can quit this by pressing CONTROL C twice.
This will then also shuts down the container
which is why we now see it here under stopped containers.
And of course, with Docker container prune
we can remove all those stopped containers.
Okay, so that's how we can run a container.
Now i will do something which might look strange at first.
I'm going to run this node container again
in interactive mode, but also in detached mode.
That means we can't enter commands like this
but internally this containers still is waiting for input,
which will keep it up and running.
So now this containers started.
Hence we can see it here
under this auto assigned name, under running containers.
And now I wanna show you a command
which we haven't used thus far.
And that's the Docker exec command.
The Docker exec command allows you to execute
certain commands inside of a running container
besides the default command, this container executes.
So besides the command that might've been specified
in a Docker file, that command still continues running.
So the application still continues to run
but you can't run additional commands inside of a container.
That's something which sometimes could be useful.
Now to use this command we need the name of a container
of a running container.
And then we can execute a command against that container.
And we could try running npm init
which creates a node project in that running node container.
And you actually see that it started asking me
some questions but then it finished immediately
because exec just like run needs to be started
in interactive mode if you want to stay connected
to that process, you're starting up here
and if you want to be able to provide input.
So just as with run,
we can add the dash IT Flag here on exec
then execute a command against this container
and execute the npm init command against it.
And if we now hit Enter
I'm walked through these questions here
where I provide a name and some other settings
to a new project which is now created
inside of that container.
And now that means that we can run this command
without node and npm being installed in our system
because it runs inside of this container.
Now that's still not too useful here because
well if it's in the container,
we created the project in here
and we have no access to that project
but it's just a first demo showing you what we can do.
There's also is one AVR syntax that can be interesting.
Let's stop this container.
So let's stop this container by its name
and thereafter, let's start it again
but let's start it differently
with Docker exec we can execute commands
in a running container without interrupting
the default command which starts when the container starts.
We also can override that default command though.
Now, when we start the node container
the default command that is executed
in the container is the node executable itself
which enters the so called a Rebel
which is just an interactive mode
where we can run node commands.
That's the default command,
the node image will execute when we started.
And if we use interactive mode, we stay connected
and we can then work with that interactive node input mode.
Now we can override this default command
by adding a new command after the image name.
And that's another syntax we haven't used thus far.
And here I could then specify npm init
to overwrite the default command
which would be to go into this interactive
enter some node code mode and instead execute npm init.
If I now hit Enter we again are prompted to provide
the configuration for this node project,
which we create with npm init.
And if I now hit enter this finishes and therefore,
it also stopped the container thereafter
because we overrode the default command
which would have opened this ongoing node interactive mode
with the npm init command, which just creates a project
and then stops once it finished.
And at this moment, that's all not too useful.
I'm just getting started with these commands
to make you aware of this feature Docker also offers.
Docker exec could be useful for example
to read some log files, which were written inside
of a container without interrupting the main process.
But now did we know about these commands?
Let's build a utility Docker container
or image and then a container based on that.
And let's see how that actually may help us.