Now, there is more we can do
with the docker command than with containers and images.
We already saw a couple of different commands and action
and I wanna come back to this mode which we have here
when we restarted a container and the mode which we had
when we initially started a container.
Here, when we're restarting it
the process in our terminal here finished immediately.
We're not attached to this running docker container
in the terminal anymore.
Nonetheless, it clearly is still running
as we can see with docker ps.
So, this container is running, it's just not blocking us
here in the terminal and that's different compared
to the docker run command we executed before.
If I run this again, binding to a different local port
since 3000 is already used by the already running container
but if I use a different port here
I'm starting a new additional container
based on the same image, so, therefore with docker ps
in a different terminal here
we see we got two containers up and running now
but you see that for docker run, we're stuck
in this process, I can't enter more commands here.
I mean, I can type but I can't commit them.
I can't confirm them with enter
instead, this process is blocking this terminal
and with docker start that was not the case.
Now, this is not a blog or anything you have to accept
this is something you can configure if you wanna be
in this attached mode or in detached mode.
And it's just a case that for starting with docker start
the detached mode is the default for running with docker run
the attached mode is the default.
And now the question of course is which mode do you want
and why does this even matter?
Well, in this example application, I am logging
something to the console whenever we set a new goal.
So, here if I add, Learn Docker in-depth! as a goal
on local host 3000, that's my started container
which was detached, we see nothing in the console anywhere.
On the other hand, if I go to local host 8,000
which is that new container, we run with docker run
and I set my goal here
you will see that it also shows up into terminal here
because here we're attached to this running container
and attached simply means that we're listening to the output
of that container.
For example, to what's being printed to the console
and this might or might not be what we want.
We can also run a container in detached mode
if we want to, we don't have to accept that default
of being attached.
For this, I'll quickly stop this more recent container
which I started with docker stop
which will take a couple of seconds
so, that's the container we started with docker run
and once this is stopped, I will re-run it
but now in detached mode by adding -d as an extra flag
in front of the image ID here
if, I now enter it you see, now I just get the ID
the automatically generated ID of the new container
you see with docker ps that we have this container up
and running that still shortened ID off that up
and running container by the way
and we see that container is running
and therefor visiting local host 8,000 also works.
But now if I Learn Docker in-depth!
for example, here on local host 8,000
we don't see that log into terminal
where we execute a docker run
because now we're not attached to the output
since we used the -d flag to start in detach mode.
The advantages that we now can use one
and the same terminal to also do other things.
If you still wanna look into your running container
you got two ways of doing that.
For one, we can see with docker help
that there is an attached command.
So, we can attach ourselves again to a running container.
We can simply find the container and use its name
or its ID both works and then run docker attach
and that name and we are attached again
and therefore now, if we change it again
and set a new goal, we do see this output again.
So, that's one way of doing it
but actually let me stop this container again
and let me restart it thereafter
now in detached mode automatically
since that is the default for docker start
and let me show you another way of getting access to
for example, the log messages that are printed inside
of a container.
This is running again.
If I now type Learn Docker in-depth! here
and commit this, so I save this and set this goal
we don't see the goal because we are detached
and we could attach again to see future log messages
or another thing we can do another useful command
is the docker logs command which fetches the logs
that were printed by a container.
And here we can first of all, find the container
eloquent_brown is the name of the container
I'm interested here
and then we can run docker logs on that container
and we see the past logs that were printed
by that container.
We can also, if we inspect the options of docker logs
enter follow mode by adding -f to keep on listening.
So, basically we're now attaching ourselves again.
So, if I add -f here, we again get
this attached process where we see future logs
future output by that container again.
So, these are the different options we have for that.
And I'm spending a lot of time on that
because it is important that you understand
the difference between a running and a stopped container
and an attached and a detached container.
No matter if it's attached or detached
it's still up and running but if you then need information
from inside the container, you can use docker logs
or attach yourself to the container again
to get that extra information.
Now ,with that I will again
docker stop eloquent_brown
to bring down that container and detach myself again here.
Now, one last note about all of that, if you would want
to restart a stopped container like eloquent_brown here
in attached mode, right from the start
you can do, so by adding the -a flag to the start command.
Now, you start this in attached mode.