Set up your first microservice using Flask and Docker: Part 1 🚀

H A M IIâ„¢
HacksNextDoor
Published in
3 min readNov 13, 2017

--

Introduction

As a client-based developer (iOS, Android, Front End Web) it can be kinda intimidating building out a backend. I’m here to help alleviate your concerns and give you the rundown on how to build your first basic microservice using Flask, a python based web framework. In Part 2 of this tutorial I will show you how to package your microservice with Docker to avoid ‘dependency hell’.

For this tutorial you will need to install the following:

  • Python 3
  • PIP 3
  • virtualenv

What is PIP?

PIP command is a tool for installing and managing Python packages, such as those found in the Python Package Index. Using PIP, you can easily install a package and all its dependencies.

Installing PIP and Python

I will be running this tutorial on MacOS Sierra. If you are using Windows or Linux here are the links to install python3:

For Mac users, make sure you have Homebrew installed and run the following line:

$ brew install python3

PIP 3 is automatically installed with python3!

Now let’s make sure both pip3 and python3 have been installed. You should see the following outputs after running each command:

$ python3 --version
Python 3.6.0
$ pip3 --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)

Once you have confirmed that both python3 and pip3 are installed, run the follow commands to install virtualenv:

$ pip3 install virtualenv

Then run the following to command to check if it installed correctly:

$ virtualenv --version
15.0.0

Don’t worry, I will explain what virtualenv is down below.

*Just follow tutorial command for command. This will help prevent any confusion with the naming conventions and references to different variables. After this tutorial you will be able to easily create an microservice and deploy it instantly.

Tutorial

  1. Setting up your virtual environment

https://gist.github.com/pandafulmanda/730a9355e088a9970b18275cb9eadef3

To get started, open up your terminal and create a new directory called my-flask-microservice:

$ mkdir my-flask-microservice

What is a virtual environment?

Virtual environments are a way to keep your dependencies inline for your app or service. virtualenv is a tool to create isolated Python environments, in which you can now install dependencies specific for that environment rather than installing them globally. Virtual environments help create consistency in your development and deployment process, which in turn will help build a better app or service. To create a virtualenv first run this command:

$ virtualenv -p python3 {name-of-virtual-env}

For my tutorial, I’ll be naming my virtual env my_venv. So I ran the following command:

$ virtualenv -p python3 my_venv

Once you create the virtual environment, run the following to activate it:

$ source my_venv/bin/activate

After running the activate command you should see the name of your virtual env in front of your terminal cursor like this:

(my_venv) $ source my_venv/bin/activate

When you are done running your app or service, you can shutdown your virtualenv by running the deactivate command:

(my_venv) $ deactivate

Installing flask

Now that we have our virtualenv setup its now time to install our dependencies. Flask is a super light, and easy-to-use micro web framework for Python based on Werkzeug, Jinja 2. A web framework essentially helps streamline and hide most of the overhead that comes with building a web application, such as HTML and Routing.

To install flask run the following command:

(my_venv) $ pip install flask
(my_venv) $ pip freeze > requirements.txt

We are using the freeze command to save all our dependencies to the requirements.txt file. This allows you to easily reinstall your service dependencies for development and production.

After installing flask create a new file called app.py and paste the following snippet into it:

from flask import Flaskapp = Flask(__name__)@app.route("/")
def hello():
return "Caio!"

To make sure the microservice is working run the following command:

$ FLASK_APP=app.py flask run
* Serving Flask app "app"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

You should see the following print statements coming from flask meaning all is well for your service! If you go to http://127.0.0.1:5000/ inside your browser you should see Caio! on the webpage.

If you are interested in learning more about microservices, I highly recommend reading Susan Fowler’s Production Ready Microservices. She does a great job going into detail explaining microservices and why a lot of companies are moving towards this architecture.

Here’s an example of a microservice that returns a payload of estimated time of arrivals to BART stations in SF.

https://bartly-vhapwffhkk.now.sh/all

Click here to view Part 2 of this tutorial

S/O to

for the help writing/publishing 🚀 🚀

Written next door 🖥

--

--