Docker: A few diagnostic commands

Ever wondered how you could investigate issues/failures or even find out more information about your Docker image and containers?

In today's post we will be doing just that by answering the following questions:

  • how do you get detailed information about a docker object?
  • how do you get the full history of a docker image?
  • You're running an application in a container and something that is meant to be working fails. How do you view the logs for this container?
  • What command can you use to see the runtime metrics of your container?

For this article, I have gone ahead and [pulled] / created a few docker images, containers, and networks (only one 😃).

a list of available docker objects on my environment

Get detailed information on a Docker object

To enable us to get detailed (or specific) information of a Docker object we will be using the docker inspect command.

docker inspect by default gives us a JSON output of the specified object. This JSON output holds all the information of the object in question.

The docker objects we will be using docker inspect in this section are image, container, and network.

Before we get to the sample exploration, it is worth showing the syntax for docker inspect

docker inspect [OPTIONS] <OBJECT_ID_OR_NAME>ORdocker image inspect [OPTIONS] <IMAGE_ID_OR_NAME>ORdocker container inspect [OPTIONS] <CONTAINER_ID_OR_NAME>ORdocker network inspect [OPTIONS] <NETWORK_ID_OR_NAME>

Now that we know the syntax, let us consider a few examples…

Images first, as without them we can’t have containers.

docker inspect 9f33b7c5cb35 This will give us a detailed JSON output with details of this image. For context, this image id is the image for containrrr/watchtower which I have already pulled. Given the huge JSON output, I have tried to capture this with the below image

Result of docker inspect 9f33b7c5cb35

Ok, I hear you ask, with this tall JSON output, how do we filter through to retrieve a specific value? 🤔

We can do this by using the --format option which docker inspect makes available to us. With this, let us now get the command that will be executed when a container initialised by this image will run

docker inspect --format='{{.ContainerConfig.Cmd}}' 9f33b7c5cb35
docker [image] inspect with format option

Let us consider containers next…

docker container inspect f4b9d7445d2e

docker container inspect result

Now let's filter out the log driver used by this container. We can do this by running the below command

docker container inspect --format='{{.HostConfig.LogConfig.Type}}' f4b9d7445d2e

Last in this section, we will inspect an already existing docker network

docker network inspect eb555521a304

result of a docker network inspect

Get image history

The key advantage of looking through the history of an image is that it gives information on how the image in question was built.

As is the pattern in this article, we will look at the syntax of docker history command then consider a few examples

docker history <IMAGE_NAME_OR_ID>ORdocker image history <IMAGE_NAME_OR_ID>

An example…

docker image history --no-trunc containrrr/watchtower will result in the below

View container logs

Logs are a wonderful thing, as you get to see when an exception (an error) is thrown in your application. Docker containers allow seeing log outputs whilst the container is running.

So how do we view logs of a docker container? 🤔

docker logs <CONTAINER_NAME_OR_ID>ORdocker container logs <CONTAINER_NAME_OR_ID>ORdocker container logs -f <CONTAINER_NAME_OR_ID>

Given we have a few containers already, we will go ahead and view the log for one of them

docker logs f4b9d7445d2e

logs from container with id f4b9d7445d2e

Now, we can inspect our docker objects, view the history of images, see logs to know what exactly is doing. How about viewing runtime metrics of our container???

View container runtime metrics

We can view metrics of a running container by using the docker stats command.

docker stats with no container ID or name would return a continuous stream of resource stats for all started containers.

A few examples…

docker stats with no container ID.

docker stats f4b9d7445d2e

In Summary 🗒

In this post, we have looked at the following docker commands with a few examples

  • docker inspect;
  • docker history;
  • docker logs; and
  • docker stats

Once again thank you for reading and looking forward to your feedback.

Java | Flutter | Android | Some JavaScript | API Design

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store