2

Hi i am trying to create a python app and redis with nginx reverse-proxy but when i run the docker-compose file it runs all the container but when I go to localhost:90 it shows nginx page, not the page defined in python file. I was following this tutorial here

Docker file:

FROM python:3
RUN pip install flask

Python file:

from flask import Flask

app = Flask(__name__)

# to connect redis 
# use host "redis_app"
# use port "6379"
# use password "add_password_here"

@app.route('/')
def hello():
    return 'Hello NGINX reverse proxy'

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

Docker-compose-file

version: '3.1'
services:
    nginx:
        image: nginx:latest
        container_name: nginx_reverseproxy
        depends_on:
            - flask
        volumes:
            - ./nginx.conf:/etc/nginx/conf.d/default.conf
        networks:
            - docker-network
        ports:
            - 90:80
    flask:
        build:
            context: ./
            dockerfile: Dockerfile
        image: flask:v1
        container_name: flask_webapp
        volumes:
            - ./:/flask_code/
        environment:
            - FLASK_APP=/flask_code/main.py
        command: python /flask_code/main.py
        networks:
            docker-network:
                aliases:
                    - flask_webapp
        ports:
            - 8080:5000
        depends_on:
                - redis
    redis:
        image: redis
networks:
    docker-network:

nginx.conf

server {
    listen 90;
    server_name localhost;
location / {
        proxy_pass http://flask_webapp:5000/;
        proxy_set_header Host "localhost";
    }
}

1 Answer 1

3

First thing you need to create bridge network. lets say the network name is my_project_network.

docker network create --driver=bridge my_project_network

Now we will create python app.

index.py

from flask import Flask

app = Flask(__name__)

# to connect redis 
# use host "redis_app"
# use port "6379"
# use password "add_password_here"

@app.route('/')
def hello():
    return 'Hello NGINX reverse proxy'

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)


We will use flask so we need to install it. we can do it by create requirements.txt file and use pip3 install -r requirements.txt to install it.

requirements.txt

flask==1.1.2

Now will create Dockerfile for python app.

Dockerfile

FROM python:3

RUN mkdir -p /my_project

WORKDIR /my_project

COPY requirements.txt ./

RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "./index.py"]

Now will create nginx configuration file.

default.conf

server {
    listen 80;
    server_name localhost;
    location / {
        proxy_pass http://flask_app:5000/;
        proxy_set_header Host "localhost";
    }
}

Now will create docker-compose file.

docker-compose.yml

version: "3.7"

services:
  nginx_app:
    image: nginx:latest
    depends_on:
      - flask_app
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8080:80
    networks:
      - my_project_network

  flask_app:
    build:
      context: .
      dockerfile: Dockerfile
    expose:
      - 5000
    depends_on:
      - redis_app
    networks:
      - my_project_network

  redis_app:
    image: redis:latest
    command: redis-server --requirepass add_password_here
    expose:
      - 6379
    networks:
      - my_project_network

networks:
  my_project_network:
    external: true

Now we done all files. so we will run docker compose.

docker-compose up --build -d

To stop it just use

docker-compose stop

Results

enter image description here


NOTE: You can replace port 8080 with any port you need.


NOTE: I created my_project_network to can connect service in docker compose each other. but I can do it with out my_project_network. but I prefer to use my_project_network to connect any service from anther docker compose to this docker compose services.

Lets say we have. ./anther_project/docker-compose.yml

version: "3.7"

services:
  flask_app_v2:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 5001:5001
    networks:
      - my_project_network

networks:
  my_project_network:
    external: true

The flask_app_v2 can connect to redis_app because they are all in the same network .


Sign up to request clarification or add additional context in comments.

1 Comment

this helped me to setup a test flask applicztion setup. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.