0
votes

I’m looking to run 2 flask containers and 1 Nginx container using one docker compose file but keep getting 404 errors.

Tried following a number of tutorials but still no luck.

Below is my docker compose file along with Nginx.conf and Uwsgi config.

docker-compose:

version: "3.7"

services:
    flask:
        build: ./flask
        container_name: flask
        restart: always
        image: flask
        environment: 
            - APP_NAME=flask
        expose: 
            - 8080
        ports:
            - "8080:8080"

    flask2:
        build: ./flask2
        container_name: flask2
        restart: always
        image: flask2
        environment: 
            - APP_NAME=flask2
        expose: 
            - 8081
        ports: 
            - "8081:8081"

    nginx: 
        build: ./nginx
        image: nginx
        container_name: nginx
        restart: always
        ports:
            - "80:80"
            - "81:81"
        depends_on: 
            - flask
            - flask2

nginx.conf

upstream flask_app1 {
    server flask:8080;
}

upstream flask_app2 {
    server flask2:8081;
}

server {
    listen 80;

    location /test1 {
        uwsgi_pass flask_app1;
    }

    location /test2 {
        uwsgi_pass flask_app2;
    }
}

uwsgi: Both identical except port.

[uwsgi]
wsgi-file = run.py
callable = app
socket = :8081
processes = 4
threads = 2
master = true
chmod-socket = 660
buffer-size = 32768
vacuum = true
die-on-term = true

I’m using docker for windows and I can see in the GUI that the 2 flask apps have ports binded at 8080 and 8081 and nginx being binded to port 80.

Not sure what the else the issue could be. Any help would be fantastic.

Thanks in advance.

1
what url you are asking? localhost/task1 and localhost/task2? And when you ask localhost:8080 or localhost:8081 it works?d0niek
@d0niek looking for url locoalhost/test1 and localhost/test2. I can try adsrees with pots such as localhost:8080 and i get nothing.demonLaMagra
If url localhost:8080 not works then url localhost/task1 won't works tood0niek
Pleae edit your question to add the docker-compose logs.Lyr

1 Answers

0
votes

Although I was not able to get what I ideally wanted by routing each appliaction to a different location I did managed to solve the issue by setting the ports for nginx in the docker-compose file to the ports I wanted to use for each app and then using individual server blocks to route the ports to the ports each app is communicating on.

docker-compose:

version: "3.7"

services:
    flask:
        build: ./flask
        container_name: flask
        restart: always
        image: flask
        environment: 
            - APP_NAME=flask

    flask2:
        build: ./flask2
        container_name: flask2
        restart: always
        image: flask2
        environment: 
            - APP_NAME=flask2

    nginx: 
        build: ./nginx
        image: nginx
        container_name: nginx
        restart: always
        ports:
            - "8001:8001"
            - "8002:8002"
        depends_on: 
            - flask
            - flask2

nginx.conf:

server {
    listen 8001;
    location / {
        uwsgi_pass flask:8001;
    }
}

server {
    listen 8002;
    location / {
        uwsgi_pass flask:8002;
    }
}