Search code examples
network-programmingdockermicroservicesservice-discovery

From local development to dockerized microservices


I am getting started with dockerized micro services.

A couple of them are running (see the code snippet below). In my local test setup, each service was available at localhost:somePort. E.g. the frontend would try to connect to the backend API at localhost:backend1. Now in the containerized world this does not work anymore. Am I supposed to run a full fledged service discovery solution like consul or etcd? Or should I just try to access the backend from the frontend using docker's naming service? https://docs.docker.com/compose/networking/

version: '2'
services:
  service1-backend:
    image: service1:0.0.1
    links:
      - service1-frontend
  service1-frontend:
      image: service1-frontend:0.0.1
      links:
            - service2-frontend
      ports:
              - "8080:80"
  service2-backend:
      image: service2-backend:0.0.1
      links:
            - service1-backend
            - service2-frontend
            - service3
  service2-frontend:
      image: service2-frontend:0.0.1
      ports:
              - "8081:80"
  service3:
      image: service3:0.0.1

Solution

  • If service1-frontend needs to access service1-backend via name:port, you need:

    1. service1-backend needs to have a port expose specified in its Dockerfile
    2. service1-frontend needs to have a link to service1-backend in the compose file.

    Then in the frontend, you can access backend simply use service1-backend:port.