Get a Docker Containers IP Address from the Host

#Introduction

Docker containers are isolated. How do you need to know each container’s IP address? Docker networking is a little complicated. Containers launched by default in the “bridge network” and allowed to communicate with another container directly. Containers added to non-default networks will be able to access each other through their alias

My example

Create new network is devopsroles, run containers, and access the other containers using an alias as below:

docker network create devopsroles
docker run --net devopsroles  --name nginx -d nginx
docker run  --name mongodb -d mongo
docker network connect devopsroles  --alias mongohost mongodb

Get a Docker Containers IP Address from the Host

How to get IP address Docker containers from the Host OS.

docker ps
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' name_or_id

The output terminal is as follows

[vagrant@localhost ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS              PORTS       NAMES
fc0c94f5ef9d   mongo     "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes        27017/tcp   mongodb
a2b21fffc9d8   nginx     "/docker-entrypoint.…"   6 hours ago     Up About a minute   80/tcp      nginx
[vagrant@localhost ~]$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' fc0c94f5ef9d
172.17.0.2172.18.0.2
[vagrant@localhost ~]$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' a2b21fffc9d8
172.18.0.3
[vagrant@localhost ~]$

You can use the docker network inspect command to print all containers in the given network.

docker network inspect bridge -f '{{json .Containers}}'

Get network config from the containers

docker exec -it container_name_Or_ID ip a

Conclusion

You have to Get a Docker Containers IP Address from the Host. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

Comment moderation is enabled. Your comment may take some time to appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.