-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kcrane3576/add-nginx
Add nginx
- Loading branch information
Showing
9 changed files
with
91 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello |