Skip to content

Commit

Permalink
Merge pull request #3 from kcrane3576/add-nginx
Browse files Browse the repository at this point in the history
Add nginx
  • Loading branch information
kcrane3576 committed Jun 14, 2022
2 parents e2e6119 + b5e4436 commit 50ba2e7
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 107 deletions.
3 changes: 2 additions & 1 deletion .env.dev-sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ DATABASE=postgres

# SQLAlchemy
# db is a specific reference to the service in the docker-compose.yml file
DATABASE_URL=postgresql://<POSTGRES_USER>:<POSTGRES_PASSWORD>@<SQL_HOST>:5432/<POSTGRES_DB>
DATABASE_URL=postgresql://<POSTGRES_USER>:<POSTGRES_PASSWORD>@<SQL_HOST>:5432/<POSTGRES_DB>
APP_FOLDER=/usr/src/app
3 changes: 2 additions & 1 deletion .env.prod-sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ DATABASE=postgres

# SQLAlchemy
# db is a specific reference to the service in the docker-compose.yml file
DATABASE_URL=postgresql://<POSTGRES_USER>:<POSTGRES_PASSWORD>@<SQL_HOST>:5432/<POSTGRES_DB>
DATABASE_URL=postgresql://<POSTGRES_USER>:<POSTGRES_PASSWORD>@<SQL_HOST>:5432/<POSTGRES_DB>
APP_FOLDER=/home/app/web
153 changes: 51 additions & 102 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,130 +1,79 @@
# flask-postgres-nginx-docker-compose
MVP repo to have default config to run flask, postgres, and nginx running and networked with docker-compose
- [Guide](https://testdriven.io/blog/dockerizing-flask-with-postgres-gunicorn-and-nginx/)
- [docker environment running flask, nginx, and postgress](https://testdriven.io/blog/dockerizing-flask-with-postgres-gunicorn-and-nginx/)
- configure `.env.dev` and `.env.prod` based on `.env.dev-sample` and `.env.prod-sample`


***
## What do you want to do? ✨
## flask only
***
### ✨ Awareness ✨
[Docker](#docker) section includes sections for building, running, inspecting, and debugging docker
- Run Dev Flask Alone
- [Dev Environment Setup](#dev-environment)
- [Dev Docker Flask Only](#dev-docker-flask-only)

- Run Dev environment with Flask and Postgress
- [Dev Environment Setup](#dev-environment)
- [Dev Docker Flask And Postgress](#dev-docker-flask-and-postgress)

- Run Prod environment with Flask and Postgres
- [Prod Environment Setup](#prod-environment)
- [Prod Docker Flask And Postgress](#prod-docker-flask-and-postgress)


***
## ✨ Environment Setup ✨
***
### ✨ WARNING:✨
You will need ot make sure you update any values surrounded by `<` and `>`
- e.g. change from `<db>` to `some_db_name_you_set`

### Dev Environment
- Create `.env.dev` file in root of project based on `.env.dev-sample`

### Prod Environment
- - Create `.env.prod` file in root of project based on `.env.prod-sample`


***
## ✨ Run the Services ✨
***
The [Docker Cleanup](#docker-cleanup) section is where I would suggest going **first** if you are getting errors before touching the code

### Dev Docker Flask Only
[http://localhost:5001/](http://localhost:5001/)
```shell
docker build -f ./services/web/Dockerfile -t flas-postgress-nginx:latest ./services/web

docker run -p 5001:5000 \
-e "FLASK_APP=project/__init__.py" -e "FLASK_ENV=development" \
flas-postgress-nginx python /usr/src/app/manage.py run -h 0.0.0.0

# cleanup
docker system prune -a -f
-e "FLASK_APP=project/__init__.py" \
-e "FLASK_ENV=development" \
flas-postgress-nginx \
python \
/usr/src/app/manage.py \
run \
-h 0.0.0.0

# container logs
docker-compose logs -f

# wipeout
docker system prune -af
```

### Dev Docker Flask and Postgress

***
## ✨ flask and postgres
***
[http://localhost:8000/](http://localhost:8000/)
```shell
docker-compose up --build -d

#cleanup
docker-compose down -v
docker system prune -a -f
# container logs
docker-compose logs -fdocker-compose -f docker-compose.prod.yml logs -f
```
[Verify DB Setup](#verify-db-setup)

### Prod Docker Flask and Postgress
***
```shell
docker-compose -f docker-compose.prod.yml up -d --build

# database setup
docker-compose -f docker-compose.prod.yml exec web python manage.py create_db

# cleanup
docker-compose -f docker-compose.prod.yml down -v
docker system prune -a -f
```
[Verify DB Setup](#verify-db-setup)
# login and verify database
docker-compose exec db psql --username=$POSTGRES_USER --dbname=$POSTGRES_PASSWORD
test_flask_dev=# \l

Optional Setup
```shell
# can create and add user to database
# create user table and login to verify
docker-compose exec web python manage.py seed_db
docker-compose exec db psql --username=$POSTGRES_USER --dbname=$POSTGRES_PASSWORD
test_flask_dev=# \dt
```


***
## ✨ Database ✨
***
### Verify Database Setup
### ✨ AWARENESS:✨
When logging into the postgres container for dev and prod, the name will be different
- dev: `test_flask_dev`
- prod: `test_flask_prod`
```shell
# validate database is setup and configured
docker-compose exec db psql --username=$POSTGRES_USER --dbname=$POSTGRES_PASSWORD

# verify create_db worked
test_flask_prod=# \l

# ^ Response
# Should show database created
```

### ✨ AWARENESS:✨
The Dev configuration will be the only only that sets up a `Users` table
```shell
# verify create_db worked
test_flask_dev=# \dt

# ^ Response
# Should show Users table created
# clean
docker-compose down -v
docker system prune -af
```



***
## Helpful Docker Commands ✨
## flask postgres nginx
***
```shell
# get docker logs
docker compose logs -f

# Connect to Postgresql in Docker Container
`docker-compose exec db psql --username=<POSTGRES_USER> --dbname=<POSTGRES_DB>`

# Get Volumes
`docker volume ls`
[http://localhost:1337/static/hello.txt](http://localhost:1337/static/hello.txt)
```shell
# build and run environment
docker-compose -f docker-compose.prod.yml up -d --build

# Inspect Volume (Use output from above command to populate <volument-reference>)
`docker volume inspect <volume-reference>`
# setup database
docker-compose -f docker-compose.prod.yml exec web python manage.py create_db
```
***
```shell
# logs
docker-compose -f docker-compose.prod.yml logs -f

# clean
docker-compose -f docker-compose.prod.yml down -v
docker system prune -af
```
10 changes: 8 additions & 2 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ services:
context: ./services/web
dockerfile: Dockerfile.prod
command: gunicorn --bind 0.0.0.0:5000 manage:app
ports:
- 8000:5000
expose:
- 5000
env_file:
- ./.env.prod
depends_on:
Expand All @@ -18,6 +18,12 @@ services:
- postgres_data_prod:/var/lib/postgresql/data/
env_file:
- ./.env.prod
nginx:
build: ./services/nginx
ports:
- 1337:80
depends_on:
- web

volumes:
postgres_data_prod:
4 changes: 4 additions & 0 deletions services/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:1.19-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
16 changes: 16 additions & 0 deletions services/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
upstream test_flask {
server web:5000;
}

server {

listen 80;

location / {
proxy_pass http://test_flask;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}

}
7 changes: 6 additions & 1 deletion services/web/project/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, jsonify
from flask import Flask, jsonify, send_from_directory
from flask_sqlalchemy import SQLAlchemy


Expand All @@ -23,3 +23,8 @@ def __init__(self, email):
@app.route("/")
def hello_world():
return jsonify(hello="world")


@app.route("/static/<path:filename>")
def staticfiles(filename):
return send_from_directory(app.config["STATIC_FOLDER"], filename)
1 change: 1 addition & 0 deletions services/web/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ class Config(object):
# Otherwise, if it is not set, it will use: sqlite://
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL", "sqlite://")
SQLALCHEMY_TRACK_MODIFICATIONS = False
STATIC_FOLDER = f"{os.getenv('APP_FOLDER')}/project/static"
1 change: 1 addition & 0 deletions services/web/project/static/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello

0 comments on commit 50ba2e7

Please sign in to comment.