Dockerfile构建一个Python Flask 镜像

作者:小叶柏杉 时间:2021-11-12 10:04:01 

1.Python 程序

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'
 

2.Dockerfile

FROM python:3.9.5-slim

COPY app.py /src/app.py

RUN pip install flask

WORKDIR /src
ENV FLASK_APP=app.py

EXPOSE 5000

CMD ["flask", "run", "-h", "0.0.0.0"]
 

3.开始构建一个小的demo

PS E:\images> docker image build -f .\flask_dockerfile -t flask_py .
[+] Building 80.3s (9/9) FINISHED
 => [internal] load build definition from flask_dockerfile                                                         0.0s
 => => transferring dockerfile: 38B                                                                                0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/python:3.9.5-slim                                              12.7s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 152B                                                                                  0.0s
 => [1/4] FROM docker.io/library/python:3.9.5-slim@sha256:9828573e6a0b02b6d0ff0bae0716b027aa21cf8e59ac18a76724d2  47.4s
 => => resolve docker.io/library/python:3.9.5-slim@sha256:9828573e6a0b02b6d0ff0bae0716b027aa21cf8e59ac18a76724d21  0.0s
 => => sha256:f42d92068b29045b6893da82032ca4fcf96193be5dcbdcfcba948489efa9e832 1.37kB / 1.37kB                     0.0s
 => => sha256:c71955050276b1e3b4be7e29089e4babeb39957981d162a3d422e084601105d3 7.63kB / 7.63kB                     0.0s
 => => sha256:b4d181a07f8025e00e0cb28f1cc14613da2ce26450b80c54aea537fa93cf3bda 27.15MB / 27.15MB                  44.7s
 => => sha256:a1111a8f2ec3f3a8ee44a123047349a70f87d1cfebb9e48b06520d0eed436a71 2.77MB / 2.77MB                     9.3s
 => => sha256:445d04774519ca200f5c48fd028beaafb49ca763dd58767f1ae7e3933306394c 10.93MB / 10.93MB                  32.9s
 => => sha256:9828573e6a0b02b6d0ff0bae0716b027aa21cf8e59ac18a76724d216bab7ef04 1.86kB / 1.86kB                     0.0s
 => => sha256:24f3f85d41f368fc2dcd569b181ef6cd4c2bee419a32853be2f8c8964cee34af 235B / 235B                        11.9s
 => => sha256:d299f7fb612d59c3d87fcb17028a25c02e94722ef6235e60537a12d0e312abfc 2.64MB / 2.64MB                    17.4s
 => => extracting sha256:b4d181a07f8025e00e0cb28f1cc14613da2ce26450b80c54aea537fa93cf3bda                          1.3s
 => => extracting sha256:a1111a8f2ec3f3a8ee44a123047349a70f87d1cfebb9e48b06520d0eed436a71                          0.2s
 => => extracting sha256:445d04774519ca200f5c48fd028beaafb49ca763dd58767f1ae7e3933306394c                          0.5s
 => => extracting sha256:24f3f85d41f368fc2dcd569b181ef6cd4c2bee419a32853be2f8c8964cee34af                          0.0s
 => => extracting sha256:d299f7fb612d59c3d87fcb17028a25c02e94722ef6235e60537a12d0e312abfc                          0.2s
 => [2/4] COPY app.py /src/app.py                                                                                  0.1s
 => [3/4] RUN pip install flask                                                                                   19.8s
 => [4/4] WORKDIR /src                                                                                             0.0s
 => exporting to image                                                                                             0.2s
 => => exporting layers                                                                                            0.2s
 => => writing image sha256:0567a371be3f084fb413092b480735083c224023f8801fc723e228a021ea54b1                       0.0s
 => => naming to docker.io/library/flask_py
 PS E:\images> docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
flask_py              latest    0567a371be3f   10 minutes ago   125MB
PS E:\images> docker container run -d  -p 5000:5000 0567a371be3f
ceb69c7ce778ebcf48a0ad91eb16902814cb20470ddb16d0ba795baa18cf4b01

访问浏览器本地ip:http://127.0.0.1:5000/ 显示Hello, World!

查看容器日志:

PS E:\images> docker logs ceb69c7ce778
 * Serving Flask app 'app.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)
PS E:\images> docker logs ceb69c7ce778
 * Serving Flask app 'app.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [13/Jan/2022 08:50:31] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [13/Jan/2022 08:50:31] "GET /favicon.ico HTTP/1.1" 404 -

来源:https://blog.csdn.net/weixin_48447848/article/details/122476888

标签:Dockerfile,Python,Flask,镜像
0
投稿

猜你喜欢

  • Tensorflow Summary用法学习笔记

    2023-08-12 15:08:53
  • Python为何不支持switch语句原理详解

    2023-03-27 08:37:27
  • 从零学Python之hello world

    2021-08-14 09:38:15
  • Javascript学习第一季 三

    2008-06-27 13:08:00
  • Pycharm2020.1安装无法启动问题即设置中文插件的方法

    2021-02-03 19:28:29
  • 网页设计趋势之:”勾引”用户的按钮

    2009-02-17 12:09:00
  • python获取当前运行函数名称的方法实例代码

    2023-03-06 15:34:22
  • 基于fastapi框架的异步解读

    2022-12-19 21:45:24
  • 使用python的pandas为你的股票绘制趋势图

    2023-12-29 19:48:46
  • Sun拟10亿美元收购MySQL

    2008-01-17 11:56:00
  • ASP用户登录模块的设计

    2008-11-21 16:55:00
  • python保存字典和读取字典的实例代码

    2023-05-12 14:17:54
  • 使用Python制作一个打字训练小工具

    2021-10-19 20:23:12
  • 详解Laravel服务容器的优势

    2023-10-31 03:36:04
  • Python 机器学习库 NumPy入门教程

    2022-09-28 23:10:41
  • 对python中的argv和argc使用详解

    2021-12-09 02:37:52
  • Cython编译python为so 代码加密示例

    2023-01-22 13:16:48
  • python多进程实现文件下载传输功能

    2022-06-27 02:22:41
  • python 获得任意路径下的文件及其根目录的方法

    2022-02-02 17:32:15
  • Python sklearn 中的 make_blobs() 函数示例详解

    2022-07-24 21:45:48
  • asp之家 网络编程 m.aspxhome.com