Docker meets Flask and Postgres

David Mukiibi
The Andela Way
Published in
4 min readFeb 26, 2018

In this article, we will Dockerize a flask application API with a postgresql database

I’m assuming you are well acquainted with docker and docker compose. Knowledge of the use of docker commands such as docker build, docker run and docker compose commands such as docker-compose up, docker-compose build is sufficient for this article.

Head over to this repository and clone it. In your terminal navigate into the api image folder. In this folder we have a Dockerfile and a bucketlist folder. The latter holds the application API code while the Dockerfile contains all the docker image configurations for the API image to be built. We will build the image in the next section.

In your terminal, while still in the bucketlist folder, run:

docker build -t david:api-image-final .

The above command does the following:

  1. docker build: “tells” the docker to build an image.
  2. -t: while building that image, tag it with david:api-image-final. “david” is the docker repository name and “api-image-final” is the image tag.
  3. . : that right there is a period. The key just above the right option key of the keyboard. This tells the docker daemon to use the current folder aka pwd as the docker context.

I have commented pretty much everything in all the files in this github code repository to guide you through what every command does.

When the above docker command has run successfully, a docker image will have been built and stored locally on your computer.

In your terminal, run the command below to see the docker images locally stored on your computer:

docker images 

Then also run the command below,

cd database-image/database/

In here, there’s a createdb.sql file and a Dockerfile. In the terminal, while still in the database-image/database folder, run the command:

docker build -t david:database-final-image .

This command is similar to the one we saw above, but this time building an image for the postgresql database server.

These image builds will take some time to complete depending on your internet connection speed

With the two images built, let’s create containers from them and connect those containers to each other so that they can “talk/communicate” to each other.

To pull this off, we shall use another docker feature/tool called docker compose.

By code.io

To install docker compose, go to docker-compose and follow the installation guide step by step.

After a successful docker compose installation, in your terminal, navigate to the root folder.

cd docker-flask/

This folder contains the docker-compose.yml file. This yaml file is one that docker compose uses to create containers out of the images we built earlier . When the two containers are up and running, they will then be able to “talk” to each other.

By independenceplus.com

It is in the docker compose file that we declaratively set up all the necessary configurations that allow the interaction between these containers.

source: google.com

Run the command below to use the docker images we built earlier to create containers, start them and then link the two to communicate with each other:

docker-compose -f docker-compose.yml up

When the above command has successfully executed, we will have two containers up and running with the application API ready to accept ingress connections and the database server ready to accept connections from the application API container.

Since this application does not have a front-end, we make use of an application called postman to query the flask application API.

To use the application, read through this README.md file for the different API endpoints.

In a nutshell, that’s it!

“We challenge each other, and leave as friends”. Hit me up on Twitter or LinkedIn for any collaborations on the topic or edits of this article.

Au revoir!

--

--