Create Python + uWSGI + Nginx environment with Docker

Create a verification environment for a web application created with Python 3.5 with Doceker

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".

Install Docker for Mac

I used Docker for Mac. Download Docker for Mac from here and install it on your Mac.

Preparation of Docker Compose configuration file

Directory structure

The figure below shows the directory structure this time.

スクリーンショット 2017-06-28 18.45.27.png

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"

Web server configuration

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

Additional files

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

application

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

Container operation

Build and start the container

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

Confirmation of container ID

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

Stop / start container

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

Log in to the running container

You can enter the container and check the status of the server.

$ docker exec -it 0be4855e99df bash

Delete container

If the container is stopped, it can be deleted.

$docker rm 0be4855e99df

demo

When you start the container and access http: // localhost: 8080 with a browser," Hello World "is displayed.

Recommended Posts

Create Python + uWSGI + Nginx environment with Docker
Create Nginx + uWSGI + Python (Django) environment with docker
Prepare python3 environment with Docker
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Build Mysql + Python environment with docker
Create a simple Python development environment with VSCode & Docker Desktop
Build Jupyter Lab (Python) environment with Docker
[Python] Create a virtual environment with Anaconda
Launch environment with LineBot + Heroku + Docker + Python
Build NGINX + NGINX Unit + MySQL environment with Docker
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Non-blocking with Python + uWSGI
Use python with docker
Python environment with docker-compose
Create a Python environment
WebSocket with Python + uWSGI
Virtual environment with Python 3.6
Prepare the execution environment of Python3 with Docker
Build Django + NGINX + PostgreSQL development environment with Docker
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
Hello World with nginx + uwsgi + python on EC2
Build PyPy and Python execution environment with Docker
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Create an environment with virtualenv
Install Python environment with Anaconda
Japanese can be used with Python in Docker environment
Manage python environment with virtualenv
Create 3d gif with python3
API with Flask + uWSGI + Nginx
Create a python development environment with vagrant + ansible + fabric
Build python3 environment with ubuntu 16.04
Launch a Python web application with Nginx + Gunicorn with Docker
Build python environment with direnv
Create a Layer for AWS Lambda Python with Docker
Create a directory with python
Create a web application execution environment of Python3.4 + Nginx + uWSGI + Flask with haste using venv on Ubuntu 14.04 LTS
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Create an environment for "Deep Learning from scratch" with Docker
Create a Todo app with Django ① Build an environment with Docker
Create plot animation with Python + Matplotlib
Get started with Python! ~ ① Environment construction ~
Create Awaitable with Python / C API
Build python virtual environment with virtualenv
Setup modern Python environment with Homebrew
Create folders from '01' to '12' with python
Create a Python environment on Mac (2017/4)
Create an Excel file with Python3
Building a virtual environment with Python 3
Build PyPy execution environment with Docker
Create a python environment on centos
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
pytorch @ python3.8 environment construction with pipenv
Prepare Python development environment with Atom
Docker environment update: add Python package
Rebuild Django's development environment with Docker! !! !! !!