How to manage endpoints in docker?

How to manage endpoints in docker?

Want to call two endpoints each inside a docker container using an nginx container

Code example:

./Container1/Dockerfile

FROM python:3.6.10-slim-stretch
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt && apt-get update && apt-get install -y 
wget curl
EXPOSE 5000

COPY . .

CMD [ "python", "./Api.py" ]

./Container2/Dockerfile

FROM python:3.6.10-slim-stretch
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt && apt-get update && apt-get install -y 
wget curl
EXPOSE 5000

COPY . .

CMD [ "python", "./Api.py" ]

docker-compose.yml

version: "3.3"

services:
  rfm:
    build: ./Container1/.
    container_name: rfm_container
    networks:
      - my-network

  recomendation:
    build: ./Container2/.
    container_name: recomendation_container
    networks:
      - my-network

  nginx:
    image: nginx:latest
    volumes:
      - ./default.conf:/etc/nginx/default.conf
    ports:
      - 80:80
      - 433:433

    networks:
      - my-network

    depends_on:
      - rfm
      - recomendation

networks:
  my-network:
    driver: bridge

default.conf

upstream rfm_sever {
  ip_hash;
  server rfm/rfm:5000;
}

upstream recomendation_server {
  ip_hash;
  server recomendation/recomendation:5000;
}


server {

  listen 80;
  server_name rfm.localhost;
  location / {
    proxy_pass http://rfm_sever;
  }

}

server {

  listen 80;
  server_name recomendation.localhost;

  location / {

    proxy_pass http://recomendation_server;

  }

}

Have 2 docker containers that contains my endpoints, each one returns one json response:

  • rfm_container
  • recomendation_container

One container nginx that tries to manage the trafic to each endpoint on every request.
The point of this is have all traffic passing on nginx container that returns the json output of each endpoint.

Expected output

When call to rfm.localhost as it’s shown in default.conf of nginx container, return json of service rfm. And same for recomendation.localhost

As it’s shown in the image we expect that when make post on recomendation.localhost or rfm.localhost returns json response

Expected output when call recomendtion.localhost

Output

Obtain the same output per each request, in this example shows the output of recomendation.localhost.

Output recomendation.localhost request, returns 405 error

Source: StackOverflow