0

I am trying to get angular and nginx containers in docker-compose to speak to each other on a google-compute vm instance (Debian OS), without success. Here is my docker-compose.yml:

version: '3'
services:
  angular:
    container_name: angular
    hostname: angular
    build: project-frontend
    ports:
      - "80:80"
        #network_mode: host
  nodejs:
    container_name: nodejs
    hostname: nodejs
    build: project-backend
    ports:
      - "8080:8080"
        # network_mode: host

I have read the docs and numerous SO posts such as this, and understand that angular should be trying to find node at http://nodejs:8080/, but I'm getting:

POST http://nodejs:8080/login/ net::ERR_NAME_NOT_RESOLVED

When I do docker networkk inspect I see this

[
    {
        "Name": "project_default",
        "Id": "2d1665ce09f712457e706b83f4ae1139a846f9ce26163e07ee7e5357d4b28cd3",
        "Created": "2020-05-22T11:25:22.441164515Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.28.0.0/16",
                    "Gateway": "172.28.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "b0fceb913ef14b0b867ae01ce4852ad4a0827c06194102082c0d4b18d7b80464": {
                "Name": "angular",
                "EndpointID": "83fba04c3cf6f7af743cae87116730805d030040f286706029da1c7a687b199c",
                "MacAddress": "02:42:ac:1c:00:03",
                "IPv4Address": "172.28.0.3/16",
                "IPv6Address": ""
            },
            "c181cd4b0e9ccdd793c4e1fc49067ef4880cda91228a10b900899470cdd1a138": {
                "Name": "nodejs",
                "EndpointID": "6da8ad2a83e2809f68c310d8f34e3feb2f4c19b40f701b3b00b8fb9e6f231906",
                "MacAddress": "02:42:ac:1c:00:02",
                "IPv4Address": "172.28.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }

I'm not sure what other steps can help me to debug this. Thanks.

EDIT: Thanks to this post I tried to ping nodejs container through the angular container successfully:

$ sudo docker exec -it angular ping nodejs
PING nodejs (172.28.0.2): 56 data bytes
64 bytes from 172.28.0.2: seq=0 ttl=64 time=0.079 ms
64 bytes from 172.28.0.2: seq=1 ttl=64 time=0.105 ms

I also tried tested the port on the nodejs constainer and it seems to be there:

$ sudo docker port nodejs
8080/tcp -> 0.0.0.0:8080

EDIT: I'm starting to think this is a google compute VM question as I have it running on my local linux box without any problem...have updated the title accordingly

Kissenger
  • 345
  • 4
  • 15
  • Your angular should connect to the server IP address/URL not nodejs, if you are running the app on localhost, then its `http://localhost:8080/login/` if its a remote server, it should be `http://REMOTE_SERVER_IP:8080/login/` or `http://REMOTE_SERVER_URL:8080/login`. – ROOT May 22 '20 at 10:56
  • I originally tried with localhost, but it didnt work so I sought other solutions. Just tried it with the full ip, no luck. I should note that I have had this working as seperate docker containers (using localhost) but not using docker-compose. – Kissenger May 22 '20 at 11:15
  • you mentioned that its cloud compute, do they have port block policy maybe you have to open port `8080` – ROOT May 22 '20 at 11:48
  • 1
    I opened the port and it did not change the behaviour... – Kissenger May 22 '20 at 12:13
  • one last thing I'm thinking of, did you change angular to point to the container host ip address and then built the image again (since its on cloud service)? just want to make sure that you are building and testing the latest changes. – ROOT May 22 '20 at 12:17
  • Yes, each time I change the pointer in the source, I am running docker-compose build before docker-compose up ... does that answer your question? – Kissenger May 22 '20 at 12:21
  • 1
    @ROOT It works - I needed to combine the `http://REMOTE_SERVER_IP:8080/login/` with opening the port... sorry I didn't connect the dots between your two comments and had changed to try another approach. HOWEVER, while it works, I dont understand why the containers cannot connect to each other locally - they are on the same machine and the docs [link](https://docs.docker.com/compose/networking/) say that each service is reachable `at a hostname identical to the container name`. Any ideas why this approach doesnt work? – Kissenger May 22 '20 at 12:42
  • glad it worked for you now, basically its the browser that need to know through javascript where to make the request, its not about the containers not able to connect to each other. – ROOT May 22 '20 at 12:49
  • of course it is, thanks for putting me straight. – Kissenger May 22 '20 at 13:34

2 Answers2

0

You need to make sure they are on the same network. You can do it by adding the following lines in your compose file at the end

networks:
  default:
    external:
      name: project.local

Note, you have to create project.local network. When you run docker-compose up it'll tell you how to do it.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
0

As @ShipluMokaddim says, containers must be in the same network ort hey can't hear each other, what I recommended is create a new network:

version: '3'
services:
   angular:
      container_name: angular
      build: project-frontend
      ports:
        - "80:80"
      networks:
         - mynetwork
   nodejs:
     container_name: nodejs
     build: project-backend
     ports:
       - "8080:8080"
     networks:
       - mynetwork

networks:
   mynetwork:

Whit this you will be fine.

Schwarz54
  • 964
  • 1
  • 9
  • 18