Docker

How to get the Docker container IP address

Welcome readers, in this tutorial, we will discuss how to get an IP address assigned to a running docker container.

1. Introduction to Docker

In the present world, Docker is an important term,

  • Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
  • Is a standard for Linux Containers
  • A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux

1.1 Docker Terminology

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
  • Engine: The code that manages, creates and runs the Docker containers
  • Hub: A public developers registry to distribute their code
  • Repository: A collection of Docker related images i.e. different versions of the same application

1.2 Docker Command Basics

Here’s an example command.

Docker container IP - Basic structure
Fig. 1: Basic structure

1.3 Need for using Docker

  • For environment replication, while the code runs locally on the machine
  • For numerous deployment phases i.e. Dev/Test/QA
  • For version control and distributing the application’s OS within a team

1.4 Setting up Docker

If someone needs to go through the Docker installation, please watch this video.

To start with this tutorial, we are hoping that users at present have the Docker installed on a Windows operating system.

2. How to get the Docker Container IP address

The docker inspect command is used to get the low-level information such as Network settings, Config, Image, Mounts, etc. about the Docker Container in a JSON format. Below is the command syntax –

docker inspect <Option> <container_name | container_id>

To get the information about the Docker Container we need to make sure that the Docker Container(s) is up and running. To list this information, we can use the docker ps command as shown in Fig. 2.

Docker container IP - information of running Docker
Fig. 2: Viewing the information of running Docker Container(s)

2.1 Get Docker Container IP Address

To fetch the IP address for all the running containers we can use the following command –

docker ps -aq | xargs docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}'

This command lists down the IP address of all the running docker containers as shown in Fig. 3.

Fig. 3: IP address

Try out these commands in your development environment to practice and learn.

3. Conclusion

That is all for this tutorial and I hope the tutorial will help you understand the basic commands to get the IP address for a Docker container. Happy Learning and do not forget to share!

4. Download the source code

Download
You can download the full source code of this example here: How to get the Docker container IP address

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button