zhzhx 发表于 2022-12-2 14:42

【Docker】小案例学习

1、进入windows版docker界面,新建文件夹pp





2、进入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地址似绑定的为准


jokerzheshao 发表于 2022-12-2 14:57

感谢,学到了

新时代 发表于 2022-12-2 15:07

666666666666

bearkr 发表于 2022-12-2 16:48

docker确实是好东西

lfordch 发表于 2022-12-2 18:34

牛的,很实用的&#128077;&#127995;

guiyuandaren 发表于 2022-12-2 20:13

平时多学学各位分享的技术贴,面试不慌张
页: [1]
查看完整版本: 【Docker】小案例学习