docker compose communication with container

1.8k views Asked by At

I am trying to create an example to create two WEB API 's and containerize them and to communicate between them. I would like to see the side car design pattern, I have found an example in Github that I am trying to run.

https://github.com/cesaroll/dotnet-sidecar

In the above example, HelloAPI makes a call to HelloSideCar API which is a different project. In the HelloAPI a call is made to another API in another project. I am trying to run in local using Docker Compose.

When I try to hit the API from HelloAPI(localhost:8080/FromSidecar) project to SideCarAPI, I see a 404 error, request is not going to another container Below Is my Docker Compose

# docker-compose up -d
# docker-compose stop
# docker-compose rm -f

version: '3.8'
services:
  hello-sidecar-api:
    image: hello-sidecar-api:latest
    container_name: hello-sidecar-api
    ports:
      - "8180:8080"

  hello-api:
    image: helloapi:latest
    container_name: hello-api
    environment:
      - SIDERCAR_URL=http://localhost:8180/
    depends_on:
      - hello-sidecar-api
    ports:
      - "8080:8080"
1

There are 1 answers

0
Michal Rosenbaum On

You can either change SIDECAR_URL to http://hello-sidecar-api:8080/ or place both APIs in the same container. Note that I also changed the port from 8180 to 8080, because 8180 is a port mapped on the host, but inside your Docker network your API is accessible by other containers on 8080.

Your containers are separate network entities with their own IPs, so when you call http://localhost:8180 from the inside of a container, you're not calling the host, but the same container from which the request originates. (assuming you're not using host network driver).

What you are trying to do here resembles the behavior of pods in Kubernetes (where sidecar term is widely used). In Kubernetes you could put these two containers in one pod and then they could call each other on localhost