Home > OS >  Setting up NGINX as reverse proxy for multiple containerized services
Setting up NGINX as reverse proxy for multiple containerized services

Time:07-17

I developed a web app with vue and django however, I'm having problems deploying it. I added another container to serve as reverse proxy so only port 80 would be exposed and when I finish struggling with this also port 443. I could not find exact anwser how to do it so I hope someone here will be kind enoug to give me some clues.

Here is the conf for the nginx. The error I'm getting is on ui container. 2022/07/14 09:09:00 [emerg] 1#1: bind() to 0.0.0.0:8080 failed (98: Address already in use)

I looked it up of course, but it was always some different scenario.

BR and thanks in advance

server {
    listen  0.0.0.0:80;
    listen [::]:80;

    location / {
                    proxy_pass http://0.0.0.0:3000;
    }
    location /predict {
                    proxy_pass http://0.0.0.0:5000/predict;
    }
    location /aggregate {
                    proxy_pass http://0.0.0.0:5000/aggregate;
    }
    location /media/pictures {
                    proxy_pass http://0.0.0.0:5000/media/pictures;
    }

    access_log /opt/bitnami/nginx/logs/anomaly_access.log;
    error_log /opt/bitnami/nginx/logs/anomaly_error.log;
}

My docker-compose looks as follows.
version: '3.2'
services:
    se-kpi-sim:
        image: test-app:0.0.1
        network_mode: "host"
        restart: unless-stopped
        environment:
            MODEL_NAME: "model_final.pickle.dat"

    se-kpi-sim-ui:
        image: test-ui:0.0.3
        network_mode: "host"
        restart: unless-stopped

    reverse-proxy:
        image: test-proxy:0.0.7
        network_mode: "host"
        restart: unless-stopped

    database:
        image: postgres
        environment:
            POSTGRES_PASSWORD: password
            POSTGRES_USER: kpi_sim_user
            POSTGRES_DB: kpi_sim
            POSTGRES_HOST_AUTH_METHOD: trust
        ports:
            - 5432:5432
        volumes:
            - database:/var/lib/postgresql/data
        restart: unless-stopped

volumes:
    database:

CodePudding user response:

You can run containers on docker internal network and docker-compose by default creates an network for inter communication of containers. One can modify the port to expose the application to host. while you are trying to run most of the app on host network, there might be two application trying to use the same port (like port 8080 [in this case]), one port can only be used by one application in an OS . Please look at the below snippet for more information to solve this issue.

[port mapping <port on HOST>:<container port where app is exposed inside container>]

version: '3.2'
services:
    se-kpi-sim:
        image: test-app:0.0.1
        ports:
          - 5000:8080
        restart: unless-stopped
        environment:
            MODEL_NAME: "model_final.pickle.dat"

    se-kpi-sim-ui:
        image: test-ui:0.0.3
        ports:
          - 3000:8080
        restart: unless-stopped

    reverse-proxy:
        image: test-proxy:0.0.7
        ports:
          - 80:80
        # this volume mount if you are using bitnami/nginx image
        volumes:
          - /path/to/my_server_block.conf:/opt/bitnami/nginx/conf/server_blocks/my_server_block.conf:ro

        restart: unless-stopped

    database:
        image: postgres
        environment:
            POSTGRES_PASSWORD: password
            POSTGRES_USER: kpi_sim_user
            POSTGRES_DB: kpi_sim
            POSTGRES_HOST_AUTH_METHOD: trust
        ports:
            - 5432:5432
        volumes:
            - database:/var/lib/postgresql/data
        restart: unless-stopped

volumes:
    database:

One have to specify either the IP address or DNS name of the application , in order to forward the traffic to specific application. docker-compose create Domain name for all the services defined in docker-compose.yaml file.

server {
    listen  0.0.0.0:80;
    listen [::]:80;

    location / {
                    proxy_pass http://se-kpi-sim-ui:8080;
    }
    location /predict {
                    proxy_pass http://se-kpi-sim:8080/predict;
    }
    location /aggregate {
                    proxy_pass http://se-kpi-sim:8080/aggregate;
    }
    location /media/pictures {
                    proxy_pass http://se-kpi-sim:8080/media/pictures;
    }

    access_log /opt/bitnami/nginx/logs/anomaly_access.log;
    error_log /opt/bitnami/nginx/logs/anomaly_error.log;
}

One can mount the nginx.conf like:[in bitnami/nginx image]

...
volumes:
      - /path/to/my_server_block.conf:/opt/bitnami/nginx/conf/server_blocks/my_server_block.conf:ro
...

Note: all the above is an example for reference to solve the problem. entrypoint for containers might change according to one's requirements.

  • Related