mirror of
https://github.com/docker/awesome-compose.git
synced 2025-04-26 10:43:35 +02:00
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>
This commit is contained in:
parent
e6b1d2755f
commit
9faad90a8b
10 changed files with 46 additions and 31 deletions
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"]
|
|
@ -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']
|
||||
}
|
||||
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
Flask==1.1.1
|
||||
gunicorn==20.0.4
|
||||
Flask==2.3.2
|
||||
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
|
||||
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"]
|
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -46,5 +46,4 @@ http {
|
|||
# ssl_session_tickets off;
|
||||
|
||||
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…
Add table
Add a link
Reference in a new issue