1、进入windows版docker界面,新建文件夹pp
2、进入pp,在目录下新建三个文件dockerfile.txt,app.py,requirements.txt
2.1 dockerfile文件
[Python] 纯文本查看 复制代码 # Use an official Python runtime as a parent image
FROM python:3.8
# Set the working directory to /app
WORKDIR /usr/src/python-app
# Copy the current directory contents into the container at /app
COPY . /usr/src/python-app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
2.2 requirements.txt
[Python] 纯文本查看 复制代码 Flask
Redis
2.3 app.py
[Python] 纯文本查看 复制代码 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(__name__)
@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 __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
3、在文件夹pp下进行docker构建,注意.不可忽略
[Python] 纯文本查看 复制代码 docker build -t pyapp:v1.0.1 -f ./dockerfile.txt .
4、查看镜像
[Python] 纯文本查看 复制代码 docker image ls
5、运行镜像
[Python] 纯文本查看 复制代码 docker run -p 4000:80 pyapp:v1.0
6、访问http://192.168.99.100:4000/,具体IP地址似绑定的为准
|