Build CGI Server running on Python 3 on Docker

Introduction

My name is Ryosuke Kamei and I am an IT consultant based on the idea of "gentle IT"! Currently, in my work, I am doing upstream processes focusing on requirements analysis and requirements definition, but I also write programs! Use the Python3 series (3.5.2 as of July 18, 2016) image on Docker, install CGIServer, and access it from the browser. (There is not much information on Python3 + Docker ~ I had a hard time crying)

Before putting in the framework, let's check how Python works as a web server. (Is it Hello World of the web application?)

Reference (very helpful)

I tried running CGI with python3.

1. Prepare the file.

It is assumed that docker is installed.

Prepare the following four files in the same folder. 1-1. Dockerfike 1-2. CGI server startup file 1-3. index.html 1-4. CGI program written in Python3

1-1. Create Dockerfile

Save it in any of the local folders below.

Dockerfile


#python is from the image
FROM python:3.5.2
MAINTAINER Ryosuke Kamei <[email protected]>

#User created
RUN groupadd web
RUN useradd -d /home/python -m python

WORKDIR /home/python
#Server installation file
ADD cgiserver.py /home/python
#HTML for testing
ADD index.html /home/python
# cgi-Create bin folder
RUN mkdir cgi-bin
ADD cgitest.py /home/python/cgi-bin
RUN chmod 755 /home/python/cgi-bin/cgitest.py

#Start the CGI server by specifying the port number
EXPOSE 8000
ENTRYPOINT ["/usr/local/bin/python", "/home/python/cgiserver.py"]
USER python

1-2. Create a CGI server startup file

(It just works)

cgiserver.py


import http.server
http.server.test(HandlerClass=http.server.CGIHTTPRequestHandler)

1-3. Create index.html

HTML of the top page. It has a form and a submit button. The characters entered here will be displayed on the next screen.

index.html


<!DOCTYPE html>
<html>
<head>
<title>CGI Sample</title>
</head>
<body>
<form action="/cgi-bin/cgitest.py" method="POST">
  <input type="text" name="text" value="test" />  
  <input type="submit" name="submit" />
</form>
</body>
</html>

1-4. CGI program written in Python3

It is a program that displays the value entered in index.html.

cgitest.py


#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import cgi

html_body = """
<!DOCTYPE html>
<html>
<head>
<title>Display received data</title>
<style>
h1 {
font-size: 3em;
}
</style>
</head>
<body>
<h1>%s</h1>
</body>
</html>
"""

form = cgi.FieldStorage()
text = form.getvalue('text','')

print(html_body % (text))

2. Build the container from the Docker file

Build locally (Mac side) from Dockerfile


$ docker build -t {Any container name} .
Example
$ docker build -t python_cgi .

3. Start container (local Mac side)

The point is to specify "-p 8000: 8000". I also wrote "EXPOSE 8000" in the Dockerfile.

Start the container locally (Mac side)


$ docker run -d -p 8000:8000 {Container name given at build time}
Example
$ docker run -d -p 8000:8000 python_cgi

Access with a browser http://192.168.99.100:8000/

Press the submit button and you will see the values entered in the form!

4. Explanation of Dockerfile

4-1. Python is from the image I tried installing with make, but after all it is more general to use the official image of python (laugh).

Dockerfile


FROM python:3.5.2
MAINTAINER Ryosuke Kamei <[email protected]>

4-2. User creation When running CGI, it will not work well with root privileges, so create a dedicated user. (I was quite addicted to it and realized that there was no infrastructure)

Dockerfile


#User created
RUN groupadd web
RUN useradd -d /home/python -m python

4-3. Place the file

Place each file. cgitest.py creates a "cgi-bin" folder and puts it in it. In addition, set the permission to "755". Other than that, place it directly under the user's HOME. (It seems to be good anywhere as long as the Python path is in place. Surprise!)

Dockerfile


WORKDIR /home/python
#Server installation file
ADD cgiserver.py /home/python
#HTML for testing
ADD index.html /home/python
# cgi-Create bin folder
RUN mkdir cgi-bin
ADD cgitest.py /home/python/cgi-bin
RUN chmod 755 /home/python/cgi-bin/cgitest.py

4-4. Finishing Specify the port number with EXPOSE, start "cgiserver.py" with ENTRYPOINT, and specify the newly created user "python" with USER.

Dockerfile


#Start the CGI server by specifying the port number
EXPOSE 8000
ENTRYPOINT ["/usr/local/bin/python", "/home/python/cgiserver.py"]
USER python

After that, as explained in 3, if you create a container and start it, it will operate as a web server!

Recommended Posts

Build CGI Server running on Python 3 on Docker
docker build python based on alpine
build Python on Ubuntu
Install Python 3.6 on Docker
Build Python environment on Windows
How to build a Django (python) environment on docker
Run Python CGI on CORESERVER
Run CGI written in python on Sakura's rental server
Build python environment on windows
Stray build of Python 3 on Sakura's rental server (Note)
Solution for inaccessible Gin server running on Docker container
Build a python environment on CentOS 7.7 for your home server
Try running tensorflow on Docker + anaconda
Run TensorFlow Docker Image on Python3
Python CGI file created on Windows
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Build a python3 environment on CentOS7
Build python 1.0
Build load test tool Locust 1.1 on Docker
Migrate Django applications running on Python 2.7 to Python 3.5
CGI server (1) python edition in one line
Build Jupyter Lab (Python) environment with Docker
Python 2.7, 3.4, 3.5 extension module build environment on Windows
Build Python3 and OpenCV environment on Ubuntu 18.04
Build a python environment on MacOS (Catallina)
Build an NFS server on Arch Linux
Build a simple WebDAV server on Linux
Created Ubuntu, Python, OpenCV environment on Docker
Build a Samba server on Arch Linux
Build Python environment with Anaconda on Mac
Dockerfile: Install Docker on your Linux server
Build jupyter notebook on remote server (CentOS)
Build a web server on your Chromebook
Install Python3 on mixhost server [Currently NG]
Build a Python + OpenCV environment on Cloud9
Run python wsgi server on NGINX Unit
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Stray build Python-3.8.5 on CentOS without root privileges
A memo of a tutorial on running python on heroku
Build a LAMP environment on your local Docker
[Note] Python environment construction on rental server "CORESERVER"
pykintone on Docker
Build python environment with pyenv on EC2 (ubuntu)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Python on Windows
twitter on python3
Simply build a Python 3 execution environment on Windows
Build a python environment with ansible on centos6
Build a Python environment on Mac (Mountain Lion)
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Python CGI basics
[Python] Build a Django development environment with Docker
Build a Python development environment on your Mac
python on mac
A story about running Python on PHP on Heroku
python at docker
Build Python3 + flask environment on GCP Compute Engine
Build a Docker image containing the private repository Python library on GitHub Actions
Python on Windbg
Building a Python environment on a Sakura VPS server
Build PyPy and Python execution environment with Docker