Python Flask, ElasticSearch and Docker Environment

P

Hi, and welcome to the second article devoted to the theme:  “How to work with ElasticSearch using Python and Flask framework”. Here we will prepare our local environment for further development. As you remember from the Part 1 we have to create a web server, microservice application and elasticsearch as standalone docker containers.

ElasticSearch tutorial environment
ElasticSearch tutorial environment

Below is docker compose file that will help us to realize environment represented at scheme above. I am also adding the screen of physical project file structure in order it would be clear how according files are organized.

Project file structure
# docker-compose.yml
version: '3'

services:
    udemy_python_app:
        build:
            context: .
        container_name: "udemy_python_app"
        volumes:
            - ./api/:/app/api
            - ./tests/:/app/tests
            - ./src/:/app/src
            - ./parameters.yaml:/app/parameters.yaml
        environment:
          - FLASK_ENV=development
          - PYTHONPATH=/app/
        command: sh -c "cd /app/
            && /usr/bin/supervisord -c /etc/supervisor/supervisord.conf"
        ports:
          - 56733:80
        expose:
            - 56733
    udemy_python_elasticsearch:
        build: ./ci/elasticsearch
        container_name: "udemy_python_elasticsearch"
        environment:
            - discovery.type=single-node
            - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
        volumes:
            - ./ci/data/es/db:/usr/share/elasticsearch/data
            - ./ci/data/es/logs:/usr/share/elasticsearch/logs
            - ./ci/data/es/repo:/usr/share/elasticsearch/repo
        ports:
            - "9203:9200"
        expose:
            - "9203"

Let’s have a look inside the configuration. So first of all, we are having here uwsgi nginx web server and flask application by itself. According docker configuration is located at ci folder. Below is a docker file that installs the server by itself and mounts our server configuration. Here I am using the tiangolo/uwsgi-nginx image. That is a popular ready solution for running python applications.

# Dockerfile
FROM tiangolo/uwsgi-nginx
RUN apt-get update && apt-get install -y bash nano vim

COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

COPY uwsgi.ini /app/uwsgi.ini
COPY main.py /app/main.py
COPY pytest.ini /app/pytest.ini
COPY .flake8 /app/.flake8

And here is our elasticsearch docker container.

# ci/elasticsearch/Dockerfile
FROM elasticsearch:7.13.3

COPY elasticsearch.yml /usr/share/elasticsearch/config/

RUN bin/elasticsearch-plugin install analysis-icu
# ci/elasticsearch/elasticsearch.yml
network:
  host: 0.0.0.0
path:
  repo: /usr/share/elasticsearch/repo
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "Authorization"

To bring up the python flask elasticsearch environment, go to the project root folder and run docker-compose up -d

In the next article (Part3. “Python flask Elasticsearch – front controller and API documentation”) we will speak about package dependencies we are going to use, some project structure aspects, controller, REST API and response/request models. If you would like to pass all material more fast, then I propose you to view my on-line course at udemy where you will also find full project skeleton. Below is the link to the course. As the reader of that blog you are also getting possibility to use coupon for the best possible low price. Otherwise, please wait at next articles. Thank you for you attention.


architecture AWS cluster cyber-security devops devops-basics docker elasticsearch flask geo high availability java machine learning opensearch php programming languages python recommendation systems search systems spring boot symfony