Simple Docker Flask SQLite API

By | March 2, 2018

Flask is a lightweight python web framework which can used for creating RESTful APIs. This post shows you how to make a simple Docker Flask SQLite API. This post builds on the excellent Flask API Lesson on The Programming Historian.

If you just want to dive straight into a Docker Flask SQLite API example you can clone this git repository.

The building blocks


Docker logo horizontal spacing Docker –  a lightweight container platform. In this case I was interested in using it to simplify the development process and have a self contained development webserver in a docker container. Flask – a lightweight web framework for python. It comes with a development server, which can be combined with other tools to make it production ready.
SQLite – a database provides some persistent storage for a web application. Other SQL databases such as MySQL and PostgreSQL are perhaps more common associated with web development, but SQLite is capable of running a web site. The simplicity of SQLite also means that it is a good candidate for testing and prototyping.

Assembling the Blocks

As I have already noted, I am essentially using the flask api developed in the programming historian guide. The difference here is that things are encapsulated in a docker container.

What we want to happen is:

  • We want to largely replicate the functionality of The Programming Historian example, but in a docker container.
  • We write and develop our python code in our local machine, and save the files to a particular folder.
  • We configure docker to know which folder we are working in and which packages it should install inside that container.
  • Docker should start our app when we start the container
  • We will need to direct a port on our server – localhost in this example – to the appropriate port on the docker container.
  • A database will provide persistent storage outside of the container (all data inside the container is lost when it is brought down).

docker flask sqlite api


Docker is going to take the place of our usual physical system, so we will start there. I’m using a base docker image based on alpine – a lightweight docker image.

We then just need to do some other set up work:

  • copy the directory containing the docker image into ‘/web’ inside the container so the container has access to the project.
  • Set the working directory to be the correct directory within web.
  • Run pip install with the requirements.txt file inside this working directory.


FROM python:3.4-alpine
COPY . /web
WORKDIR /web/api
RUN pip install -r ./requirements.txt
ENTRYPOINT ["python"]
CMD [""]

Since we want the docker container to interact with the filesystem, we will need to mount the right directory to do docker when we spin up the container. We could run this dockerfile directly, but instead we will use docker compose.


Docker compose is primarily a way for controlling multi-container builds, but we can use it here to encapsulate some of our configuration.

For this case the docker-compose.yaml file controls: how the container is built, the port mapping, and which volumes to mount.

version: '2'
        build: .
            - "5000:5000"
            - .:/web

Docker should now be configured to pick up the contents of the development directory and try to run ‘’ in that directory using python. will be our flask app which we now need to create.

The docker file also expects to find a file ‘requirements.txt‘ which will define which packages need to be installed to the docker container with pip.


For the purposes of this example I am using the sample database used in the programming historian guide. The flask application will read from this. I put it in a directory ‘data’, but it is largely up to you where you put it.

The data directory is in the same directory as the docker file, so will be included in the mounted volume specified by the docker-compose.yaml file.


With these things in place we can write our flask code in As noted elsewhere my example here is based on the programming historian example. My code is available here.

I won’t go too much into the detail of the flask app – you can download it here – but will point out a couple of things to watch out for when developing:

  • While developing this flask app we should run flask in debug mode so it shows us the debugging info in the command line.
  • Flask should be set to run on host ‘’ – This makes the flask server externally visible. Since flask is inside a docker container, externally visible just means visible from outside that container.

Running the API

To test run the API we run docker-compose up –build. Which runs docker with the docker-compose configuration we defined earlier and builds a docker container using the dockerfile.

If all is successful the api should be accessible on localhost:5000.

There are more examples on the programming historian site, but a couple you can try to test out your api are: