How to change the Docker ps output format?

bobbyiliev

Bobby Iliev

Posted on January 18, 2021

How to change the Docker ps output format?

Introduction

Docker is an open platform for developing, shipping, and running applications.

If you are new to Docker I would recommend going through this Introduction to Docker tutorial!

In this tutorial, you’ll learn how to customize the output of the docker ps command so that it only shows the fields that you really need.

Prerequisites

In order to be able to follow along, you would need a Linux machine with Docker installed.

I'll be using DigitalOcean for this demo, you can use my referral link to get a free $100 credit that you could use to deploy your virtual machines and test the guide yourself on a few DigitalOcean servers:

DigitalOcean $100 Free Credit

Once you have your account ready, you would also need the following:

  • Docker installed and running - to do that you can follow this step by step tutorial here

What is docker ps used for?

The docker ps command is used to show you the running containers on your current Docker host. It gives you information like:

  • Container ID
  • Image
  • Command
  • The exact date the container was created
  • The status of the container
  • The ports that the container is listening on
  • The name of the container

If you run docker ps you would get the following output:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS NAMES
8bf2d29479db        nginx               "/docker-entrypoint.…"   3 weeks ago         Up 2 weeks          80/tcp   pensive_bose
bca2057b7849        eboraas/laravel     "/usr/sbin/apache2ct…"   8 weeks ago         Up 52 minutes       443/tcp, 0.0.0.0:8080->80/tcp   tender_rosalind
Enter fullscreen mode Exit fullscreen mode

In many cases the output might not fit on your screen so it would be spread into multiple lines so it might be hard to read, for example:

docker change ps output format permenantly

Rather than using shell commands like awk to format the output, Docker has a builtin functionality which lets you do that out of the box!

Changing format ad hoc

You can format the output of docker ps on the fly by using the --format argument followed by the name of the fields that you would like to see.

For example if you wanted to get only the container IDs, image names and the container names, you would need to use the following command:

docker ps --format '{{ .ID }}\t{{.Image}}\t{{ .Names }}'
Enter fullscreen mode Exit fullscreen mode

Notice the \t part, this is the delimiter that is going to be used in the output, and it specifies that you want to use tabs.

The output that you would get would look like this:

8bf2d29479db    nginx   pensive_bose
bca2057b7849    eboraas/laravel tender_rosalind
Enter fullscreen mode Exit fullscreen mode

The --format is very handy in most cases. However, the docker ps command might get quite long. So if you want to have a specific format every time, rather than typing the whole command each time, you could set the format permanently in your $HOME/.docker/config.json file.

Making the change permanent

Using the .docker/config.json file will allow you to customize the output of your docker ps command so that it matches your needs and only shows the information that you need.

Start by creating a .docker directory in your user's home folder:

mkdir ~/.docker
Enter fullscreen mode Exit fullscreen mode

After that create a config.json file in that directory:

touch ~/.docker/config.json
Enter fullscreen mode Exit fullscreen mode

Finally, with your favorite text editor open the file:

nano ~/.docker/config.json
Enter fullscreen mode Exit fullscreen mode

And add the following content:

{
  "psFormat": "table {{.ID}}\\t{{.Image}}\\t{{.Status}}\\t{{.Names}}"
}
Enter fullscreen mode Exit fullscreen mode

The table part, specifies which fields you want to see its output.

With the above config file, when running docker ps, we would only see the following:

  • The container IDs
  • The image names
  • The Status of the containers
  • And the names of the containers

That way if you run docker ps on a smaller screen, the output would display on one line and it would be easy to read:

docker-change-ps-output-format-config-json.PNG

Conclusion

For more information regarding the Docker command line, I would recommend reading the Docker official documentation, and also for more information about formatting I would recommend taking a look at the Docker formatting section here.

Hope that this helps!

💖 💪 🙅 🚩
bobbyiliev
Bobby Iliev

Posted on January 18, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related