DevOpsDockerDockersHow to

Docker container inspect

When you run a container, there is a lot of metadata that gets associated with the container.
There are many times that you will want to review that metadata. The command for doing that is:

# using the new syntax
# Usage: docker container inspect [OPTIONS] CONTAINER [CONTAINER...] docker container inspect web-server
# using the old syntax
docker inspect web-server

As mentioned, this command returns a lot of data. You may only be interested in a subset of the metadata.
You can use the –format parameter to narrow the data returned. Check out these examples:

  • Getting some State data:
# if you want to see the state of a container you can use this command
docker container inspect --format '{{json .State}}' web-server1 | jq
# if you want to narrow the state data to just when the container started,
use this command
docker container inspect --format '{{json .State}}' web-server1 | jq
'.StartedAt'
  • Getting some NetworkSettings data:
# if you are interested in the container's network settings, use this
command
docker container inspect --format '{{json .NetworkSettings}}' web-server1 |
jq
# or maybe you just want to see the ports used by the container, here is a
command for that
docker container inspect --format '{{json .NetworkSettings}}' web-server1 |
jq '.Ports'
# maybe you just want the IP address used by the container, this is the
command you could use.
docker container inspect -f '{{json .NetworkSettings}}' web-server1 | jq
'.IPAddress'
  • Getting data for more than one container with a single command:
# maybe you want the IP Addresses for a couple containers
docker container inspect -f '{{json .NetworkSettings}}' web-server1 webserver2
| jq '.IPAddress'
# since the output for each container is a single line, this one can be
done without using jq
docker container inspect -f '{{ .NetworkSettings.IPAddress }}' web-server1
web-server2 web-server3

Here some of examples how to install jq on each of OS’s

# install jq on Mac OS
brew install jq
# install jq on ubuntu
sudo apt-get install jq
# install jq on RHEL/CentOS
yum install -y epel-release
yum install -y jq
# install jq on Windows using Chocolatey NuGet package manager
chocolatey install jq

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

NQuG4

Please type the text above:

Close
Close