Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use docker discover host instead of using a environment variable way #375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions nginx-wsgi-flask/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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!
```

Expand All @@ -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:
Expand Down
8 changes: 2 additions & 6 deletions nginx-wsgi-flask/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions nginx-wsgi-flask/flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 2 additions & 0 deletions nginx-wsgi-flask/flask/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Flask, request, jsonify
import socket

app = Flask(__name__)

Expand All @@ -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']
}

Expand Down
4 changes: 2 additions & 2 deletions nginx-wsgi-flask/flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Flask==1.1.1
gunicorn==20.0.4
Flask==2.3.2
gunicorn==21.0.1
5 changes: 0 additions & 5 deletions nginx-wsgi-flask/flask/wsgi.py

This file was deleted.

6 changes: 4 additions & 2 deletions nginx-wsgi-flask/nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
5 changes: 2 additions & 3 deletions nginx-wsgi-flask/nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,5 +25,4 @@ server {
add_header Content-Type text/plain;
return 200 "success";
}

}
1 change: 0 additions & 1 deletion nginx-wsgi-flask/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ http {
# ssl_session_tickets off;

include /etc/nginx/conf.d/*.conf;

}
2 changes: 0 additions & 2 deletions nginx-wsgi-flask/nginx/start.sh

This file was deleted.