So now we've had the fun of managing our images, we can take a look at
containers. So we could use our docker run ubuntu, so it takes the
ubuntu image and makes a container out of it.
We've not assigned a name, so there's going to be a random
two‑word name assigned to this container.
It will start up and stop, though.
We could always go through and use docker ps.
Docker ps will show us our running containers, and we won't have any.
Docker ps ‑a will show those that are also stopped.
We can also run a little bit more using our docker run. ‑It, this is going to
give me my interactive terminal. I can specify the name,
it makes it easier to manage if we've assigned a name that we understand.
I'm just calling it u1, based on our ubuntu image.
Now the default command for the Ubuntu image is the bash
command, so we will see that we will get a bash prompt. If
at the prompt we type exit,
we stop that command. That command will be process ID 1 in
our container, so our container will stop.
So if we go back then looking at both ps and ps ‑a, we'll
say that u1, the container u1 is stopped.
If we go back and run another container here, here we're doing the same
thing, but calling it u2. If we want to leave it running, we can use the
CNTRL and pq, and this will keep our container running.
It doesn't shut the bash shell down.
Now when we look at docker ps or docker container ls, we're
able to see our running container. We're also able to
attach back to those containers.
So come on, let's move out to our command line and get some
hands‑on practice managing our containers.
Now look at this, not just a glorious command line,
but many images for us to play with.
Remember we don't have to have the image preloaded, but if we run a container
and the image is not there, it will be pulled automatically.
But let's go through and take a look at the docker container command. If I do a
docker container ls, this is looking for running containers.
Remember, we've got the subcommand container in the same way we've got the
subcommand image. When I do a container ls ‑a, we're looking then for all our
containers, irrelevant if they are running or not.
So we've got some stopped containers.
We can see we've got one named bad and one that was
generated a name called jovial‑fermi.
It's obviously better if we provide our own names, but
we'll get to see this as we go through.
But we've also got the docker prune command, which is a great
way to look at removing all of our stopped containers. So I can
quickly then delete those containers. If I do now the container
ls ‑a, we don't have any.
But remember, if I wanted the shortcut command, it's just docker ps or
docker ps ‑a. So the old school way of doing it is just with a top‑level
Docker command, rather than going into the subcommands, either you can use.
Now if we want to look at creating new containers, that comes from the
docker run command. And if we just do docker run ubuntu, then not a lot
really seems to happen.
We fire up the Ubuntu image.
The default command is bash, so it fires up and stops straightaway.
Docker ps shows that we don't have any running containers.
Docker ps ‑a, though, shows us that we've got this new container
tender_wescoff. It started up bash, but then exited 15 seconds ago, so it
was created 17 seconds ago, but lasted just a paltry 2 seconds. Now we can
run the containers slightly differently.
We can up arrow key and add in ‑it for ‑interactive and ‑tty. We can go
through and clear our screen, adding in ‑‑name, so rather than having a
generated name we can assign our own name, and then, again, the ubuntu
image. When we look at this then, I've got a bash shell running as root
inside of the container, so this is good. But, of course, when I type
exit, we shut down bash, which is going to shut down the container.
So if I look at my docker ps, I don't have any running containers; docker ps ‑a,
and there we can see then the name u1 that exited 9 seconds ago, and obviously
we had a little bit of interactive time with it.
If we go through there and set up another one, so here we've got u2
running, I can list, I can run any commands that I have inside of this
container, but instead of using exit, if I use CNTRL and pq, I get back
to my host operating system, I can use ps ‑a and I can see that u2, the
status is still Up.
But also, if I just use the ps command, we can see
then that it is still up and running.
If I want to connect to a running container, I can use the
docker container and attach. Put in the name.
This is where that having friendly names like u2 really do work for me
so I can then quickly attached to this container.
Now obviously, again, if I use the exit command, I'm going to
close down bash, which is going to close down the container,
so the container becomes stopped.
But I can go back to my container and prune command, if I don't need any
of these containers anymore, and I can remove them.
But this has given us the very basics of running our containers, but
very often containers are going to be used to host services. And
we're going to be taking a look at how we manage our services inside of containers in the next clips.