(5) Docker view container IP and designated fixed IP

If you don't understand Docker's network mode and network configuration, please check Docker's four network modes and Docker network configuration

1. How does Docker view the container IP

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

The IP address of the container can be obtained directly, such as: 172.18.0.4

Display all container IP addresses:

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

There are two common methods

docker inspect 容器ID | grep IPAddress

Method Two

View docker name:

sudo docker inspect -f='{
   
   {.Name}}' $(sudo docker ps -a -q)

Check the dockers ip:

sudo docker inspect -f='{
   
   {.NetworkSettings.IPAddress}}' $(sudo docker ps -a -q)

In summary, we can write the following script to list the names, ports, and ips of all containers

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

Second, specify a fixed ip for the container

When running a container requires a specified network, we can use the --network parameter to specify that our container is connected to a specified network.

1. Create a network named net01 and the gateway is 10.1.9.1

[root@docker02 opt]docker network create -d bridge --subnet 10.1.9.0/24 --gateway 10.1.9.1 net01
53db74e6642a2a0fad3b55ef5c109b79b36a100df733074f300ff222828ec6ea
[root@docker02 opt]docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
aa53a77695ff        bridge              bridge              local
452e063a1c6f        harbor_harbor       bridge              local
3a821c0610b7        host                host                local
53db74e6642a        net01               bridge              local
18f030428279        none                null                local

 2. Create a container 1, specify the IP as 10.1.9.100, and the network as net01

[root@docker02 opt]docker run -it --rm --network=net01 --ip 10.1.9.100 busybox
/ #
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
166: eth0@if167: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
    link/ether 02:42:0a:01:09:64 brd ff:ff:ff:ff:ff:ff
    inet 10.1.9.100/24 brd 10.1.9.255 scope global eth0
       valid_lft forever preferred_lft forever

  3. Create a container 1, specify the IP as 10.1.9.101, and the network as net01

[root@docker02 ~]docker run -it --rm --network=net01 --ip 10.1.9.101 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
177: eth0@if178: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
    link/ether 02:42:0a:01:09:65 brd ff:ff:ff:ff:ff:ff
    inet 10.1.9.101/24 brd 10.1.9.255 scope global eth0
       valid_lft forever preferred_lft forever

 4. Test the IP connectivity of the custom network

/ # ping 10.1.9.100
PING 10.1.9.100 (10.1.9.100): 56 data bytes
64 bytes from 10.1.9.100: seq=0 ttl=64 time=0.141 ms
64 bytes from 10.1.9.100: seq=1 ttl=64 time=0.064 ms
64 bytes from 10.1.9.100: seq=2 ttl=64 time=0.174 ms
64 bytes from 10.1.9.100: seq=3 ttl=64 time=0.069 ms
64 bytes from 10.1.9.100: seq=4 ttl=64 time=0.070 ms
64 bytes from 10.1.9.100: seq=5 ttl=64 time=0.064 ms

 5. View net01 network information

[root@docker02 opt]docker network inspect net01
[
    {
        "Name": "net01",
        "Id": "412877924ea50a7e2838930b040ebd19f75e8808cba5220df03f23b8e9fa2ba0",
        "Created": "2018-03-29T14:29:02.930787227-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "10.1.9.0/24",
                    "Gateway": "10.1.9.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "eb264658b33532e014624da402ca1d5c9ac2fb723b96b23051834ce2fef86faa": {
                "Name": "eager_chatterjee",
                "EndpointID": "a0993eefa02a9e562ee25af0c0c5f29bb2e3fd1c00b6e85a12031bb26c1dee57",
                "MacAddress": "02:42:0a:01:09:65",
                "IPv4Address": "10.1.9.101/24",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

 

Guess you like

Origin blog.csdn.net/sanmi8276/article/details/108749676