├── Dockerfile
├── README.md
├── app.py
├── docker-compose.yml
├── init-db.js
└── requirements.txt
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM python:3.6
2 | ADD . /app
3 | WORKDIR /app
4 | RUN pip install -r requirements.txt
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # flask_mongodb_dockerized_app
2 | This code is for illustration to build dockerized Flask + MongoDB app as described in my youtube video below.
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, jsonify
2 | import pymongo
3 | from pymongo import MongoClient
4 |
5 | app = Flask(__name__)
6 |
7 | def get_db():
8 | client = MongoClient(host='test_mongodb',
9 | port=27017,
10 | username='root',
11 | password='pass',
12 | authSource="admin")
13 | db = client["animal_db"]
14 | return db
15 |
16 | @app.route('/')
17 | def ping_server():
18 | return "Welcome to the world of animals."
19 |
20 | @app.route('/animals')
21 | def get_stored_animals():
22 | db=""
23 | try:
24 | db = get_db()
25 | _animals = db.animal_tb.find()
26 | animals = [{"id": animal["id"], "name": animal["name"], "type": animal["type"]} for animal in _animals]
27 | return jsonify({"animals": animals})
28 | except:
29 | pass
30 | finally:
31 | if type(db)==MongoClient:
32 | db.close()
33 |
34 | if __name__=='__main__':
35 | app.run(host="0.0.0.0", port=5000)
36 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | app:
2 | build: .
3 | command: python -u app.py
4 | ports:
5 | - "5000:5000"
6 | volumes:
7 | - .:/app
8 | links:
9 | - db
10 | db:
11 | image: mongo:latest
12 | hostname: test_mongodb
13 | environment:
14 | - MONGO_INITDB_DATABASE=animal_db
15 | - MONGO_INITDB_ROOT_USERNAME=root
16 | - MONGO_INITDB_ROOT_PASSWORD=pass
17 | volumes:
18 | - ./init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
19 | ports:
20 | - 27017:27017
21 |
--------------------------------------------------------------------------------
/init-db.js:
--------------------------------------------------------------------------------
1 | db = db.getSiblingDB("animal_db");
2 | db.animal_tb.drop();
3 |
4 | db.animal_tb.insertMany([
5 | {
6 | "id": 1,
7 | "name": "Lion",
8 | "type": "wild"
9 | },
10 | {
11 | "id": 2,
12 | "name": "Cow",
13 | "type": "domestic"
14 | },
15 | {
16 | "id": 3,
17 | "name": "Tiger",
18 | "type": "wild"
19 | },
20 | ]);
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | flask
2 | pymongo
--------------------------------------------------------------------------------