Create a web service with Docker + Flask

Create a web service with Docker + Flask

Introduction

We will introduce it using the sample application (feedback-api).

`Although it is an article on Mac environment, the procedure is the same for Windows environment. Please read and try the environment-dependent part. ``

Purpose

After reading this article to the end, you will be able to:

No. Overview keyword
1 docker settings install, Dockerfile, docker-compose.yml
2 start docker docker run, docker-compose up

Execution environment

environment Ver.
macOS Catalina 10.15.3
Python 3.7.3
Docker 19.03.8

Source code

I think that understanding will deepen if you read while actually following the implementation contents and source code. Please use it by all means.

GitHub

Related articles

0. Development environment

tree.sh


/
│── config/
├── app/
└── Dockerfiles/
     ├── app/
     │   ├── Dockerfile
     │   ├── docker-compose.yml
     │   └── entrypoint.sh
     ├── docker_compose_up.sh
     └── docker_run.sh

1. Install docker

command_line.sh


~$ brew install docker
~$ brew cask install docker

2. Common definition

2-1. Dockerfile definition

FROM python:3.7

RUN mkdir /code
WORKDIR /code

ADD entrypoint.sh /code/entrypoint.sh
ADD app/ /code

RUN pip install --upgrade pip --no-cache-dir
RUN pip install -r requirements.txt --no-cache-dir

EXPOSE 5000
ENV PYTHONPATH "${PYTHONPATH}:/code/"
ENV FLASK_APP "/code/run.py"
CMD ["/code/entrypoint.sh"]

2-2. Entry point definition

Dockerfiles/app/entrypoint.sh


#!/bin/sh

sleep 5

flask db init
flask db migrate
flask db upgrade

flask run --host=0.0.0.0 --port=5000

3. When connecting to RDB on localhost

docker(flask) <-> localhost(postgres)

3-0. Prerequisites

Keep postgres start on localhost.

3-1. sh definition

Dockerfiles/docker_run.sh


#!/bin/sh

docker stop $(docker ps -q)
docker rm $(docker ps -q -a)
# docker rmi $(docker images -q) -f

rsync -av --exclude=app/tests* app Dockerfiles/app

docker build -t feedback-api Dockerfiles/app
docker run -it -p 5000:5000 feedback-api:latest

3-2. Sh execution

command_line.sh


Dockerfiles/docker_run.sh

4. When connecting to the RDB of the container

docker(flask) <-> docker(postgres)

4-0. Prerequisites

Keep postgres on localhost stop.

4-1. sh definition

Dockerfiles/docker_compose_up.sh


#!/bin/sh

docker stop $(docker ps -q)
docker rm $(docker ps -q -a)
# docker rmi $(docker images -q) -f

rsync -av --exclude=app/tests* app Dockerfiles/app

docker-compose -f Dockerfiles/app/docker-compose.yml up --build

4-2. yml definition

Dockerfiles/app/docker-compose.yml


version: '3'
services:
  docker_postgres:
    image: postgres:11.5
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: feedback
      POSTGRES_INITDB_ARGS: --encoding=UTF-8

  app:
    build: .
    depends_on:
      - docker_postgres
    ports:
      - 5000:5000
    environment:
      ENV_CONFIG: docker
      SQLALCHEMY_POOL_SIZE: 5
      SQLALCHEMY_MAX_OVERFLOW: 10
      SQLALCHEMY_POOL_TIMEOUT: 30

4-3. Sh execution

command_line.sh


Dockerfiles/docker_compose_up.sh

Recommended Posts

Create a web service with Docker + Flask
Create a simple web app with flask
Creating a Flask server with Docker
Run a Python web application with Docker
Create a web service in Flask-SQLAlchemy + PostgreSQL
Start a simple Python web server with Docker
Launch a web server with Python and Flask
Web application development with Flask
Create a homepage with django
Launch a Python web application with Nginx + Gunicorn with Docker
Looking back on creating a web service with Django 2
Create a Layer for AWS Lambda Python with Docker
Create a heatmap with pyqtgraph
Web application with Python + Flask ② ③
Create a directory with python
Web application with Python + Flask ④
[Linux] Create a self-signed certificate with Docker and apache
How to deploy a web app made with Flask to Heroku
Create a web application that recognizes numbers with a neural network
Create a web surveillance camera with Raspberry Pi and OpenCV
(Failure) Deploy a web app made with Flask on heroku
Create a simple Python development environment with VSCode & Docker Desktop
Tornado-Let's create a Web API that easily returns JSON with JSON
Create a Todo app with Django ① Build an environment with Docker
Creating a web application using Flask ②
Build a deb file with Docker
Deploy a Django application with Docker
Create a SlackBot service on Pepper
Build a web application with Django
Creating a web application using Flask ①
Service mesh learned with Docker Swarm
Creating a web application using Flask ③
Create a poisson stepper with numpy.random
Creating a web application using Flask ④
Application development with Docker + Python + Flask
Create a file uploader with Django
Steps to set up Pipenv, create a CRUD app with Flask, and containerize it with Docker
Let's make a WEB application for phone book with flask Part 1
[AWS Hands-on] Let's create a celebrity identification service with a serverless architecture!
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Create a Docker container image with JRE8 / JDK8 on Amazon Linux
Let's make a WEB application for phone book with flask Part 4
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
Getting Started with Flask with Azure Web Apps
Create a dummy image with Python + PIL.
Set up a Samba server with Docker
Play like a web app with ipywidgets
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Make a rare gacha simulator with Flask
Create a GUI app with Python's Tkinter
Get a local DynamoDB environment with Docker
Let's scrape a dynamic site with Docker