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?)
I tried running CGI with python3.
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
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
(It just works)
cgiserver.py
import http.server
http.server.test(HandlerClass=http.server.CGIHTTPRequestHandler)
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>
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))
Build locally (Mac side) from Dockerfile
$ docker build -t {Any container name} .
Example
$ docker build -t python_cgi .
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-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