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

Add development docker w/ postgres #2

Open
wants to merge 3 commits into
base: main
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@ Just A Cool Online Ballot-Box
## Requirements

- Python 3.11

## Docker for development

You can use docker and avoid any setup. Just run:

```
docker-compose -f dev.yaml build
docker-compose -f dev.yaml up
```

which will spin up the Flask application (http://localhost:5000), a Postgres instance, and an Adminer instance to poke at the database (http://localhost:5001).
Any changes to the `app/` folder will be immediately reflected inside the Flask container.
To wipe the database, delete the `jacob_dev_db_data` volume.
By default, webservers are only visible to localhost.
8 changes: 8 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3.11

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .

CMD ["python", "app.py"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a 'proper' way to run flask using the Flask CLI, which we could set as an entrypoint (and then pass in any args we want via docker-compose or manually running the container), but that's not strictly necessary.

At some point in the future we should rejig this (or have a separate production dockerfile) to use a WSGI server, see the flask docs on deployment

Copy link
Member Author

@mxbi mxbi Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is to 100% have a separate production dockerfile, so I'm happy running in debug here.

Flask API for running could be nicer I agree.

Copy link
Collaborator

@swedgwood swedgwood Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also looks like you can do some other nice things like custom commands that have access to app context which we might want to use in the future to make administering jacob easier. But I still think having a sane __name__="__main__" block for running without the CLI makes sense to have.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably going to be needed for "first time DB setup" (and migrations? but i'm gonna pretend we'll never need to do migrations)

File renamed without changes.
9 changes: 8 additions & 1 deletion app.py → app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ def about():
import admin

if __name__ == '__main__':
app.run()
import os

if os.environ.get('PRODUCTION'):
raise Exception("Can't run interactively in PRODUCTION")

app.run(host=os.environ.get('FLASK_HOST', '127.0.0.1'),
port=os.environ.get('FLASK_PORT', 5000),
debug=os.environ.get('FLASK_DEBUG', True))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: '3.8'

services:
# Flask app
app:
build: ./app/
ports:
- 127.0.0.1:5000:5000
volumes:
- type: bind # For development, we pass through the existing files for ez reload
source: app/
target: /app/
depends_on:
- db
environment:
- FLASK_HOST=0.0.0.0

# Postgres DB
db:
image: postgres
volumes:
- dev_db_data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=dev
expose:
- 5432 # visible to other containers only

# Admin panel for Postgres DB - DEVELOPMENT ONLY
# Use login credentials postgres/dev
adminer:
image: adminer
ports:
- 127.0.0.1:5001:8080

volumes:
dev_db_data: