'Two apps with different port in single docker-compose and nginx reverse proxy
I have an app running with app.dev domain in my localhost. However, I would want to have an additional app running with a different port. Instead of port 80, it would be 2000 with domain app.dev:2000 at app2. What changes should I make to my docker-compose and nginx?
This is my docker-compose:
version: "2"
volumes:
mongostorage:
services:
app:
build: ./app
ports:
- "3000"
links:
- mongo
- redis
command: node ./bin/www
app2:
build: ./app2
ports:
- "2000"
links:
- mongo
- redis
command: node app.js
nginx:
build: ./nginx
ports:
- "80:80"
links:
- app:app
mongo:
image: mongo:latest
environment:
- MONGO_DATA_DIR=/data/db
volumes:
- mongostorage:/data/db
ports:
- "27017:27017"
redis:
image: redis
volumes:
- ./data/redis/db:/data/db
ports:
- "6379:6379"
This is my nginx.conf
events {
worker_connections 1024;
}
http{
upstream app.dev{
least_conn;
server app:3000 weight=10 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name app.dev;
location / {
proxy_pass http://app.dev;
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;
}
}
}
Solution 1:[1]
I think just changing the ports of app2 in the following way should be enough, as nginx only listens to port 80.
app2:
build: ./app2
ports:
- "2000:2000"
links:
- mongo
- redis
command: node app.js
Solution 2:[2]
The simplest way is to expose app2 on port 2000
app2:
build: ./app2
ports:
- '2000:2000'
The other is to route the connection via nginx, to be consistent with the first app.
Compose
nginx:
build: ./nginx
ports:
- '80:80'
- '2000:2000'
Nginx
http{
upstream app.dev{
least_conn;
server app:3000 weight=10 max_fails=3 fail_timeout=30s;
}
upstream app2.dev{
least_conn;
server app2:2000 weight=10 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name app.dev;
location / {
proxy_pass http://app.dev;
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;
}
}
server {
listen 2000;
server_name app.dev;
location / {
proxy_pass http://app2.dev/;
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;
}
}
}
By the way you don't need any of the links
in a version 2+ compose file. You already have access to all the services via the service name.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | samprog |
Solution 2 | Matt |