Update flask version. Use docker discover host instead of using a environment variable way. Remove unnecessary files. In the endpoint /info, add the container id.

Signed-off-by: Dany Savard <dany.savard@gmail.com>
pull/375/head
Dany Savard 12 months ago
parent e6b1d2755f
commit 9faad90a8b
  1. 41
      nginx-wsgi-flask/README.md
  2. 8
      nginx-wsgi-flask/compose.yaml
  3. 3
      nginx-wsgi-flask/flask/Dockerfile
  4. 2
      nginx-wsgi-flask/flask/app.py
  5. 4
      nginx-wsgi-flask/flask/requirements.txt
  6. 5
      nginx-wsgi-flask/flask/wsgi.py
  7. 6
      nginx-wsgi-flask/nginx/Dockerfile
  8. 5
      nginx-wsgi-flask/nginx/default.conf
  9. 1
      nginx-wsgi-flask/nginx/nginx.conf
  10. 2
      nginx-wsgi-flask/nginx/start.sh

@ -11,12 +11,10 @@ Project structure:
   ├── app.py    ├── app.py
   ├── Dockerfile    ├── Dockerfile
   ├── requirements.txt    ├── requirements.txt
   └── wsgi.py
└── nginx └── nginx
├── default.conf ├── default.conf
├── Dockerfile ├── Dockerfile
├── nginx.conf ├── nginx.conf
   └── start.sh
``` ```
[_compose.yaml_](compose.yaml) [_compose.yaml_](compose.yaml)
@ -56,15 +54,15 @@ Listing containers must show two containers running and the port mapping as belo
```bash ```bash
$ docker ps $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 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 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 About a minute (healthy) 5000/tcp, 0.0.0.0:8000->8000/tcp ...flask-app_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 ```bash
$ curl localhost:80 $ curl localhost
Hello World! Hello World!
``` ```
@ -79,6 +77,35 @@ Removing nginx-wsgi-flask_flask-app_1 ... done
Removing network nginx-wsgi-flask_default 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 ## 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: 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:

@ -2,10 +2,6 @@ services:
nginx-proxy: nginx-proxy:
build: nginx build: nginx
restart: always restart: always
volumes:
- ./nginx/default.conf:/tmp/default.conf
environment:
- FLASK_SERVER_ADDR=flask-app:8000
ports: ports:
- "80:80" - "80:80"
depends_on: depends_on:
@ -15,12 +11,12 @@ services:
interval: 10s interval: 10s
timeout: 10s timeout: 10s
retries: 3 retries: 3
command: /app/start.sh command: nginx -g 'daemon off;'
flask-app: flask-app:
build: flask build: flask
restart: always restart: always
ports: ports:
- '8000:8000' - '8000'
healthcheck: healthcheck:
test: ["CMD-SHELL", "curl --silent --fail localhost:8000/flask-health-check || exit 1"] test: ["CMD-SHELL", "curl --silent --fail localhost:8000/flask-health-check || exit 1"]
interval: 10s interval: 10s

@ -26,7 +26,4 @@ ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN export FLASK_APP=app.py RUN export FLASK_APP=app.py
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
# define the port number the container should expose
EXPOSE 5000
CMD ["python", "app.py"] CMD ["python", "app.py"]

@ -1,4 +1,5 @@
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
import socket
app = Flask(__name__) app = Flask(__name__)
@ -17,6 +18,7 @@ def info():
'connecting_ip': request.headers['X-Real-IP'], 'connecting_ip': request.headers['X-Real-IP'],
'proxy_ip': request.headers['X-Forwarded-For'], 'proxy_ip': request.headers['X-Forwarded-For'],
'host': request.headers['Host'], 'host': request.headers['Host'],
'containe_id': socket.gethostname(),
'user-agent': request.headers['User-Agent'] 'user-agent': request.headers['User-Agent']
} }

@ -1,2 +1,2 @@
Flask==1.1.1 Flask==2.3.2
gunicorn==20.0.4 gunicorn==21.0.1

@ -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)

@ -5,7 +5,8 @@ RUN apk add bash
# Add nginx.conf to container # Add nginx.conf to container
COPY --chown=nginx:nginx nginx.conf /etc/nginx/nginx.conf 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 # set workdir
WORKDIR /app WORKDIR /app
@ -29,4 +30,5 @@ RUN touch /var/run/nginx.pid && chown -R nginx:nginx /var/run/nginx.pid
USER nginx USER nginx
CMD ["nginx", "-g", "'daemon off;'"] # CMD ["nginx", "-g", "'daemon off;'"]
CMD ["bash"]

@ -4,14 +4,14 @@ server {
listen 80; listen 80;
location / { location / {
proxy_pass http://$FLASK_SERVER_ADDR; proxy_pass http://flask-app:8000;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} }
location /cache-me { location /cache-me {
proxy_pass http://$FLASK_SERVER_ADDR; proxy_pass http://flask-app:8000;
proxy_cache cache; proxy_cache cache;
proxy_cache_lock on; proxy_cache_lock on;
proxy_cache_valid 200 30s; proxy_cache_valid 200 30s;
@ -25,5 +25,4 @@ server {
add_header Content-Type text/plain; add_header Content-Type text/plain;
return 200 "success"; return 200 "success";
} }
} }

@ -46,5 +46,4 @@ http {
# ssl_session_tickets off; # ssl_session_tickets off;
include /etc/nginx/conf.d/*.conf; include /etc/nginx/conf.d/*.conf;
} }

@ -1,2 +0,0 @@
#!/bin/bash
envsubst '$FLASK_SERVER_ADDR' < /tmp/default.conf > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'
Loading…
Cancel
Save