I rarely wrote web applications in Python. I wanted to prepare it as a Web application when verifying the operation of non-blocking I / O (considering comparison verification with node.js
and Golang
), so at that time, it was easy with Docker for Mac
. Since the environment was built, it is described as a memorandum.
I prepared a container for the operating environment of Python3.5 + uWSGI + Nginx with Docker Compose
.
As an aside, uWSGI seems to be commonly read as "wesugi". I read it as "you whiskey".
I used Docker for Mac
. Download Docker for Mac
from here and install it on your Mac.
The figure below shows the directory structure this time.
docker-compose.yml There is only one container, and after starting the container, access http from the local port number 8080.
docker_python/docker-compose.yml
version: "2"
services:
# nginx
nginx-python:
build: ./nginx-python
ports:
- "8080:80"
volumes:
- ./app/:/var/www/html/app/
environment:
TZ: "Asia/Tokyo"
Install Python3.5, uWSGI, Nginx on CentOS.
Dockerfile Based on CentOS 6.8.
docker_python/nginx-python/Dockerfile
FROM centos:6.8
ADD ./conf/nginx.repo /etc/yum.repos.d/
# nginx & python
RUN yum localinstall -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN yum install -y https://centos6.iuscommunity.org/ius-release.rpm
RUN yum install -y nginx-1.10.1
RUN yum install -y make gcc
RUN yum install -y libxml2-devel
RUN yum install -y python35u python35u-libs python35u-devel python35u-pip
RUN yum clean all
RUN ln -s /usr/bin/python3.5 /usr/bin/python3
RUN unlink /usr/bin/python
RUN ln -s /usr/bin/python3 /usr/bin/python
RUN ln -s /usr/bin/pip3.5 /usr/bin/pip
RUN pip install uwsgi
# setting nginx
COPY conf/nginx.conf /etc/nginx/nginx.conf
ADD conf/default.conf /etc/nginx/conf.d/default.conf
RUN usermod -u 1000 nginx
EXPOSE 80
ADD ./conf/start.sh /tmp/start.sh
CMD /bin/sh /tmp/start.sh
docker_python/nginx-python/conf/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
docker_python/nginx-python/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile off;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
docker_python/nginx-python/conf/default.conf
server {
listen 80 default;
server_name _;
location / {
include uwsgi_params;
uwsgi_pass localhost:8080;
}
location = /favicon.ico {
empty_gif;
}
}
docker_python/nginx-python/conf/start.sh
#!/bin/sh
/etc/init.d/nginx start
cd /var/www/html/app
chmod -R 777 .
uwsgi --ini uwsgi.ini
Prepare a directory for the application and place the Python file and uWSGI configuration file. Mounted when the container starts.
ʻUwsgi.log and ʻuwsgi.pid
are prepared as empty files (touch
command).
docker_python/app/webapp.py
def application(environ, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
docker_python/app/uwsgi.ini
[uwsgi]
master = True
socket = localhost:8080
wsgi-file = webapp.py
stats = localhost:8181
logto = uwsgi.log
pidfile = uwsgi.pid
Create an empty file.
$ touch docker_python/app/uwsgi.log
$ touch docker_python/app/uwsgi.pid
When both build and start (start in the background with the -d
option):
$ docker-compose up --build
For build only:
$ docker-compose build
For boot only (start in background with the -d
option):
$ docker-compose up
Executed after the container build is completed successfully.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0be4855e99df dockerpython_nginx-python "/bin/sh -c '/bin/..." 2 hours ago Up 2 hours 0.0.0.0:8088->80/tcp dockerpython_nginx-python_1
Taking the above "Confirm container ID" as an example, specify 0be4855e99df
for CONTAINER_ID
.
When stopping:
$ docker stop 0be4855e99df
To get started:
$ docker start 0be4855e99df
When rebooting:
$ docker restart 0be4855e99df
You can enter the container and check the status of the server.
$ docker exec -it 0be4855e99df bash
If the container is stopped, it can be deleted.
$docker rm 0be4855e99df
When you start the container and access http: // localhost: 8080
with a browser," Hello World "is displayed.
Recommended Posts