Creating a Flask server with Docker

I needed to create a Flask server to send data to Elasticsearch, so I built it using docker-compose

Directory structure

fl/  ├ Dockerfile  ├ docker-compose.yml  └ src/    ├app.py    └requirements.txt

First description of Dockerfile Setting up a file that describes the python version, project directory, and required libraries

FROM python:3.6
ARG project_dir=/projects/
ADD src/requirements.txt $project_dir
WORKDIR $project_dir
RUN pip install -r requirements.txt

Next, write docker-compose.yml Described port setting and flask run command --with-threds is an option to set whether to run in multithreading

docker-compose.yml


version: '3'

services:
  flask:
    build: .
    ports:
      - "6000:6000"
    volumes:
      - "./src:/projects"
    tty: true
    environment:
      TZ: Asia/Tokyo
    command: flask run --host 0.0.0.0 --port 6000 --with-threads

Description of requirements.txt Describe the library to be installed with pip

requirements.txt


flask
elasticsearch

Describe the actual server

app.py


from flask import Flask, jsonify, request
from elasticsearch import Elasticsearch
from datetime import datetime

app = Flask(__name__)
#Elasticsearch host, port and index name
host = "192.168.xx"
port = "9200"
index_name = "XXXXX"

@app.route('/', methods=['POST']) #Enter the URL path and allow only POST
def insert():
    data = json.loads(request.data) #Get the data thrown into flask
        doc = {
            "location" : data["location"]
        }
    es = Elasticsearch(
        hosts = [{'host': host, 'port' : port}]
    )
    res = es.index(index=index_name, body=doc)

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

Go to the fl / directory and

$ docker-compose up -d --build

Start with

If you want to drop it once for correction etc.

$ docker-compose down --rmi all --volumes

Image can be deleted with

Recommended Posts

Creating a Flask server with Docker
Creating a simple app with flask
Set up a Samba server with Docker
Create a web service with Docker + Flask
Tftp server with Docker
Proxy server with Docker
Start a simple Python web server with Docker
Launch a web server with Python and Flask
I made a ready-to-use syslog server with Play with Docker
A server that echoes data POSTed with flask / python
Creating a web application using Flask ②
Creating a decision tree with scikit-learn
Build a deb file with Docker
Persist Flask API server with forever
Deploy a Django application with Docker
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
Application development with Docker + Python + Flask
Make a rare gacha simulator with Flask
Get a local DynamoDB environment with Docker
Let's scrape a dynamic site with Docker
Creating a login screen with Django allauth
Create a simple web app with flask
[Linux] Build a jenkins environment with Docker
Run a Python web application with Docker
Launch Flask application with Docker on Heroku
[Linux] Build a Docker environment with Amazon Linux 2
A series of amateur infrastructure engineers touching Django with Docker (2): Creating a model
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Start a temporary http server locally with Pytest
Build a local server with a single command [Mac]
Set up a simple HTTPS server with asyncio
Set up a local server with Go-File upload-
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
Make a simple pixel art generator with Flask
Procedure for creating a LineBot made with Python
[Python] Build a Django development environment with Docker
Create a bulletin board with Heroku, Flask, SQLAlchemy
Pretend to be a server with two PCs
Set up a local server with Go-File download-
A memo when creating a python environment with miniconda
Commands for creating a python3 environment with virtualenv
Flow of creating a virtual environment with Anaconda
Mount a directory on another server with sshfs
Create a "Hello World" (HTTP) server with Tornado
Try creating a FizzBuzz problem with a shell program
I made a Mattermost bot with Python (+ Flask)
Put Docker in Windows Home and run a simple web server with Python
Until you install Python with pythonbrew and run Flask on a WSGI server
Create an animated GIF local server with Python + Flask
[Day 9] Creating a model
Creating a Home screen
I want to transition with a button in flask
4. Creating a structured program
Looking back on creating a web service with Django 1
A4 size with python-pptx
Set up a Minecraft resource (Spigot) server via docker (2)
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (installation)
Output log to console with Flask + Nginx on Docker