Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reach another container from a dockerised nginx

Tags:

docker

nginx

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.

The nginx docker container is listening to port 80 (will do the certificate later, first this base must be working).

And now I want a subdomain to be forwarded to this 8080 nodejs app. lets say app1.example.com

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).

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.

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?

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....

Edit Just found out, this command of docker might help:

sudo docker network inspect bridge

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)

Edit Following alkaline answer I now have (but still not working):

my docker-compose.yml file:

version: "2"
services:
  nginx:
    container_name: nginx
    image: nginx_img
    build: ../docker-nginx-1/
    ports:
      - "80:80"
    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

and my nginx (skipped the include in the conf.d folder for simplicity):

worker_processes 1;

events { worker_connections 1024; }

http {

  sendfile on;

  upstream upsrv {
      server nodejs:8080;
  }

  server {

    listen 80;
    server_name  app1.example.com;
    
    location / {
        proxy_pass http://upsrv;
    }
    
  }
}

edit 31-08-2016

this might be the problem, the name is not backbone, but called after the folder started the service from:

sudo docker network ls

out puts:

NETWORK ID          NAME                       DRIVER              SCOPE
1167c2b0ec31        bridge                     bridge              local               
d06ffaf26fe2        dockerservices1_backbone   bridge              local               
5e4ec13d790a        host                       host                local               
7d1f8c32f259        none                       null                local 

edit 01-09-2016

It might be caused by the way I have my nginx docker container setup?

this is the docker file I used:

############################################################
# 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 "deb http://archive.ubuntu.com/ubuntu/ raring main     universe"     >> /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 "daemon off;" to the beginning of the configuration
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

# Expose ports
EXPOSE 80

# Set the default command to execute
# when creating a new container
CMD service nginx start

My final solution 1th sept. 2016

I used this compose file now:

version: "2"
services:
    nginx:
      image: nginx
      container_name: nginx
      volumes:
        - ./nginx-configs:/etc/nginx/conf.d
      ports:
        - "80:80"
      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

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

Therefor I copied the default.cfg from the nginx container before I added this volume mount. using the command:

docker exec -t -i container_name /bin/bash

and than cat /etc/nginx/conf.d/default.conf

and added the same default.conf in the project folder with nginx configs.

Besides the default I added app1.conf with this content:

upstream upsrv1 {
    server nodejs:8080;
}

server {

    listen 80;
    server_name  app1.example.com;
    
    
    location / {
        proxy_pass http://upsrv1;
    }
    
}

This way, I can easily add a second app... third and so on.

So the basics is working now.

like image 837
michel.iamit Avatar asked Aug 29 '16 09:08

michel.iamit


People also ask

How do I connect to a nginx container?

Copy the Docker container's Nginx config file to your local file system. Add proxy_pass entries that point to your backend origin servers. Copy the config file back into the Nginx Docker container. Reload the Nginx configuration and test the setup.

How multiple containers communicate with each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.


1 Answers

Here's a best practice. Only expose port 80 outside of the host. The nodejs app can be in a private network only accessible through nginx.

version: "2"
services:
    nginx:
      ...
      ports:
        - "80:80"
      networks:
        - backbone
    nodejs:
      ...
      networks:
        - backbone
      expose: 
        - 8080

networks:
  backbone:
    driver: bridge

In your nginx.conf file, the upstream servers can be listed as nodejs:8080. The docker daemon will resolve it to the correct internal ip.

like image 154
Bernard Avatar answered Sep 21 '22 05:09

Bernard