Getting Started with Docker and Flask

Overview

Getting-Started-with-Docker-and-Flask

Introduction

Docker makes it easier, simpler and safer to build, deploy and manage applications in a docker container. This article will help you get a detailed understanding of;

  • What is docker?
  • Why use Docker?
  • What is a docker image?
  • What is a docker container?
  • Dockerizing a flask application
What is Docker?

Docker is an open source containerization platform for developing, shipping and running applications.

Docker packages software into standardized units called containers. Containers have everything the software needs to run including libraries, system tools, code, and runtime.

More about what docker is

Why use Docker?
  • Responsive deployment and scaling.
  • Faster And Consistent Delivery Of Applications.
Docker image.

Image is a read-only template with instruction for creating containers. Docker images can be considered as the blueprint of the entire application environment that you create.

Docker container.
  • A Docker container is a virtualized run-time environment where users can isolate applications from the underlying system.
  • Containers are compact, portable units in which you can start up an application quickly and easily.
  • Docker containers contains all the essential things required to run an application like code, runtime, system tools, system libraries, and settings.
Dockerizing a flask application
File structure and setup
\-- dockerExample
    |-- app.py
    |-- Dockerfile
    |-- requirements.txt
   \-- templates
        |-- index.html
        

First I created a simple Flask application and added the following code to app.py.

from flask import Flask,render_template
app=Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

if __name__==('__main__'):
    app.run(debug=True)

Check how to create a simple flask application

Add the following code to index.html

>
<html>
<head>
<title>Getting started with Dockertitle>
head>
<body>
<p>This is my Dockerfilep>

body>

Now we need to dockerize the flask application by creating a Dockerfile Dockerfileis a text document that contains all the commands a user could call on the command line to assemble an image.

Add the following code to docker;

FROM python:3.9.6
COPY . /doc
COPY ./requirements.txt /doc/requirements.txt
WORKDIR /doc
EXPOSE 5000:5000
RUN pip install -r requirements.txt
CMD ["python","app.py"]
  • FROM keyword is used to specify the base image to be used. We'll be making use of a python base image.
  • WORKDIR defines the main directory of operations.
  • EXPOSE informs Docker that the container listens on the specified network ports at runtime.
  • RUN is used to install the project’s dependencies.
  • CMD provide defaults for an executing container.

Read more

We can now build our image with the docker build command as shown below;

docker image build -t docker_example .

Screenshot (119)

Once the build process is done, we can run the application with the docker run command as shown below;

docker run -p 5000:5000 docker_example

Screenshot (122)

Owner
Phylis Jepchumba
Software Engineer||Technical writer||Community manager
Phylis Jepchumba
REST API built using flask framework that used for managing bookmarks by individual users.

Bookmarks REST API REST API built using flask framework that used for managing bookmarks by individual users. API Consumers Note This app is built usi

Venkatesh Tantravahi 1 Dec 27, 2021
SQLAlchemy database migrations for Flask applications using Alembic

Flask-Migrate Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations

Miguel Grinberg 2.2k Dec 28, 2022
Connect is a Python Flask project within the cloud-native ecosystem

Connect is a Python Flask project within the cloud-native ecosystem. Second project of Udacity's Cloud Native Nanodegree program, focusing on documenting and architecting a monolith migration to micr

Lauren Ferreira 3 Feb 28, 2022
A boilerplate Flask API for a Fullstack Project :rocket:

Flask Boilerplate to quickly get started with production grade flask application with some additional packages and configuration prebuilt.

Yasser Tahiri 32 Dec 24, 2022
Heroku Flask Setup

Heroku Flask Setup

Abhimanyu Haralukallu 0 Dec 07, 2021
The Snoopy boilerplate in flask framework for development enterprise application.

Snoopy What is snoopy! The "Snoopy" boilerplate in flask framework for development enterprise application. Motivation In my 10 years of development ex

Bekhzod 2 Sep 29, 2022
Cross Origin Resource Sharing ( CORS ) support for Flask

Flask-CORS A Flask extension for handling Cross Origin Resource Sharing (CORS), making cross-origin AJAX possible. This package has a simple philosoph

Cory Dolphin 803 Jan 01, 2023
A team blog based on Flask

A team blog based on Flask This project isn't supported at the moment, please see a newer pypress-tornado Thanks for flask_website and newsmeme at [ht

老秋 549 Nov 10, 2022
Flask 文档中文翻译

Flask 中文文档 这里是 Flask 文档中文翻译项目,欢迎参与! 在开始翻译之前,请务必阅读下面的 Contributing Guide 了解贡献流程,然后阅读这个 Issue 了解翻译要求,在这个 Discussion 投票选出你认为合适的翻译词汇,在这个 Discussion 投票选出你喜

Grey Li 93 Nov 28, 2022
Learn REST API with Flask, Mysql and Docker

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 C

Aldo Matus 0 Jul 31, 2021
Browsable web APIs for Flask.

Flask API Browsable web APIs for Flask. Status: This project is in maintenance mode. The original author (Tom Christie) has shifted his focus to API S

Flask API 1.3k Jan 05, 2023
A python package for integrating ripozo with Flask

flask-ripozo This package provides a dispatcher for ripozo so that you can integrate ripozo with Flask. As with all dispatchers it is simply for getti

Vertical Knowledge 14 Dec 03, 2018
A web application for a fake pizza store, built in Python with Flask and PostgreSQL.

✨ Pizza Pizza - Pizza Store ✨ A web application for a fake Pizza Store, the app let you create an account and order pizza, complements or drinks. Buil

Bonnie Fave 6 Dec 18, 2022
Flask + marshmallow for beautiful APIs

Flask-Marshmallow Flask + marshmallow for beautiful APIs Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmal

marshmallow-code 770 Jan 05, 2023
A simple demo of using aiogram + async sqlalchemy 1.4+

aiogram-and-sqlalchemy-demo A simple demo of using aiogram + async sqlalchemy 1.4+ Used tech: aiogram SQLAlchemy 1.4+ PostgreSQL as database asyncpg a

Aleksandr 68 Dec 31, 2022
An python flask app with webserver example

python-flask-example-keepalive How it works? Basically its just a python flask webserver which can be used to keep any repl/herokuapp or any other ser

KangersHub 2 Sep 28, 2022
Sample Dockerized flask app deployed on Kubernetes on Azure using AKS

Sample Dockerized flask app deployed on Kubernetes on Azure using AKS

Ahmed khémiri 22 Sep 08, 2021
Pagination support for flask

flask-paginate Pagination support for flask framework (study from will_paginate). It supports several css frameworks. It requires Python2.6+ as string

Lix Xu 264 Nov 07, 2022
Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development.

Flask-Starter Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development. It has all the r

Kundan Singh 259 Dec 26, 2022
Alexa Skills Kit for Python

Program the Amazon Echo with Python Flask-Ask is a Flask extension that makes building Alexa skills for the Amazon Echo easier and much more fun. Flas

John Wheeler 1.9k Dec 30, 2022