Docker - Basic Flask app with Postgresql connection

 

Creating Basic flask app with Postgres, using docker.

This guide will show you how to create a basic flask app and PostgreSQL database in separate containers and connect them together in hopefully a very easy to understand and follow approach.

 

Initial steps to create the environment.

1.       First clone this repository https://github.com/R3m1xed/flask-postgres.git

Navigate to a directory that you wish to work out of
run: git clone https://github.com/R3m1xed/flask-postgres.git

2.       Go into that directory you cloned and create directory called data, if you are on linux run: mkdir data

 

 

Creating the PostgreSQL Database Image.

To make this neater and easier to understand. Create a Dockerfile outside of the of the cloned directory.

In the docker file add the following:

 

FROM postgres:15.2-alpine3.17

 

# Set the environment variables for PostgreSQL

ENV POSTGRES_USER postgres

ENV POSTGRES_PASSWORD MYDATABASEPASSWORD

ENV POSTGRES_DB mydatabase

 

# Expose the PostgreSQL port

EXPOSE 5432

 



Now run in the same directory as the Dockfile you just created
docker image built -t flaskpostgresdb .
Note: you can change flaskpostgresdb to any name you want, this is naming the image. However, for this tutorial to avoid errors and confusion, I would suggest keeping it this name to make sure it works.

 

 

Creating self-signed SSL certificate

This is completely optional, I added this in here for a bit of fun as an easy way to https for your developmental flask app
Run the following commands to generate your key and certificate.

openssl genrsa -out server.key 2048

openssl req -new -key server.key -out server.csr

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

 

After the commands have been run, copy server.key and server.crt to the directory you cloned.

 

 

Creating the Flask image

Go back into the directory that we cloned the GitHub repo for this part and the remainder of this guide. We are going to build the image from the Dockerfile

In the base Directory, run the following

docker image build -t myflaskapp .

 



 

Docker compose and running the flask app with PostgreSQL

If you’ve done all steps up to this part correctly, this part is where the magic happens, and we can have the application running.



Be sure to make sure that POSTGRES_PASSWORD and POSTGRES_DB are the same as what was in the Dockerfile for the PostgreSQL database and in view.py.

This Dockerfile has also created volumes in the local directory that mount to both the flask app and PostgreSQL database. It is particularly useful for the database so if you wish to upgrade the database to a newer version or just keep your data, you have a very easy way of accessing it this way.

Now run:

docker compose up

 

Final Touches

Last thing, I purposely left this out because I did not want to prepopulate the database. I go through logging into database through the container itself.

Run:

docker container ls

If all is going well you should see both containers running



 

Now run:
docker container exec -it flaskdb bash

Now we have a bash shell inside our container.

I would suggest trying to work the next part out by running psql –help to find what you need to do next. However, run the following to get into your database:

psql -U postgres

 

Now that we are logged into the database run the following to see all our databases:

                \l             (that is a lowercase L)

This will show all databases



 

Now, if you’ve looked at view.py, you will see which database and table we are meant to use to retrieve information from our database.

Note: in psql, you can use tab completion

To use mydatabase, run the following:

                \c mydatabase

Now we are going to great a table that is being viewed in by our flask app. Run the following:

CREATE TABLE person ( firstName CHAR(50), lastName CHAR(50));

Now to add some data into the table, we are going to add a few entries

INSERT INTO person firstName, lastName VALUES ('john ', 'doe '), ('mary ', 'jones ');

 

Now lets navigate to https://localhost



This confirms that your database flask app is working with a connection to your database.

 

 

Conclusion

We covered a range of skills here from creating Docker images, using docker compose to create multiple containers and connect them, create the volumes for your containers to keep the data and if need to migrate it or upgrade the containers or services.

Comments

Popular Posts