How can I get the IP address of a running Docker / Podman container?. A container is a set of one or more processes that are isolated from the rest of the system and has information that can be queried in the command line interface using docker or podman CLI tools.

In this guide I’ll demonstrate how you can query for the IP address of a container running in Docker or Podman container runtime environment. I have MySQL 8 containers as shown below.

Podman container:

$ podman ps
CONTAINER ID  IMAGE                      COMMAND  CREATED       STATUS           PORTS   NAMES
83524fde1c05  docker.io/library/mysql:8  mysqld   10 hours ago  Up 10 hours ago          mysql-8

Docker container:

$ podman ps
CONTAINER ID  IMAGE                      COMMAND  CREATED       STATUS           PORTS   NAMES
83524fde1c05  docker.io/library/mysql:8  mysqld   10 hours ago  Up 10 hours ago          mysql-8

We’ll use docker/podman inspect command option to query container’s IP Address. Here is the syntax:

# Docker
$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' <container_name_or_container_id>

# Podman
$ podman inspect -f '{{ .NetworkSettings.IPAddress }}' <container_name_or_container_id>

See these examples:

$ podman inspect -f '{{ .NetworkSettings.IPAddress }}' 83524fde1c05
10.88.0.2

$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' mysql-8
172.17.0.2

The IP address is assigned through DHCP from a bridge available in the host system. For default setting the bridge is created during Docker or Podman installation.

$ ip ad | egrep -i 'docker0|podman0'
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
9: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default

10: cni-podman0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    inet 10.88.0.1/16 brd 10.88.255.255 scope global cni-podman0
11: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master cni-podman0 state UP group default

Confirm you can ping the IP address.

$ ping -c 2 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.074 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.090 ms

--- 172.17.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1011ms
rtt min/avg/max/mdev = 0.074/0.082/0.090/0.008 ms

Service reachability should also be okay.

Testing with telnet:

$ telnet 172.17.0.2 3306
Trying 172.17.0.2...
Connected to 172.17.0.2.
Escape character is '^]'.
J
8.0.22
e<-|Izcaching_sha2_password

Actual connection to my MySQL service on port 3306.

$ mysql -u root -p'[email protected]' -h 172.17.0.2
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT VERSION ();
+------------+
| VERSION () |
+------------+
| 8.0.22     |
+------------+
1 row in set (0.00 sec)

mysql> \q
Bye

That is how you can easily and quickly query the IP address of a container running on a Linux server using Docker or Podman container management tool.

More articles available on containers:

How To Run MySQL Database in Docker Container

How To Manage Docker Containers With Ansible

Run Linux Containers with LXC/LXD on Ubuntu

LEAVE A REPLY

Please enter your comment!
Please enter your name here

fifteen − ten =