【Docker】小案例学习
1、进入windows版docker界面,新建文件夹pp2、进入pp,在目录下新建三个文件dockerfile.txt,app.py,requirements.txt
2.1 dockerfile文件
# 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
Flask
Redis
2.3 app.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(__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构建,注意.不可忽略
docker build -t pyapp:v1.0.1 -f ./dockerfile.txt .
4、查看镜像
docker image ls
5、运行镜像
docker run -p 4000:80 pyapp:v1.0
6、访问http://192.168.99.100:4000/,具体IP地址似绑定的为准
感谢,学到了 666666666666 docker确实是好东西 牛的,很实用的👍🏻 平时多学学各位分享的技术贴,面试不慌张
页:
[1]