[PYTHON] [With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 1]

Summary of this article

Premise

The command notation is based on the assumption that MacOS will be used as the host PC.

Preparation as a host PC

In this article, we will build a local PC as a Docker host PC. In this case, you need to prepare to run Docker. There are several methods, but for MacOS, installing Docker for Mac with brew is the quickest way.

Terminal


brew cask install docker

This will install the application, so launch Docker.app locally. If an icon like a whale appears in the upper right and it is Running as shown below, it is complete. スクリーンショット 2020-01-08 14.20.55.png

Final product

Image diagram

Flask_Nginx_unicorn_diagramdrawio-Step3-docker-compose (1).png

Source code

See Github repository.

Source code brief explanation

There are two main changes from Step 1 in making Docker containerization.

--Create a new Dockerfile for each web server and app server --Create a new Makefile containing the startup command

Of these, the second Makefile only simplifies the input of docker commands, and the essential addition is the first Dockerfile. I would like to take a look at the contents of each Dockerfile. (The Dockerfile itself will be described later)

Web server

Dockerfile


#Specify the base image. In this case, a specific tag is specified up to the version.
FROM nginx:1.17.6-alpine

#Host PC configuration file(config/nginx.conf)Copy to the directory created above and send
RUN mkdir -p /etc/nginx
COPY config/nginx.conf /etc/nginx/nginx.conf

If you just want to use nginx, just pull the nginx Official Image published on Docker Hub. This time, I want to read the nginx configuration file created at hand and start it, so I am doing the following work.

  1. Instruct the base image
  2. Create a path to send your own config file
  3. Copy your own configuration file from the host PC to this image

App server

Dockerfile


#This time it is based on a debian-based python image
FROM python:3.8-slim-buster

#Transfer of configuration files(I can't think of a suitable place/Directly under the app)
RUN mkdir -p /app/config
COPY requirements.txt /app/requirements.txt
COPY config/gunicorn_settings.py /app/config/gunicorn_settings.py
COPY flask_app.py /app/flask_app.py

#Package installation
RUN pip install -r /app/requirements.txt

EXPOSE 9876

WORKDIR /app

#Command at run time
# ENTRYPOINT->In CMD, CMD can be overwritten with the argument at the time of run
ENTRYPOINT [ "gunicorn", "flask_app:app" ]
CMD [ "-c", "/app/config/gunicorn_settings.py" ]

You can start with the base image of os only and install python etc., but since the official image of python has been released, this time we will start based on this. Up to package installation is almost the same as the web server, but below that it is a description that the web server did not have. I am doing the following.

  1. Put 9876 port in LISTEN state in the container of App server.
  2. Set the base directory when starting the container to / app
  3. Execute the gunicorn flask_app: app -c /app/config/gunicorn_settings.py command when starting the container.

About Docker

Reasons for Docker

I think there are some things about this, but here are two of the benefits I feel.

Unification of development / execution environment and simplification of preparation

In the past, when developing a team, it was necessary for the developer to prepare a development environment on each machine and then start development. There may be a document that shows the procedure, but in reality, it may cost more because the environment cannot be built properly due to reasons such as incomplete documents or tool updates. There is also the risk that each individual will upgrade their language and cause subtle differences. On the other hand, by using the Docker container prepared in advance,

--Environment can be built with a small number of commands (docker run) --Easy to unify development environment

You can benefit from that.

Containers can be placed in the production environment

Now that operation on the cloud has become commonplace, many container orchestration tools using GKE etc. are on the market. Since it is just a container orchestration, it means that the application is actually running on the container even in the production environment. Of course, it is necessary to change environment variables and endpoints between development, staging, and production, but it can also be absorbed by the orchestration side, and in any case ** The container that worked in the development environment can be used almost as it is in the production environment. It will be operational **.

Images and containers

First, create an image (= build), then create and start a container based on that image. It's not a good example, but if you replace it with a PC game --Docker image = disk --Docker container = window while the game is running It's like that.

What is Docker Hub?

A service that allows you to upload, publish, and share container images that you have created. In short, it's like * Github's Docker Limited Edition *. The published images can be freely downloaded and used. When creating your own customized image, select the base image from the ones registered in Docker Hub.

Image reliability

Many images by volunteers are also registered in Docker Hub. Among them, the one that should be selected as the base image is the one with the tag Docker Official Images. In a nutshell, it's ** because it's reliable **, but here are a few bullet points.

--Timely security updates --There is a clear document --Provide Dockerfile best practices --Providing an indispensable base image

What is a Dockerfile

It is a file that describes instructions that can perform a series of operations such as the image that is the base of the container to be created, application installation, and preparation for application startup.

Please refer to here for details on the description of the Dockerfile.

Recommended Posts

[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 2]
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 1]
Output log to console with Flask + Nginx on Docker
Until API made with Flask + MySQL is converted to Docker
Nginx setting summary (Flask + Docker + Gunicorn edition)
Make Django's environment Docker (Docker + Django + Gunicorn + nginx) Part 2
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Using Flask with Nginx + gunicorn configuration [Local environment]
Make Django's environment Docker (Docker + Django + Gunicorn + nginx) Part 3
How to upload with Heroku, Flask, Python, Git (Part 3)
Launch a Python web application with Nginx + Gunicorn with Docker
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
Image processing with Python (Part 2)
API with Flask + uWSGI + Nginx
From environment construction to deployment for flask + Heroku with Docker
Image processing with Python (Part 1)
I noticed while trying to run ninix-aya with Docker (Part 3)
Image processing with Python (Part 3)
(Note) Notes on building TensorFlow + Flask + Nginx environment with Docker Compose
Creating a Flask server with Docker
How to write Docker base image
Convert PDF to image with ImageMagick
Application development with Docker + Python + Flask
How to log in to Docker + NGINX
How to authenticate with Django Part 2
Image upload function with Vue.js + Flask
How to authenticate with Django Part 3
I tried to draw a system configuration diagram with Diagrams on Docker
HTML email with image to send with python
Image classification with Keras-From preprocessing to classification test-
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Send CSS compressed to Gzip with Flask
Connect to MySQL with Python within Docker
Use cryptography library cryptography with Docker Python image
Create Python + uWSGI + Nginx environment with Docker
[Part1] Scraping with Python → Organize to csv!
[Golang] Create docker image with Github Actions
Create a web service with Docker + Flask
Launch Flask application with Docker on Heroku
Try to generate an image with aliasing
Build NGINX + NGINX Unit + MySQL environment with Docker
How to install python3 with docker centos
Sample to convert image to Wavelet with Python
Flask + Gunicorn + Nginx + Supervisor Until it works