Mervin Hemaraju
Mervin Hemaraju

Reputation: 2117

Host multiple web apps on NGINX Docker

I want to host multiple Flask apps on my docker nginx image. I want each app to listen on a different port.

However, i am unable to do so.

nginx.conf

server {
    listen 80;
    location / {
       include uwsgi_params;
       uwsgi_pass flask1:8080;
    }
}

server {
    listen 81;
    location / {
       include uwsgi_params;
       uwsgi_pass flask2:8081;
    }
}

docker-compose.yml

version: "3.7"

services:
  flask1:
    build: ./flask1
    container_name: flask1
    restart: always
    environment:
      - APP_NAME=MyFlaskNginxDockerApp
    expose:
      - 8080

  flask2:
    build: ./flask2
    container_name: flask2
    restart: always
    environment:
      - APP_NAME=MyFlaskNginxDockerApp
    expose:
      - 8081

  nginx:
    build: ./nginx
    container_name: nginx
    restart: always
    ports:
      - "8080:80"
      - "8081:81"

nginx - Dockerfile

# Use the Nginx image
FROM nginx

# Remove the default nginx.conf
RUN rm /etc/nginx/conf.d/default.conf

# Replace with our own nginx.conf
COPY nginx.conf /etc/nginx/conf.d/

When I built and run this docker-compose, my websites are not available.

I want flask1 to be accessible via localhost:8080 and flask 2 to be accessible via localhost:8081

Can someone please help point out what I did wrong ?

Upvotes: 0

Views: 870

Answers (2)

CantankerousBullMoose
CantankerousBullMoose

Reputation: 512

What you've set up right now is that external clients can connect to your flask apps on :81 and :82. Other containers like nginx can connect on flask1:80 and flask2:80.

Nginx could also be set up with host network mode to go back out and connect to :81 and :82, but that's probably not what you want. In fact, my guess would be that exposing external ports on the flask apps at all is probably not what you want to do in the long term, although it can be helpful for debugging because it gives you a way bypass the proxy.

Oops, forgot to add, you need to set up Nginx to use docker's internal dns to resolve the service names to IPs as mentioned here: https://stackoverflow.com/a/37656784/9194976

Upvotes: 0

Pratik Thanki
Pratik Thanki

Reputation: 252

You should not be using the service name, instead use host.docker.internal which resolves request to the host. Make this change in your nginx.conf

I would suggest using docker networks instead..

Upvotes: 1

Related Questions