how to reach another container from a dockerised nginx
<p>I have nginx in a docker container, and a nodejs webapp in another docker container.
The nodejs server is reachable from the host server on port 8080.</p>
<p>The nginx docker container is listening to port 80 (will do the certificate later, first this base must be working).</p>
<p>And now I want a subdomain to be forwarded to this 8080 nodejs app. lets say app1.example.com</p>
<p>From outside I can reach the app by the server ip (or hostname) and port 8080 but not on app1.example.com. And it does work on app1.example.com:8080 (I have opened up port 8080 on the host server).</p>
<p>I get a bad gateway nginx message when approaching the app1.example.com So I get in the first nginx container, but how do i get back to the host server to proxy pass it to the port 8080 of the host server (and not port 8080 of the nginx container). looking for the reverse EXPOSE syntax.</p>
<p>the main problem is, of course if I use the ip and port 127.0.0.1:8080 it will try on the nginx container....
So how do I let the nginx container route back to the host 127.0.0.1:8080?</p>
<p>I have tried 0.0.0.0 and defining an upstream, actually been googling a lot, and have tried a lot of configurations... but not yet found a working one....</p>
<p><strong>Edit</strong>
Just found out, this command of docker might help:</p>
<pre><code>sudo docker network inspect bridge
</code></pre>
<p>This shows the Ip address used inside the containers (in my case 172.17..0.2), but not sure this address stays the same every time the docker will restart... (e.g. server reboot)</p>
<p><strong>Edit</strong>
Following alkaline answer I now have (but still not working):</p>
<p>my docker-compose.yml file:</p>
<pre><code>version: &quot;2&quot;
services:
  nginx:
    container_name: nginx
    image: nginx_img
    build: ../docker-nginx-1/
    ports:
      - &quot;80:80&quot;
    networks:
      - backbone
  nodejs:
    container_name: nodejs
    image: merites/docker-simple-node-server
    build: ../docker-simple-node-server/
    networks:
    - backbone
    expose: 
    - 8080
  networks:
    backbone:
      driver: bridge
</code></pre>
<p>and my nginx (skipped the include in the conf.d folder for simplicity):</p>
<p>worker_processes 1;</p>
<p>events { worker_connections 1024; }</p>
<pre><code>http {

  sendfile on;

  upstream upsrv {
      server nodejs:8080;
  }

  server {

    listen 80;
    server_name  app1.example.com;
    
    location / {
        proxy_pass http://upsrv;
    }
    
  }
}
</code></pre>
<p><strong>edit 31-08-2016</strong></p>
<p>this might be the problem, the name is not backbone, but called after the folder started the service from:</p>
<pre><code>sudo docker network ls
</code></pre>
<p>out puts:</p>
<pre><code>NETWORK ID          NAME                       DRIVER              SCOPE
1167c2b0ec31        bridge                     bridge              local               
d06ffaf26fe2        dockerservices1_backbone   bridge              local               
5e4ec13d790a        host                       host                local               
7d1f8c32f259        none                       null                local 
</code></pre>
<p><strong>edit 01-09-2016</strong></p>
<p>It might be caused by the way I have my nginx docker container setup?</p>
<p>this is the docker file I used:</p>
<pre><code>############################################################
# Dockerfile to build Nginx Installed Containers
# Based on Ubuntu
############################################################

# Set the base image to Ubuntu
FROM ubuntu

# File Author / Maintainer
MAINTAINER Maintaner Name

# Install Nginx

# Add application repository URL to the default sources
# RUN echo &quot;deb http://archive.ubuntu.com/ubuntu/ raring main     universe&quot;     &gt;&gt; /etc/apt/sources.list

# Update the repository
RUN apt-get update

# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools

# Download and Install Nginx
RUN apt-get install -y nginx  

# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf

# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/

# Append &quot;daemon off;&quot; to the beginning of the configuration
RUN echo &quot;daemon off;&quot; &gt;&gt; /etc/nginx/nginx.conf

# Expose ports
EXPOSE 80

# Set the default command to execute
# when creating a new container
CMD service nginx start
</code></pre>
<p><strong>My final solution 1th sept. 2016</strong></p>
<p>I used this compose file now:</p>
<pre><code>version: &quot;2&quot;
services:
    nginx:
      image: nginx
      container_name: nginx
      volumes:
        - ./nginx-configs:/etc/nginx/conf.d
      ports:
        - &quot;80:80&quot;
      networks:
        - backbone
    nodejs:
      container_name: nodejs
      image: merites/docker-simple-node-server
      build: ../docker-simple-node-server/
      networks:
        - backbone
      expose: 
        - 8080
networks:
  backbone:
    driver: bridge
</code></pre>
<p>In the project folder, from which you run docker-compose up -d, I added a folder named nginx-configs. This folder will 'override' all the files in the nginx container named /etc/nginx/conf.d</p>
<p>Therefor I copied the default.cfg from the nginx container before I added this volume mount. using the command:</p>
<p>docker exec -t -i container_name /bin/bash</p>
<p>and than cat /etc/nginx/conf.d/default.conf</p>
<p>and added the same default.conf in the project folder with nginx configs.</p>
<p>Besides the default I added app1.conf with this content:</p>
<pre><code>upstream upsrv1 {
    server nodejs:8080;
}

server {

    listen 80;
    server_name  app1.example.com;
    
    
    location / {
        proxy_pass http://upsrv1;
    }
    
}
</code></pre>
<p>This way, I can easily add a second app... third and so on.</p>
<p>So the basics is working now.</p>