skip to Main Content

I have one node.js application (web-app) and two lumen applications (api, customer-api) that are load balanced by an nginx container listening on port 80.

My docker-compose.yml file:

version: '2'
services:
  nginx:
    build:
      context: ../
      dockerfile: posbytz-docker/nginx/dockerfile
    volumes:
      - api
      - customer-api
    ports:
      - "80:80"
    networks:
      - network
    depends_on:
      - web-app
      - api
      - customer-api
  web-app:
    build:
      context: ../
      dockerfile: posbytz-docker/web-app-dockerfile
    volumes:
      - ../web-app:/posbytz/web-app
      - /posbytz/web-app/node_modules
    ports:
      - "3004:3004"
    networks:
      - network
  api:
    build:
      context: ../
      dockerfile: posbytz-docker/api-dockerfile
    volumes:
      - ../api:/var/www/api
    networks:
      - network
  customer-api:
    build:
      context: ../
      dockerfile: posbytz-docker/customer-api-dockerfile
    volumes:
      - ../customer-api:/var/www/customer-api
    networks:
      - network
  redis:
    image: redis
    ports:
      - "6379:6379"
    networks:
      - network
  memcached:
    image: memcached
    ports:
      - "11211:11211"
    networks:
      - network
  mysql:
    image: mysql:5.7
    volumes:
      - ./db-data:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - network
  adminer:
    image: adminer
    restart: always
    ports:
      - "9001:8080"
    networks:
      - network
networks:
  network:
    driver: bridge

Since I am using a bridged network, I am able to access each container from another container using the container names. But what I want instead is, access the containers using the server_name of their nginx configuation.

Below are the nginx configuration of each application,

web-app.conf:

server {
  listen 80;
  server_name posbytz.local;
    resolver 127.0.0.11 valid=10s;

  location / {
    proxy_pass http://web-app:3004;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

api.conf:

server {
  listen 80;
  index index.php index.html;
  root /var/www/api/public;
  server_name api.posbytz.local;
    resolver 127.0.0.11 valid=10s;

  location / {
    try_files $uri /index.php?$args;
  }

  location ~ .php$ {
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    fastcgi_pass api:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

customer-api.conf

server {
  listen 80;
  index index.php index.html;
  root /var/www/customer-api/public;
  server_name customer-api.posbytz.local;
    resolver 127.0.0.11 valid=10s;

  location / {
    try_files $uri /index.php?$args;
  }

  location ~ .php$ {
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    fastcgi_pass customer-api:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

The problem

I want to access both api and customer-api containers from web-app container. The problem is when I try curl http://nginx I’am only getting response from the api container. Is there any way to access the customer-api container through the nginx container?

What I tried

When I manually mapped the IP of nginx container (172.21.0.9) with their respective server_name in the /etc/hosts file on the web-app container it seems to work.

What I added on /etc/hosts file on web-app container:

172.21.0.9  api.posbytz.local
172.21.0.9  customer-api.posbytz.local

Is there any other way to achieve this without manual intervention?

2

Answers


  1. Chosen as BEST ANSWER

    Finally made it to work by changing the nginx configuration on customer-api.conf to listen on port 81 ie. listen 80; to listen 81;. Now http://nginx resolves to http://api:9000 and http://nginx:81 resolves to http://customer-api:9000


  2. You can use aliases:

    networks:
      some-network:
        aliases:
          - api.posbytz.local
          - customer-api.posbytz.local
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search