diff --git a/nginx-wsgi-flask/README.md b/nginx-wsgi-flask/README.md index 96dc33d..d0e2d90 100644 --- a/nginx-wsgi-flask/README.md +++ b/nginx-wsgi-flask/README.md @@ -11,12 +11,10 @@ Project structure: │   ├── app.py │   ├── Dockerfile │   ├── requirements.txt -│   └── wsgi.py └── nginx ├── default.conf ├── Dockerfile ├── nginx.conf -    └── start.sh ``` [_compose.yaml_](compose.yaml) @@ -56,15 +54,15 @@ Listing containers must show two containers running and the port mapping as belo ```bash $ docker ps -CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -bde3f29cf571 ...nginx-proxy "/docker-entrypoint.…" About a minute ago Up About a minute (healthy) 0.0.0.0:80->80/tcp ...nginx-proxy_1 -86c44470b547 ...flask-app "gunicorn -w 3 -t 60…" About a minute ago Up About a minute (healthy) 5000/tcp, 0.0.0.0:8000->8000/tcp ...flask-app_1 +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +bde3f29cf571 ...nginx-proxy "/docker-entrypoint.…" About a minute ago Up 4 seconds (health: starting) 0.0.0.0:80->80/tcp ...nginx-proxy_1 +86c44470b547 ...flask-app "gunicorn -w 3 -t 60…" About a minute ago Up 5 seconds (health: starting) 0.0.0.0:53323->8000/tcp ...flask-app_1 ``` -After the application starts, navigate to `http://localhost:80` in your web browser or run: +After the application starts, run the command in a terminal: ```bash -$ curl localhost:80 +$ curl localhost Hello World! ``` @@ -79,6 +77,35 @@ Removing nginx-wsgi-flask_flask-app_1 ... done Removing network nginx-wsgi-flask_default ``` +Deploy and scale the application. Create three different instances. + +```bash +$ docker compose up -d --scale flask-app=3 +✔ Network nginx-wsgi-flask_default Created 0.0s +✔ Container nginx-wsgi-flask-flask-app-1 Started 0.4s +✔ Container nginx-wsgi-flask-flask-app-3 Started 0.9s +✔ Container nginx-wsgi-flask-flask-app-2 Started 0.6s +✔ Container nginx-wsgi-flask-nginx-proxy-1 Started 1.1s +``` + +See the three different instances. + +```bash +$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +604ad97b224b nginx-wsgi-flask-nginx-proxy "/docker-entrypoint.…" 5 seconds ago Up 4 seconds (health: starting) 0.0.0.0:80->80/tcp nginx-wsgi-flask-nginx-proxy-1 +317e4d706858 nginx-wsgi-flask-flask-app "gunicorn -w 3 -t 60…" 5 seconds ago Up 5 seconds (health: starting) 0.0.0.0:53323->8000/tcp nginx-wsgi-flask-flask-app-1 +39905ed2b5a0 nginx-wsgi-flask-flask-app "gunicorn -w 3 -t 60…" 5 seconds ago Up 4 seconds (health: starting) 0.0.0.0:53324->8000/tcp nginx-wsgi-flask-flask-app-2 +f98baa201cb6 nginx-wsgi-flask-flask-app "gunicorn -w 3 -t 60…" 5 seconds ago Up 4 seconds (health: starting) 0.0.0.0:53325->8000/tcp nginx-wsgi-flask-flask-app-3 +``` + +Each time the url is called, nginx fowards the message to one of three instances. See container id value in the result string. + +``` bash +$ curl localhost/info +{"connecting_ip":"172.30.0.1","containe_id":"f98baa201cb6","host":"localhost","proxy_ip":"172.30.0.1","user-agent":"curl/7.79.1"} +``` + ## About By following the steps above, you will have an NGINX Reverse Proxy and a Flask backend. The general traffic flow will look like the following: diff --git a/nginx-wsgi-flask/compose.yaml b/nginx-wsgi-flask/compose.yaml index d637c95..ef93025 100644 --- a/nginx-wsgi-flask/compose.yaml +++ b/nginx-wsgi-flask/compose.yaml @@ -2,10 +2,6 @@ services: nginx-proxy: build: nginx restart: always - volumes: - - ./nginx/default.conf:/tmp/default.conf - environment: - - FLASK_SERVER_ADDR=flask-app:8000 ports: - "80:80" depends_on: @@ -15,12 +11,12 @@ services: interval: 10s timeout: 10s retries: 3 - command: /app/start.sh + command: nginx -g 'daemon off;' flask-app: build: flask restart: always ports: - - '8000:8000' + - '8000' healthcheck: test: ["CMD-SHELL", "curl --silent --fail localhost:8000/flask-health-check || exit 1"] interval: 10s diff --git a/nginx-wsgi-flask/flask/Dockerfile b/nginx-wsgi-flask/flask/Dockerfile index 1c92802..ae373f7 100644 --- a/nginx-wsgi-flask/flask/Dockerfile +++ b/nginx-wsgi-flask/flask/Dockerfile @@ -26,7 +26,4 @@ ENV PATH="$VIRTUAL_ENV/bin:$PATH" RUN export FLASK_APP=app.py RUN pip install -r requirements.txt -# define the port number the container should expose -EXPOSE 5000 - CMD ["python", "app.py"] \ No newline at end of file diff --git a/nginx-wsgi-flask/flask/app.py b/nginx-wsgi-flask/flask/app.py index db47c3c..d28bd4f 100644 --- a/nginx-wsgi-flask/flask/app.py +++ b/nginx-wsgi-flask/flask/app.py @@ -1,4 +1,5 @@ from flask import Flask, request, jsonify +import socket app = Flask(__name__) @@ -17,6 +18,7 @@ def info(): 'connecting_ip': request.headers['X-Real-IP'], 'proxy_ip': request.headers['X-Forwarded-For'], 'host': request.headers['Host'], + 'containe_id': socket.gethostname(), 'user-agent': request.headers['User-Agent'] } diff --git a/nginx-wsgi-flask/flask/requirements.txt b/nginx-wsgi-flask/flask/requirements.txt index f358d0a..abc3373 100644 --- a/nginx-wsgi-flask/flask/requirements.txt +++ b/nginx-wsgi-flask/flask/requirements.txt @@ -1,2 +1,2 @@ -Flask==1.1.1 -gunicorn==20.0.4 \ No newline at end of file +Flask==2.3.2 +gunicorn==21.0.1 \ No newline at end of file diff --git a/nginx-wsgi-flask/flask/wsgi.py b/nginx-wsgi-flask/flask/wsgi.py deleted file mode 100644 index 5e582a8..0000000 --- a/nginx-wsgi-flask/flask/wsgi.py +++ /dev/null @@ -1,5 +0,0 @@ -from app import app -import os - -if __name__ == "__main__": - app.run(host='0.0.0.0', port=os.environ.get("FLASK_SERVER_PORT"), debug=True) diff --git a/nginx-wsgi-flask/nginx/Dockerfile b/nginx-wsgi-flask/nginx/Dockerfile index bfb3338..0389f3b 100644 --- a/nginx-wsgi-flask/nginx/Dockerfile +++ b/nginx-wsgi-flask/nginx/Dockerfile @@ -5,7 +5,8 @@ RUN apk add bash # Add nginx.conf to container COPY --chown=nginx:nginx nginx.conf /etc/nginx/nginx.conf -COPY --chown=nginx:nginx start.sh /app/start.sh +COPY --chown=nginx:nginx default.conf /etc/nginx/conf.d/default.conf +# COPY --chown=nginx:nginx start.sh /app/start.sh # set workdir WORKDIR /app @@ -29,4 +30,5 @@ RUN touch /var/run/nginx.pid && chown -R nginx:nginx /var/run/nginx.pid USER nginx -CMD ["nginx", "-g", "'daemon off;'"] +# CMD ["nginx", "-g", "'daemon off;'"] +CMD ["bash"] \ No newline at end of file diff --git a/nginx-wsgi-flask/nginx/default.conf b/nginx-wsgi-flask/nginx/default.conf index 4cf2eb0..ad440f5 100644 --- a/nginx-wsgi-flask/nginx/default.conf +++ b/nginx-wsgi-flask/nginx/default.conf @@ -4,14 +4,14 @@ server { listen 80; location / { - proxy_pass http://$FLASK_SERVER_ADDR; + proxy_pass http://flask-app:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /cache-me { - proxy_pass http://$FLASK_SERVER_ADDR; + proxy_pass http://flask-app:8000; proxy_cache cache; proxy_cache_lock on; proxy_cache_valid 200 30s; @@ -25,5 +25,4 @@ server { add_header Content-Type text/plain; return 200 "success"; } - } diff --git a/nginx-wsgi-flask/nginx/nginx.conf b/nginx-wsgi-flask/nginx/nginx.conf index e6700a3..9cf0077 100644 --- a/nginx-wsgi-flask/nginx/nginx.conf +++ b/nginx-wsgi-flask/nginx/nginx.conf @@ -46,5 +46,4 @@ http { # ssl_session_tickets off; include /etc/nginx/conf.d/*.conf; - } diff --git a/nginx-wsgi-flask/nginx/start.sh b/nginx-wsgi-flask/nginx/start.sh deleted file mode 100644 index 39f666d..0000000 --- a/nginx-wsgi-flask/nginx/start.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -envsubst '$FLASK_SERVER_ADDR' < /tmp/default.conf > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'