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.
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 .
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)
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/
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.
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
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