Detailed explanation on how to get the IP address of a docker container

Detailed explanation on how to get the IP address of a docker container

1. After entering the container

cat /etc/hosts

It will show the IP address of the container itself and the (- link) soft link.

2. Use commands

docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-ID>

or

docker inspect <container id> 

or

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

3. Consider writing a bash function in ~/.bashrc:

function docker_ip() {
 sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1
}

source ~/.bashrc and then:

$ docker_ip <container-ID>

172.17.0.6

4. To get all container names and their IP addresses just one command is needed.

docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)

If you use docker-compose the command will be:

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

5. Display all container IP addresses:

docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Goodbye Docker: How to Transform to Containerd in 5 Minutes
  • Docker dynamically exposes ports to containers
  • How to enter and exit the Docker container
  • Docker learning: the specific use of Container containers
  • Detailed explanation of docker dynamically mapping running container ports
  • Docker removes abnormal container operations

<<:  How to write a Node.JS version of a game

>>:  MySQL 5.7.18 MSI Installation Graphics Tutorial

Recommend

Perform data statistics on different values ​​of the same field in SQL

Application scenario: It is necessary to count th...

Vue uses canvas to realize image compression upload

This article shares the specific code of Vue usin...

Problems with creating placeholders for HTML selection boxes

I'm using a placeholder in a text input and i...

jQuery combined with CSS to achieve the return to top function

CSS Operations CSS $("").css(name|pro|[...

Deep understanding of JavaScript syntax and code structure

Table of contents Overview Functionality and read...

Detailed steps for QT to connect to MYSQL database

The first step is to add the corresponding databa...

UDP simple server client code example

I won’t go into details about the theory of UDP. ...

Introduction to commonly used MySQL commands in Linux environment

Enter the mysql command: mysql -u+(user name) -p+...

Ubuntu 18.04 installs mysql 5.7.23

I installed MySQL smoothly in Ubuntu 16.04 before...

Detailed example of locating and optimizing slow query sql in MySQL

Table of contents 1. How to locate and optimize s...

Do you know how to use Vue to take screenshots of web pages?

Table of contents 1. Install html2Canvas 2. Intro...

Seven ways to implement array deduplication in JS

Table of contents 1. Using Set()+Array.from() 2. ...

Implementation example of react project from new creation to deployment

Start a new project This article mainly records t...