Darragh O'Flaherty
Darragh O'Flaherty

Reputation: 1005

Docker Cannot Resolve Hostname

I am trying to link my containerized VueJS frontend with my containerized Spring Boot API backend, with great difficulty.

Whenever I try to make an HTTP request to my API using the container name, I get the following.

OPTIONS http://api:4505/user/sign-in net::ERR_NAME_NOT_RESOLVED

Here is my docker-compose file:

version: "3"

services:
  mongodb:
    image: mongo
    container_name: mongo
    ports:
      - "27017:27017"
  api:
    image: registry.gitlab.com/darragh.oflah/api:latest
    container_name: api
    ports:
      - "4505:4505"
    links:
      - mongodb
  web:
    image: registry.gitlab.com/darragh.oflah/web:latest
    container_name: web
    ports:
      - "80:8080"
    links:
      - api

Here what I get when I run sudo docker network inspect tmp_default

So it would seem that the network is set up correctly

[
    {
        "Name": "tmp_default",
        "Id": "75ab7c89cb5a80aa7eddd7c5a3f7f4aafb911cfe96a923ad3db2219552366fd7",
        "Created": "2020-02-04T16:55:49.131485109Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "6ae3db08be2ed22245a173a677ae1b0f28eca878aa84e43744a320589cbda5af": {
                "Name": "mongo",
                "EndpointID": "b399bb72f28b6d47a93927712a665dcc725d27a6ba2ee432e715db00c9cbc835",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "fa7e4066e436181ce2991e048790f8de518af31fb97cf9351316ff8f41824449": {
                "Name": "api",
                "EndpointID": "bf8d071683bfa4ecbd215f3dd534d0e278702ed4377552ef242e5c65b01c3fa1",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "fb544ef5389afd74d53f45d6de968008632b65340f161527a9c7aa4214aa7674": {
                "Name": "web",
                "EndpointID": "2dc3e8a452c241916a2e9f7e25b33ee7997fe2d25ec3543e5d34e888e50d905c",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "tmp",
            "com.docker.compose.version": "1.21.2"
        }
    }
]

In the net work tab, the request is saying failed, which would indicate to me that the request is failing to leave the container at all.

enter image description here

Upvotes: 1

Views: 3037

Answers (1)

Ben Lorantfy
Ben Lorantfy

Reputation: 963

Your compose file creates a docker network. You're running "web" in a container on that network. All containers in the docker network can access other containers via the hostname. However, the browser is running on your host machine. Your host machine is not in the docker network. Therefore your browser won't be able to access the api container via the hostname. If you type http://api:4505/user/sign-in into the browser url bar, it's doing the same thing, and you'll also get an error.

Upvotes: 2

Related Questions