Docker nginx multiple apps on one host
Dockerfile:
FROM ubuntu:14.04MAINTAINER Test (test@example.com)# install nginxRUN apt-get update -yRUN apt-get install -y python-software-propertiesRUN apt-get install -y software-properties-commonRUN add-apt-repository -y ppa:nginx/stableRUN apt-get update -yRUN apt-get install -y nginx# deamon mode offRUN echo "\ndaemon off;" >> /etc/nginx/nginx.confRUN chown -R www-data:www-data /var/lib/nginx# volumeVOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/var/log/nginx"]# expose portsEXPOSE 80 443# add nginx confADD nginx.conf /etc/nginx/conf.d/default.confWORKDIR /etc/nginxCMD ["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
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