It was a little difficult to do flask with the docker version of nginx-unit

The package nginx-unit worked fine as before, but docker didn't work very well and I had a hard time. I didn't write much in the official form, so I'll write it down.

docker image

Official doc ↓

https://unit.nginx.org/howto/docker/#running-apps-in-containerized-unit

Since you hit apt first, you may make an image after hitting apt. (Because it was troublesome to wait with apt when an error occurred ...)

FROM nginx/unit:1.19.0-python3.7

RUN apt-get update && apt-get install -y python3-pip \
    && rm -rf /var/lib/apt/lists/*

Just build with name docker build -t nginx-unit-python .

I tried to move the flask

Flask itself uses a sample created by Visual Studio. Click here for github https://github.com/microsoft/python-sample-vs-learning-flask

(Actually, I changed it to work from app.py)

config.json for nginx-unit

This is also required for the docker version, so create it

config.json


{
  "listeners":{
    "*:8000":{
      "pass":"applications/webapp"
    }
  },
  "applications":{
    "webapp":{
      "type":"python 3",
      "path":"/www/",
      "module":"app"
    }
  }
}

So, the dockerfile for the app image

FROM nginx-unit-python:latest

COPY requirements.txt /config/requirements.txt

RUN pip3 install --no-cache-dir -r /config/requirements.txt \
    && rm -rf /var/lib/apt/lists/*

COPY ./config /docker-entrypoint.d/        # config.put json
COPY --chown=nobody:nogroup webapp/ /www/  #chown important

EXPOSE 8000

The image uses the local image created above. Of course, you don't have to create an image once, you can just enter the apt command.

For the time being, send it to the official command and copy it. (Although the official is included with the bind option)

Put requirements.txt in / config and pip

Put config.json in /docker-entrypoint.d/

*** What was important ***

There is a flask in webapp / and I put this in / www, but if it is as it is When reading a template, I get premission denied and it doesn't work.

2020/08/21 13:45:42 [alert] 19#19 Python failed to import module "views"
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 724, in exec_module
  File "<frozen importlib._bootstrap_external>", line 859, in get_code
  File "<frozen importlib._bootstrap_external>", line 916, in get_data
PermissionError: [Errno 13] Permission denied: '/www/views.py'

Like this. (The same permission denied is said in the template) I just thought that the docker container was running as basic root, so I was really into it here. You can't change the location of various apps. I had no choice but to go inside the container and change the number below / www to 777 and it worked fine.

In-container user survey

What on earth is this? I thought, so I will investigate what process is running inside the container. (Note that the ps aux command inside the container cannot be used) docker container top [container id] Looking at

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                27891               27874               0                   13:50               ?                   00:00:00            unit: main v1.19.0 [unitd --no-daemon --control unix:/var/run/control.unit.sock]
nobody              27947               27891               0                   13:50               ?                   00:00:00            unit: controller
nobody              27948               27891               0                   13:50               ?                   00:00:00            unit: router
nobody              27949               27891               0                   13:50               ?                   00:00:00            unit: "webapp" application

↑ It was like this. So, if you look closely, the main body of the unit seems to be running with nobody. Also check the nobody group. Enter the container, # groups nobody no group is returned.

So, add the --chown option when copying. (At first, it became chmod in the docker file, and I thought that it would be useless if I did not include chown, but it seems that an option was made) https://ken5scal.hatenablog.com/entry/2017/10/13/DockerfileのADD/COPYに--chownオプションができた ↑ I used it as a reference. Thank you very much.

Now it works fine. very

What I thought

It is hard to use the one with little information. Especially those with little information about the latest version ... Does that mean that you can use standard uWSGI etc. because you can turn around in a hurry?

Recommended Posts

It was a little difficult to do flask with the docker version of nginx-unit
A memo on how to overcome the difficult problem of capturing FX with AI
A beginner tried coloring line art with chainer. I was able to do it.
Make a Linux version of OpenSiv3D with find_package a little easier
A network diagram was created with the data of COVID-19.
The sound of tic disorder at work is ... I managed to do it with the code
Make a note of what you want to do in the future with Raspberry Pi
Return the image data with Flask of Python and draw it to the canvas element of HTML
Steps to set up Pipenv, create a CRUD app with Flask, and containerize it with Docker
The story that the version of python 3.7.7 was not adapted to Heroku
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
Creating a Flask server with Docker
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
Even if I converted jpg to png, I managed to do it because the transparency was not tampered with
The story of making a tool to load an image with Python ⇒ save it as another name
Upload data to s3 of aws with a command and update it, and delete the used data (on the way)
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to check the version of Django
Create a web service with Docker + Flask
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
I wanted to know the number of lines in multiple files, so I tried to get it with a command
The file edited with vim was readonly but I want to save it
To output a value even in the middle of a cell with Jupyter Notebook
P100-PCIE-16GB was added to the GPU of Google Colab before I knew it
I failed to install django with pip, so a reminder of the solution
A record of the time it took to deploy mysql on Cloud9 + Rails
If you guys in the scope kitchen can do it with a margin ~ ♪
SSH login to the target server from Windows with a click of a shortcut
What to do if a version error occurs in the selenium Chrome driver
Make it easy to specify the time of AWS CloudWatch Events with CDK.
How to get a list of files in the same directory with python
I tried to create a model with the sample of Amazon SageMaker Autopilot
[Introduction to Python] How to get the index of data with a for statement
I managed to do it because the custom of attaching a zip with a password to an email and saying "I will send you the password separately" is troublesome.
Note: Prepare the environment of CmdStanPy with docker
Prepare the execution environment of Python3 with Docker
The inaccuracy of Tensorflow was due to log (0)
A note about the python version of python virtualenv
Save the object to a file with pickle
I will summarize what I was happy to do to get out of the state where I do not know the letter D of Docker
Try to create a battle record table with matplotlib from the data of "Schedule-kun"
When I tried to do socket communication with Raspberry Pi, the protocol was different
It was a life I wanted to OCR on AWS Lambda to locate the characters.
I want to take a screenshot of the site on Docker using any font
What I did when I was angry to put it in with the enable-shared option
A memo of misunderstanding when trying to load the entire self-made module with Python3
A story about trying to introduce Linter in the middle of a Python (Flask) project
[It was a specification of curl & HTTP] When Flask with uWSGI is run by HTTP, there is a clean delay of 1 second when posting a file.
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
Two solutions to the problem that it is hard to see the vector field when writing a vector field with quiver () of matplotlib pyplot
A memo connected to HiveServer2 of EMR with python
I want to transition with a button in flask
A memo to visually understand the axis of pandas.Panel
Python Ver. To introduce WebPay with a little code.
Take a screenshot of the LCD with Python-LEGO Mindstorms
[PyTorch] A little understanding of CrossEntropyLoss with mathematical formulas
Output log to console with Flask + Nginx on Docker
Try to get the contents of Word with Golang
Visualize the characteristic vocabulary of a document with D3.js