Docker Community Forums

Share and learn in the Docker community.

Newbie question: ip of container when host is not connected to network

I have the network settings below.
I can access the machine with the host IP without problem.
But when the host is not connected to a network I don’t know how to reach the container.
Since I use host IP to connect machines between them (eg. wordpress backend with db) I also have to change ip configuration everytime host’s IP changes.
I guess there is another way of doing it.

Below the network settings of the image and the dockerfile

   "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "b2343a479f716a64ba32ba63dee69bc7a3cf9e9eaf5d37a16a7b552af830eb72",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "443/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "843"
                }
            ],
            "80/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "90"
                }
            ],
            "8080/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "8080"
                }
            ]
        },
        "SandboxKey": "/var/run/docker/netns/b2343a479f71",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "",
        "Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "",
        "IPPrefixLen": 0,
        "IPv6Gateway": "",
        "MacAddress": "",
        "Networks": {
            "centos7_bnet": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": [
                    "portal",
                    "dcdae133542e"
                ],
                "NetworkID": "de7015e503266b0f4ef3bb38f6fb8ff7e650fe8dceeb7eae78985ef61cff753e",
                "EndpointID": "2ad74353c168b97bb0159f282268469c96bab743d3430e1afadbc8e22db6bc22",
                "Gateway": "172.18.0.1",
                "IPAddress": "172.18.0.3",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:12:00:03",
                "DriverOpts": null
            }
        }

relevant part of docker-compose

version: ‘2’
services:
db:
image: mysql:5.7
networks:
- bnet
ports:
- “3366:3306”
portal:
image: centos7
networks:
- bnet
ports:
- “90:80”
- “8080:8080”
- “843:443”
networks:
bnet:

Hard to help debug your problem. You have not provided the Docker Version or Operating System (Docker CE? Docker EE? Linux? Docker for Windows with Linux containers?). And you have not provided the entire docker-compose file.

This example might help you.

Example

My docker-compose.yml file

🐳 gforghetti@172.16.129.75:[~] $ cat docker-compose.yml
version: '3.3'

networks:
  wildbook-network:

volumes:
  wildbook-database-data:

services:

  ################################################################
  # PostgreSQL Database Service
  ################################################################
  database-wildbook:
    image: postgres:latest
    environment:
      - POSTGRES_DB=wildbook
      - POSTGRES_USER=wildbook
      - POSTGRES_PASSWORD=my-secret-password
    volumes:
      - wildbook-database-data:/var/lib/postgresql/data
    networks:
      - wildbook-network

  ################################################################
  # Wildbook Application Service
  ################################################################
  tomcat-wildbook:
    image: gforghetti/tomcat-wildbook:latest
    depends_on:
      - database-wildbook
    networks:
      - wildbook-network
    ports:
      - "8080:8080"
    volumes:
      - ./docker-compose-setenv.sh:/usr/local/tomcat/bin/setenv.sh

🐳 gforghetti@172.16.129.75:[~] $

Bring up the stack

🐳  gforghetti@172.16.129.75:[~] $ docker-compose up -d
Creating network "gforghetti_wildbook-network" with the default driver
Creating volume "gforghetti_wildbook-database-data" with default driver
Pulling database-wildbook (postgres:latest)...
latest: Pulling from library/postgres
6ae821421a7d: Pull complete
060245e5c056: Pull complete
d6c2e60cfc1c: Pull complete
38d23c24a9df: Pull complete
9998068fb35e: Pull complete
2bb2385a4bb9: Pull complete
2ad14c51497d: Pull complete
77519615ed40: Pull complete
aef70912f86d: Pull complete
74cab331a5d3: Pull complete
afdf28fbe06f: Pull complete
46c6c5ea887a: Pull complete
ae19128049b6: Pull complete
3968885a0fc0: Pull complete
Pulling tomcat-wildbook (gforghetti/tomcat-wildbook:latest)...
latest: Pulling from gforghetti/tomcat-wildbook
6c40cc604d8e: Pull complete
e78b80385239: Pull complete
f41fe1b6eee3: Pull complete
65369fd9d03b: Pull complete
7391503a863e: Pull complete
18d5a6a9e618: Pull complete
9c7ee8a3107e: Pull complete
080d12b27dbb: Pull complete
Creating gforghetti_database-wildbook_1 ... done
Creating gforghetti_tomcat-wildbook_1   ... done
🐳  gforghetti@172.16.129.75:[~]

What are the IP addresses on the Docker Node

🐳  gforghetti@172.16.129.75:[~] $ hostname -I
10.0.2.15 172.16.129.75 172.17.0.1 172.18.0.1 172.19.0.1

In my environment the first address is a NAT address. The 2nd address 172.16.129.75 is the IP address of the Docker Node.

Install nmap on the Docker Node. nmap is your friend.

🐳  gforghetti@172.16.129.75:[~] $ sudo apt-get update -qq && apt-get install nmap -y >/dev/null 2>&1
🐳  gforghetti@172.16.129.75:[~] $ nmap --version

Nmap version 7.60 ( https://nmap.org )
Platform: x86_64-pc-linux-gnu
Compiled with: liblua-5.3.3 openssl-1.1.0g nmap-libssh2-1.8.0 libz-1.2.8 libpcre-8.39 libpcap-1.8.1 nmap-libdnet-1.12 ipv6
Compiled without:
Available nsock engines: epoll poll select

Check the Tomcat Web port 8080 with nmap… See below, it’s open (listening and not filtered/blocked).

🐳  gforghetti@172.16.129.75:[~] $ nmap 172.16.129.75 -p 8080 -Pn

Starting Nmap 7.60 ( https://nmap.org ) at 2019-02-15 16:29 EST
Nmap scan report for manager.example.com (172.16.129.75)
Host is up (0.0043s latency).

PORT     STATE SERVICE
8080/tcp open  http-proxy

Nmap done: 1 IP address (1 host up) scanned in 0.06 seconds

Check the Tomcat Web port 8080. See below, it’s open (listening and not filtered/blocked).

🐳  gforghetti@172.16.129.75:[~] $ nmap 172.16.129.75 -p 8080 -Pn

Starting Nmap 7.60 ( https://nmap.org ) at 2019-02-15 16:29 EST
Nmap scan report for manager.example.com (172.16.129.75)
Host is up (0.0043s latency).

PORT     STATE SERVICE
8080/tcp open  http-proxy

Nmap done: 1 IP address (1 host up) scanned in 0.06 seconds

Access the Tomcat application running in the container listening on Port 8080 with curl using the IP address of the Docker Node 172.16.129.75.

🐳  gforghetti@172.16.129.75:[~] $ curl -s 172.16.129.75:8080 | grep 'Apache Tomcat'
        <title>Apache Tomcat/8.5.38</title>
                <h1>Apache Tomcat/8.5.38</h1>

I can also access the Tomcat application running in the container listening on Port 8080 with curl using localhost or 127.0.0.1.

🐳  gforghetti@172.16.129.75:[~] $ cat /etc/hosts | grep localhost
127.0.0.1	localhost
::1     localhost ip6-localhost ip6-loopback
🐳  gforghetti@172.16.129.75:[~] $ curl -s 127.0.0.1:8080 | grep 'Apache Tomcat'
        <title>Apache Tomcat/8.5.38</title>
                <h1>Apache Tomcat/8.5.38</h1>
🐳  gforghetti@172.16.129.75:[~] $ curl -s localhost:8080 | grep 'Apache Tomcat'
        <title>Apache Tomcat/8.5.38</title>
                <h1>Apache Tomcat/8.5.38</h1>
🐳  gforghetti@172.16.129.75:[~] $

And since I have a network connection from my desktop to the Docker Node I can access the Tomcat Web App from my desktop’s web browser.

04%20PM

Hi thanks for the detailed explanation. I didn’t realise that there was also a “docker node” ip.
Now I’m able to access the web server both from public IP and from docker node IP.

However my question was more like this:

I have a web service and a db service, on different containers, and the web service must be configured to connect to the db service.

I can do it, but it’s not “portable”. Now with the docker node ip it’s better as doesn’t depend on the public ip of my Mac. But still I can’t port it on another machine.

Is there a better way of doing it?

The Web service must use the Docker db service name to connect to it.

Refer to this for an example/tutorial.