Tools, FAQ, Tutorials:
Requirements to Python Hello Website Image
What are the requirements to build a Hello Website Docker image using the Python base image?
✍: FYIcenter.com
If you want to build simple Hello Website Docker image using a Python script,
you need to prepare the following required files.
1. Create the Hello Website Python script.
fyicenter$ mkdir python
fyicenter$ cd python
fyicenter$ vi hello-web.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(Docker Container Platform - Tutorials)
@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if Docker Container Platform - Tutorials == "__main__":
app.run(host='0.0.0.0', port=80)
2. Create a text file, package.lst, to hold the required Python package names.
fyicenter$ vi package.lst Flask Redis
3. Create the Dockerfile.
fyicenter$ vi Dockerfile # Use an official Python runtime as a parent image FROM python # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install required packages RUN pip install --trusted-host pypi.python.org -r package.lst # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME World # Run the Python script when the container launches CMD ["python", "hello-web.py"]
As you can see, we provided in the following instructions in the Dockerfile:
See next tutorial to build a new Docker image using this Docker file.
⇒ Build and Run Python Hello Website Image
⇐ "python" - Python Docker Image
2020-08-25, ∼1458🔥, 0💬
Popular Posts:
How To Copy Array Values to a List of Variables in PHP? If you want copy all values of an array to a...
Where to find tutorials on EPUB file format? I want to know how to create EPUB books. Here is a larg...
What is EPUB 2.0 Metadata "dc:creator" and "dc:contributor" elements? EPUB 2.0 Metadata "dc:creator"...
Where to find tutorials on Visual Studio? I want to know How to learn Visual Studio. Here is a large...
What's Wrong with "while ($c=fgetc($f)) {}" in PHP? If you are using "while ($c=fgetc($f)) {}" to lo...