Skip to content

Instantly share code, notes, and snippets.

@pangyuteng
Forked from davidlares/Dockerfile
Last active April 5, 2023 16:57
Show Gist options
  • Save pangyuteng/8398773c00204c041dec9dad837aebf4 to your computer and use it in GitHub Desktop.
Save pangyuteng/8398773c00204c041dec9dad837aebf4 to your computer and use it in GitHub Desktop.
Scaling Nginx servers via Docker' (a Flask app)
**/__pycache__/*

How to use

  • in one terminal
git clone $url docker-scaling
cd docker-scaling
docker-compose up --scale flask=5
  • in another terminal
for i in `seq 1 200`; do curl localhost:2000; done
import socket
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, My container is named: " + socket.gethostname()
version: '3'
services:
flask:
image: myflask
build:
context: .
dockerfile: Dockerfile.flask
volumes:
- ./:/src
expose:
- "5000"
environment:
- FLASK_APP=/src/app.py
nginx:
image: mynginx
build:
context: .
dockerfile: Dockerfile.nginx
ports:
- "2000:80"
volumes:
- ${PWD}:/var/www-data/
- ./:/etc/nginx/conf.d
depends_on:
- flask
FROM python:3
RUN pip3 install flask
CMD flask run --host=0.0.0.0
# using Nginx base image
FROM nginx
# delete nginx default .conf file
RUN rm /etc/nginx/conf.d/default.conf
# add the .conf file we have created
COPY nginx.conf /etc/nginx/nginx.conf
events {}
http {
upstream serv {
server docker-scaling_flask_1:5000;
server docker-scaling_flask_2:5000;
server docker-scaling_flask_3:5000;
server docker-scaling_flask_4:5000;
server docker-scaling_flask_5:5000;
}
server {
listen 80;
location / {
proxy_pass http://serv;
}
location /static/ {
alias /var/www-data/;
}
}
}
@pangyuteng
Copy link
Author

https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-nginx-on-ubuntu-14-04
simple auth with nginx

Dockerfile

COPY nginx.conf /etc/nginx/nginx.conf
COPY .htpasswd /etc/nginx/.htpasswd
RUN chmod 777 /etc/nginx/.htpasswd

nginx.conf

events {}

http {
    upstream serv {
        server flask:5000;
    }
    server {
        listen 80;
        location / {
	    auth_basic "hello";
            auth_basic_user_file .htpasswd;
            proxy_pass http://serv;
        }
    }
}

.htpasswd

echo -n 'myusername:' >> .htpasswd
openssl passwd -apr1 >> .htpasswd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment