- Python, Flask, PostgreSQL in a Docker Development environment
- Install and run Docker Desktop
On the command line (the terminal)
-
Clone this repository where you want it.
git clone
-
Change into the directory
-
cd pfp
-
Change the PostgreSQL account info in the
docker-compose.yml
file if you wantPOSTGRES_DB: "dbase" POSTGRES_USER: "dbuser" POSTGRES_PASSWORD: "dbpass" PGDATA: "/var/lib/postgresql/data"
-
The first time you run this, you will need to create a new docker network
docker network create traefikNetwork
-
Start the container
docker-compose up
- Or run it in the background to free up the terminal
docker-compose up -d
-
To stop the containers
- press ctrl-c
- then run
docker-compose down
-
View the web pages at http://lvh.me or http://pfp.lvh.me
- You can also edit the /etc/hosts file to allow for using existing domain
names. For example, add
127.0.0.1 example.com
to your /etc/hosts file (Linux or Mac), or c:\windows\system32\drivers\etc\hosts (Windows). Now you can browse to http://example.com .
- You can also edit the /etc/hosts file to allow for using existing domain
names. For example, add
-
View pgAdmin at http://pga.lvh.me
- After start up, log into pgAdmin with credentials:
- username: dbuser@pgadmin.com
- password: dbpass
- This can be changed in the docker-compose.yml file
PGADMIN_DEFAULT_EMAIL: "dbuser@pgadmin.com" PGADMIN_DEFAULT_PASSWORD: "dbpass"
- Select the 'Add New Server' button.
- Fill out the field for 'Name'.
- Select the 'Connection' tab, and fill out 'Host' with
postgres
, 'Password' withdbpass
, and check the 'Save Password' box. - Click the 'Save' button
- The
dbase
database is now available. - type in the db user name and db password to log in
- After start up, log into pgAdmin with credentials:
-
Connect to the PostgreSQL database with the following credentials:
POSTGRES_DB: "dbase" POSTGRES_USER: "dbuser" POSTGRES_PASSWORD: "dbpass" url: "postgresql://dbuser:dbpass@postgres:5432/dbase"
- The server/host/database url is 'postgres' which is the name of the PostgreSQL container. Because the Python/Flask and PostgreSQL are all in containers, they know to connect to each other through shortcut network names.
You can set environment variables in the docker-compose.yml file and have them available in the Flask app.
Add Environment variables to the Flask container
flask:
build:
context: .
depends_on:
- "traefik"
- "postgres"
environment:
SECRET_KEY: "put_your_secrets_here"
volumes:
- "./webapp:/webapp/"
labels:
- "traefik.enable=true"
- "traefik.http.services.flask.loadbalancer.server.port=80"
- "traefik.http.routers.flask.rule=Host(`localhost`, `lvh.me`, `pfp.lvh.me`, `example.com`)"
- "traefik.http.routers.flask.entrypoints=web"
Access them in the Flask app
Add import os
to the top of the app.py file.
Then add this line after the app = Flask(__name__)
line.
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
- This will run four containers: a proxy container, a Python/Flask container, a PostgreSQL container and a pgAdmin container.
- All of the files for the website building can go in the
webapp
folder. - The database files are stored in the
db-data
folder. This allows for the data to persist between restarts and for hands on access.- To restart with a clean database, just delete this folder.
- To seed the database with a database, tables, and data, just uncomment the
line in the docker-compose.yml file referencing
postgres_seed.sql
. Thedb-data
folder will need to be deleted first. This works best if using a postgres dump file. Otherwise, the sql file just needs to have valid SQL statments.#- ./postgres_seed.sql:/docker-entrypoint-initdb.d/postgres_seed.sql
- You can view the console output of any of the containers by using the
command:
docker logs -f <container name>
This uses the Traefik image from here: https://hub.docker.com/_/traefik/
- Documentation is here: https://doc.traefik.io/traefik/
- You can have multiple domains and subdomains pointing to a single container
using the Hosts line in the label section of docker-compose.yml
- "traefik.http.routers.flask.rule=Host(
lvh.me
,fun.lvh.me
,realdomain.com
)"
- "traefik.http.routers.flask.rule=Host(
lvh.me is a free service that redirects to localhost, so now you can access the site at http://lvh.me instead of http://localhost