Home > database >  Why is my docker networks gateway IP unaccessible from my host OS
Why is my docker networks gateway IP unaccessible from my host OS

Time:09-29

I am trying to get the following set up working:

  • My local machine OS = Linux
  • I am building a docker mysql container on this local machine
  • I plan to seed the database within the container, and then run tests locally (on my local Linux machine) against this container (which i will spin up on my linux machine too)

Unfortunately when running my tests and trying to connect to the container, the default bridge networks Gateway IP is inaccessible.

My docker-compose.yaml file is as follows


version: "3.4"

services:
  integration-test-mysql:
    image: mysql:8.0
    container_name: ${MY_SQL_CONTAINER_NAME}
    environment:
      - MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
    ports:
      - "3306:3306"
    volumes:
      # - ./src/db:/usr/src/db #Mount db folder so we can run seed files etc.
      - ./seed.sql:/docker-entrypoint-initdb.d/seed.sql
    network_mode: bridge
    healthcheck:
      test: "mysqladmin -u root -p$MYSQL_ROOT_PASSWORD -h 127.0.0.1 ping --silent 2> /dev/null || exit 1" 
      interval: 5s
      timeout: 30s
      retries: 5
      start_period: 10s 
    entrypoint: sh -c "
      echo 'CREATE SCHEMA IF NOT EXISTS gigs;' > /docker-entrypoint-initdb.d/init.sql;
      /usr/local/bin/docker-entrypoint.sh --default-authentication-plugin=mysql_native_password
      "

When running docker network ls i see the following

 docker network ls
NETWORK ID     NAME                  DRIVER    SCOPE
42a11ef835dd   bridge                bridge    local
c7453acfbc98   host                  host      local
48572c69755a   integration_default   bridge    local
bd470f8620fd   none                  null      local

So the integration_default network was created. Then if i inspect this network

docker network inspect integration_default
[
    {
        "Name": "integration_default",
        "Id": "48572c69755ae1bbc1448ab203a01d81be4300da12c97a9c4f1142872b878387",
        "Created": "2022-09-28T00:48:20.504251612Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.27.0.0/16",
                    "Gateway": "172.27.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "79e897decb4f0ae5836c018d82e78997e8ac2f615b399362a307cc7f585c0875": {
                "Name": "integration-test-mysql-host",
                "EndpointID": "1f7798554029cc2d07f7ba44d057c489b678eac918f7916029798b42585eda41",
                "MacAddress": "02:42:ac:1b:00:02",
                "IPv4Address": "172.27.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "integration",
            "com.docker.compose.version": "2.7.0"
        }
    }
]

Comparing this to the default bridge

docker inspect bridge

[
    {
        "Name": "bridge",
        "Id": "42a11ef835dd1b2aec3ecea57211bb2753e0ebd4a2a115ace8b7df3075e97d5a",
        "Created": "2022-09-27T21:54:44.239215269Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

Interestingly running ping 172.17.0.1 on my Linux machine works fine but ping 172.27.0.1 fails to return anything

UPDATE I have got it working now. By specifying network_mode: bridge in my docker compose file i was able to use the default bridge network which was accessible on my local machine as i mentioned.

However, i would like to know why creating my own network didn't work here. Does anyone know why this was the case?

CodePudding user response:

Docker networks are meant to be hidden and you should let docker do its job unless there is a good reason for it.

The correct way to interract with a service is through its open ports. And those ports are mapped on the host so that talking to the host:port is like talking to the app inside the container.

So when you say that you can't ping your container from the host, it is because Docker does its job good. "Fixing" this breaks the isolation of the container and makes it available to other services that shouldn't have acccess to it.

  • Related