Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker nginx multiple apps on one host

I'm confused by how I should manage reverse proxy (nginx) with multiple independent webapps on the same host. I know that i can use https://github.com/jwilder/nginx-proxy and configure VIRTUAL_HOST per app but then I wont be able to have nginx visible as a service in every app docker-compose.yml.

I want to do it this way because I want to clearly define all services needed to run app in production and replicate it easy in development.

In other words: I have two webapps that I need to run on the same host and I want to define nginx as a service dependency in docker-compose.yml in both apps but share that service with both as only one nginx can forward port 80.

like image 321
Axon Avatar asked Mar 26 '18 10:03

Axon


People also ask

Can we run multiple apps on one server with Docker?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.

Where is NGINX config in Docker?

Maintaining Content and Configuration Files on the Docker Host. Any change made to the files in the local directories /var/www and /var/nginx/conf on the Docker host are reflected in the directories /usr/share/nginx/html and /etc/nginx in the container.


2 Answers

Dockerfile:

FROM ubuntu:14.04
MAINTAINER Test ([email protected])
# install nginx
RUN apt-get update -y
RUN apt-get install -y python-software-properties
RUN apt-get install -y software-properties-common
RUN add-apt-repository -y ppa:nginx/stable
RUN apt-get update -y
RUN apt-get install -y nginx
# deamon mode off
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN chown -R www-data:www-data /var/lib/nginx
# volume
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/var/log/nginx"]
# expose ports
EXPOSE 80 443
# add nginx conf
ADD nginx.conf /etc/nginx/conf.d/default.conf
WORKDIR /etc/nginx
CMD ["nginx"]

nginx.conf:

server {
    listen          80;
    server_name     test1.com www.test1.com;
    location / {
        proxy_pass  http://web1:81/;
    }
}
server {
    listen          80;
    server_name     test2.com www.test2.com;
    location / {
        proxy_pass  http://web1:82/;
    }
}

** where web1 and web2 - container names

docker-compose.yml:

version: "2"
services:
    web1:
        image: your_image
        container_name: web1
        ports:
            - 81:80
    web2:
        image: your_image
        container_name: web2
        ports:
            - 82:80
    nginx:
        build: .
        container_name: nginx
        ports:
            - 80:80
            - 443:443
        links:
            - web1
            - web2

How to run:

docker-compose up -d

When you call test1.com - nginx forwards your request to container web1:81, when test2.com - to container web2:82

P.S.: your question was about NGINX-reverse-proxy. But better and easier do this with TRAEFIK https://traefik.io

like image 118
Oleh Vasylyev Avatar answered Sep 20 '22 11:09

Oleh Vasylyev


You should also be able to open two ports in the same container

services:
web:
    image: your_image
    container_name: web
    ports:
        - 8080:80
        - 8081:81

And the add new config file for second app in nginx site-enabled (or conf.d) that will listen 81 port.

First App

server {
listen 80;
server_name localhost;
root /app/first;
}

Second App

server {
listen 81;
server_name localhost;
root /app/second;
}

So that:

  • first app access on localhost:8080
  • second app access on localhost:8081
like image 31
Deki Jovic Avatar answered Sep 20 '22 11:09

Deki Jovic