Docker Home Lab | Managing Containers

Managing Docker Containers

In the previous post we saw a number of ways to create and starts containers. Once you started them, there are various command you have to manage them. You were already introduced to a few, but let’s look to some more of them

CLICK IN THE FOLLOWING LINKS TO JUMP TO THE POST OF THE THIS SERIES: 

DOCKER HOME LAB | Where to Start ?

DOCKER HOME LAB | Managing Docker Containers

DOCKER HOME LAB | Commit changes to a container

DOCKER HOME LAB | Using / Finding / Sharing in Docker Public index

DOCKER HOME LAB | Building Containers with Dockerfile

DOCKER HOME LAB | Affect container with Dockerfile

In this post I will introduce this commands:

docker ps

docker inspect

docker logs

docker stop

docker kill

docker rm

Don’t be intimidated most of these are very simple commands.

Managing containers is mostly a matter of managing processes, but in a way that is container aware.

Docker provides a suite of utilities to manage container processes in much the same way you would with regular processes.

We are going to see how you can inspect containers, see the Logs, stop them and ultimately removes them.

Let’s begin by starting a couple of containers that will sticker around by doing pointless work. Ping will work well

docker run -d ubuntu ping google.com

docker run -d ubuntu ping.google.com

We can see all the running containers with docker ps

docker ps

This shows relevant information about the containers, the most useful which is the container ID; which we can use with other commands.

However you might also notice the names generated for each container

You can also use this human friendly name to refer to container and you can even set this family name the using the –name flag with docker run

docker ps

One frequently used flag of docker ps is the –a which shows all containers, including containers that has stopped

docker ps -a

We can use the IDs listed to get more information about the container running or not

For example we can take any container and inspect it with docker inspect. This give us a Jason data structure of the state of that container. Take a look and see what kind of information is associated with the container

docker inspect

Again we can also inspect to container by his name like this:

docker inspect

Let’s dig deeper into these processes. We can see the output of any container running or not using docker logs

If it is a running container we can follow the logs live as if we were using tail –f flag

docker logs

Docker logs let you see process output, but if you want to interact with the detach container process you want to attach it using docker attach. This is a more advanced command so I let you to explore it at your own, however be careful if you don’t use it correctly you can lock up your terminal.

Checking and copying the container ID can be a bit annoying, names make it a little easier, but here is a quick trick to work with last container you made

Let’s make a detach container

docker run

This will return the full container ID, but we don’t even need to bother with that because we can run a variant of docker ps to get the ID of the last container we made.

Just use the –l flag to only show the last container and –q to only show the id of the containers, we can use this as a subcommand in for example getting logs

docker ps

When your container processes don’t end by themselves you can stop them with docker stop

docker stop

By default it would try to stop with a sig term, but then after a timeout kill it with sig kill

I’m using a more forceful so I often skip docker stop and use docker kill.

Now if we look at our docker ps we don’t have any running containers

DP2-11

But if we run docker ps –a, we can see they are still around. They are not running but their filesystem and metadata exist.

docker ps -a

This can be useful for inspecting logs after you stop the process

If you don’t run commands with the –rm option or run a lot of detach processes you end up with a lot of stopped containers sitting around. You can delete them individually with docker –rm

docker rm

But if ever you want to just clear all out, including running containers we can use docker ps -aq which list all containers ids with –xargs to call docker rm –f for each id like this:

docker ps -aq | xargs docker rm -f

Now we have no running or stop containers listed

docker ps -a

To wrap-up we have learned all the basic commands for working with the process aspect of containers

We can now start, list, stop, inspect, follow logs, and remove your containers

We will continue to explore in the next post how to commit changes to a container image. Stay tuned for the next post!!

Docker Home Lab

Docker Home Lab

Trackbacks

  1. […] Docker.We will continue to explore in the next post more advance docker commands and options. Click here to continue to the next […]

  2. […] our previous post we learned how to start and manage containers. You may have noticed every time that we run a […]

  3. […] DOCKER HOME LAB | Managing Docker Containers […]

  4. […] DOCKER HOME LAB | Managing Docker Containers […]

Speak Your Mind

*

%d bloggers like this: