Run Flask App with MongoDB using Docker Compose

In this tutorial you will learn how to create a simple Flask App with MongoDB integration, deploy and run it inside a docker container using docker Compose

Required Software

  • docker (1.6.0 or above)
  • docker-compose (1.3.1+)
  • python 2.7 or above
  • Linux VM - (We used ubuntu 14.04 64 bit)

Files to be created in the app

├── app.py
├── docker-compose.yml
├── Dockerfile
├── README.md
├── requirements.txt
└── templates
    └── todo.html

Create the Parent Directory

Create a parent directory flask_mongo_compose_sample

$ mkdir flask_compose_sample
$ cd flask_compose_sample

Flask Application

Create a new file app.py and add the following python code

import os
from flask import Flask, redirect, url_for, request, render_template
from pymongo import MongoClient

app = Flask(__name__)

client = MongoClient(
    os.environ['DB_PORT_27017_TCP_ADDR'],
    27017)
db = client.tododb


@app.route('/')
def todo():

    _items = db.tododb.find()
    items = [item for item in _items]

    return render_template('todo.html', items=items)


@app.route('/new', methods=['POST'])
def new():

    item_doc = {
        'name': request.form['name'],
        'description': request.form['description']
    }
    db.tododb.insert_one(item_doc)

    return redirect(url_for('todo'))

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)

Template file

This file todo.html contains the Jinja template to display HTML. These will be stored in templates folder

<form action="/new" method="POST">
  <input type="text" name="name"></input>
  <input type="text" name="description"></input>
  <input type="submit"></input>
</form>

{% for item in items %}
  <h1> {{ item.name }} </h1>
  <p> {{ item.description }} <p>
{% endfor %}

Requirements File

Requirements file states the software required to be installed in the container. Create a file requirements.txt inside web folder

flask
pymongo

Dockerfile

This file is needed to create a docker image and deploy it

FROM python:2.7
ADD . /todo
WORKDIR /todo
RUN pip install -r requirements.txt

Docker Compose File

Go to the parent directory flask_mongo_compose_sample and create a file docker-compose.yml. For each service there is a parent tag and child tags which specify

web

  • Builds from the Dockerfile in the current directory.
  • Forwards the exposed port 5000 on the container to port 5000 on the host machine..
  • Mounts the current directory on the host to /todo inside the container allowing you to modify the code without having to rebuild the image.
  • Links to the container name db which is the MongoDB container

db

  • Creates a standard MongoDB container from the image mongo:3.0.2

    web:
      build: .
      command: python -u app.py
      ports:
        - "5000:5000"
      volumes:
        - .:/todo
      links:
        - db
    db:
      image: mongo:3.0.2
    

Build and Run the Service using Docker Compose

Run the following command to build the docker image flask_mongo_compose_sample from web directory and deploy is as a service

$ docker-compose build
$ docker-compose up

You can go to the browser and open the url http://localhost:5000 to see the HTML rendered