title: Docker - Access nginx container using nginx server_name from another container
body: <p>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.</p>
  
  <p>My docker-compose.yml file:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>Below are the nginx configuration of each application,</p>
  
  <p>web-app.conf:</p>
  
  <pre><code>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;
    }
  }
  </code></pre>
  
  <p>api.conf:</p>
  
  <pre><code>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;
    }
  }
  </code></pre>
  
  <p>customer-api.conf</p>
  
  <pre><code>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;
    }
  }
  </code></pre>
  
  <h1>The problem</h1>
  
  <p>I want to access both api and customer-api containers from web-app container. The problem is when I try <code>curl http://nginx</code> I'am only getting response from the api container. Is there any way to access the customer-api container through the nginx container?</p>
  
  <h2>What I tried</h2>
  
  <p>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. </p>
  
  <p>What I added on /etc/hosts file on web-app container:</p>
  
  <pre><code>172.21.0.9  api.posbytz.local
  172.21.0.9  customer-api.posbytz.local
  </code></pre>
  
  <p>Is there any other way to achieve this without manual intervention?</p>