Learn REST API with Flask, Mysql and Docker
A project for you to learn to work a flask REST api with docker and the mysql database manager!
Table of Contents
About The Project
This project is made with the intention of teaching how to use Docker with the backend technologies Flask and Mysql in the project we are going to take into account the following points:
- Create the dockerfile that will have the necessary instructions to create a Python image that will later be converted into a single application.
- Docker Compose allows you through YAML files to instruct the Docker Engine to perform tasks, programmatically. Here we will install the mysql image, declare the environment variables for both mysql and Flask, and also declare the volumes.
- We will add the list of requirements in a requirements.txt file that will be executed automatically within the Dockerfile
Built With
This section should list any major frameworks that you built your project using. Leave any add-ons/plugins for the acknowledgements section. Here are a few examples.
Libraries
SQLAlchemy (Offers an ORM along with a Core)
The Python SQL Toolkit and Object Relational Mapper SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
Flask-Marshmallow (Serializer)
Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmallow (an object serialization/deserialization library) that adds additional features to marshmallow, including URL and Hyperlinks fields for HATEOAS-ready APIs. It also (optionally) integrates with Flask-SQLAlchemy.
Flask-SQLAlchemy
Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It aims to simplify using SQLAlchemy with Flask by providing useful defaults and extra helpers that make it easier to accomplish common tasks.
Getting Started
Prerequisites
For this project you need to have Docker and Docker compose installed
- Link to install Docker engine: Linux - Windows or Mac
- After installing docker engine install docker compose Linux Windows Mac
-
To obtain my repository you must create a folder in a desired directory and within this folder open a terminal or use cmd in the case of windows.
-
Clone the repo
git remote add origin [email protected]:aldomatus/flask_rest_api.git
-
In the folder where docker-compose.yml is located, open a terminal (the same address where you ran the previous line) and write the following command to build the image.
docker-compose build
-
Once the previous execution is finished, you must run the services made in the build.
docker-compose up
-
If all goes well, our application should already be executing the app.py file with python using the mysql database, now we just have to check by entering the following link in our browser:
http://localhost:5000/
-
You should have a response like this:
{"message": "Welcome to my API"}
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Installation
Usage
With this base you can make any flask code, modify the API and adapt it to your projects. It is important that you study the docker code to understand what is behind each file in both the Docker and the docker-compose.yml.
Roadmap
See the open issues for a list of proposed features (and known issues).
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
License
Distributed under the MIT License. See LICENSE
for more information.
Contact
Aldo Matus - Linkedin Facebook
Project Link: Repository