mwilson
mwilson

Reputation: 12900

API Communication Between 2 Containers

I am using docker-compose in two separate API Projects in .NET Core 3.1

ContainerAuth has an endpoint I need to hit from ContainerAppApi

Endpoint on ContainerAuth

POST: http://locahost:5000/auth/validate

I have the ContainerAppApi running in a separate docker container (entirely different repo/project/etc..). That is running on http://localhost:5100

Issue When making a request from ContainerAppApi -> ContainerAuth I receive the below exception:

Cannot assign requested address Cannot assign requested address

From what I've read, the issue is that ContainerAppApi doesn't know about ContainerAuth's Api running on http://localhost:5000

I thought I could establish a bridge network, but I still get the same error. I have verified both containers are connected to my bridge network, so, am I missing a port binding of some sort? Or is there some extra step within the networking process that I'm missing?

docker-compose for ContainerAuth

version: '3.4'
services:
  web:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
    build:
      context: ..
      dockerfile: Dockerfile
    ports:
      - 5000:80
      - 5001:443
    networks:
      - my-network
networks:
  my-network:
    driver: bridge
    external: true

docker-compose for ContainerAppApi

version: '3.4'
services:
  web:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
    build:
      context: ..
      dockerfile: Dockerfile
    ports:
      - 5100:80
      - 5101:443
    networks:
      - my-network
networks:
  my-network:
    driver: bridge
    external: true

docker network ls

NETWORK ID          NAME                DRIVER              SCOPE
23179718cd0a        bridge              bridge              local
431eba987024        host                host                local
733c83d6dee2        my-network          bridge              local
2bf6118b71a4        none                null                local

docker network inspect my-network

[
    {
        "Name": "my-network",
        "Id": "733c83d6dee2d8ffb007bdb99324e9312a07d5735d329059a2083a11c1582642",
        "Created": "2020-07-11T02:28:09.2078715Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.21.0.0/16",
                    "Gateway": "172.21.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "741624d9304add4d39bc0dc30b04af2fc26a36e87565d87ac5eca44b2a2e361e": {
                "Name": "ContainerAppApi_web_1",
                "EndpointID": "623a99e45344ef52c8352fa1a35a6f02c8417107d85bccae6a06289c73b0d4c5",
                "MacAddress": "02:42:ac:15:00:03",
                "IPv4Address": "172.21.0.3/16",
                "IPv6Address": ""
            },
            "74c08686a62f6028a74796c41f4b2fcfe9e6818588783c3ab0e76665acd032d7": {
                "Name": "ContainerAuth_web_1",
                "EndpointID": "6bb7a30f2d231514a3569481de166d46c30c78b70b725e5ee595d027dab1639f",
                "MacAddress": "02:42:ac:15:00:02",
                "IPv4Address": "172.21.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

It looks like everything is connected, but what can't ContainerAppApi call ContainerAuth's API? Both are up and running and I can hit each of the api's with Postman.

Upvotes: 1

Views: 2208

Answers (1)

Adiii
Adiii

Reputation: 59916

If both docker-compose project using same network then you can call ContainerAuth's service by service name instead of localhost, localhost mean this container means ContainerAppApi container.

You will need little changes to make it working, replace the service name and use internal PORT that is 80 and 443. publish port is for outer world communication.

API

version: '3.4'
services:
  api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
    build:
      context: ..
      dockerfile: Dockerfile

Auth

version: '3.4'
services:
  auth:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
    build:
      context: ..
      dockerfile: Dockerfile

Now you can access auth from API container using its service name

curl api

Here is the simples example for your understand

nging service

version: '3.4'
services:
  nginx:
    image: nginx:alpine
    networks:
      - my-network
networks:
  my-network:
    driver: bridge
    external: true

Alpine service

version: '3.4'
services:
  web:
    image: node:alpine
    command: sh -c "apk add --no-cache curl && curl nginx"
    networks:
      - my-network
networks:
  my-network:
    driver: bridge
    external: true

Once you up Nginx and then up Alpine you will able to see

curl nginx

This call seems working.

Upvotes: 2

Related Questions