Mr. Mars
Mr. Mars

Reputation: 840

Multiple apps (microservices) and one proxy (nginx) docker-compose configuration/architecture

Having the following architecture:

Which of the following options would be the best to deploy in the production environment?

  1. Docker Compose Overriding. Have a docker-compose for each microservice and another docker-compose for the proxy. When the production deployment is done, all the docker-compose would be merged to create only one (with docker-compose -f microservice1/docker-compose.yml -f microservice2/docker-compose.yml -f proxy/docker-compose.yml up. In this way, the proxy container, for example nginx, would have access to microservices to be able to redirect to one or the other depending on the request.
  2. Shared external network. Have a docker-compose for each microservice and another docker-compose for the proxy. First, an external network would have to be created to link the proxy container with microservices.docker network create nginx_network. Then, in each docker-compose file, this network should be referenced in the necessary containers so that the proxy has visibility of the microservices and thus be able to use them in the configuration. An example is in the following link https://stackoverflow.com/a/48081535/6112286.

The first option is simple, but offers little felxibility when configuring many microservices or applications, since all docker-compose of all applications would need to be merged to generate the final configuration. The second option uses networks, which are a fundamental pillar of Docker. On the other hand, you don't need all docker-compose to be merged.

Of these two options, given the scenario of having several microservices and needing a single proxy to configure access, which would be the best? Why?

Tnahks in advance.

Upvotes: 2

Views: 1066

Answers (2)

Marcel Waldvogel
Marcel Waldvogel

Reputation: 432

There is a third approach, for example documented in https://www.bogotobogo.com/DevOps/Docker/Docker-Compose-Nginx-Reverse-Proxy-Multiple-Containers.php and https://github.com/Einsteinish/Docker-compose-Nginx-Reverse-Proxy-II/. The gist of it is to have the proxy join all the other networks. Thus, you can keep the other compose files, possibly from a software distribution, unmodified.

docker-compose.yml

version: '3'
services:
  proxy:
    build: ./
    networks:
      - microservice1
      - microservice2
    ports:
      - 80:80
      - 443:443

networks:
  microservice1:
    external:
      name: microservice1_default
  microservice2:
    external:
      name: microservice2_default

Proxy configuration

The proxy will refer to the hosts by their names microservice1_app_1 and microservice2_app_1, assuming the services are called app in directories microservice1 and microservice2.

Upvotes: 2

breakthewall
breakthewall

Reputation: 183

docker-compose is designed to orchestrate multiple containers in one single file. I do not know the content of your docker-compose files but the right way is to write one single docker-compose.yml that could contains:

version: '3.7'

services:

  microservice1_app:
    image: ...
    volumes: ...
    networks:
      - service1_app
      - service1_db

  microservice1_db:
    image: ...
    volumes: ...
    networks:
      - service1_db

  microservice2_app:
    image: ...
    volumes: ...
    networks:
      - service2_app
      - service2_db

  microservice2_db:
    image: ...
    volumes: ...
    networks:
      - service2_db

  nginx:
    image: ...
    volumes: ...
    networks:
      - default
      - service1_app
      - service2_app

volumes:
...

networks:
  service1_app:
  service1_db:
  service2_app:
  service2_db:
  default:
    name: proxy_frontend
    driver: bridge

In this way nginx container is able to communicate with microservice1_app container through microservice1_app hostname. If other hostnames are needed, it can be configured with aliases subsection within services networks section.

Security Bonus In the above configuration, microservice1_db is only visible by microservice1_app (same for microservice2) and nginx is only able to see microservice1_app and microservice2_app and is reachable from outside of Docker (bridge mode)

Upvotes: 2

Related Questions