之前一篇构建了最简单的helloworld程序,只是在命令行界面打印,此篇博客尝试需要环境依赖的镜像(flask框架)构建,实现的效果是能在网页上显示helloworld。

1 项目准备

创建项目文件夹及对应源码

root@master:/home/hqc# cd docker_learning/
root@master:/home/hqc/docker_learning# mkdir flask-hello-world
root@master:/home/hqc/docker_learning# cd flask-hello-world/
root@master:/home/hqc/docker_learning/flask-hello-world# touch flask-hello-world.py
root@master:/home/hqc/docker_learning/flask-hello-world# vim flask-hello-world.py from flask import Flaskapp = Flask(__name__)@app.route('/')def hello_world():return 'hello world'if __name__ == '__main__':app.run(debug=True,host='0.0.0.0')# 创建专用文件夹并在其中创建python程序

2 下载flask框架在本地测试可行性

测试可行!

root@master:/home/hqc/docker_learning/flask-hello-world# pip install flaskSuccessfully installed Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.4 itsdangerous-1.1.0
# 安装flaskroot@master:/home/hqc/docker_learning/flask-hello-world# python flask-hello-world.py * Serving Flask app "flask-hello-world" (lazy loading)* Environment: productionWARNING: This is a development server. Do not use it in a production deployment.Use a production WSGI server instead.* Debug mode: on* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)* Restarting with stat* Debugger is active!* Debugger PIN: 182-111-029127.0.0.1 - - [29/Nov/2021 19:54:52] "GET / HTTP/1.1" 200 -127.0.0.1 - - [29/Nov/2021 19:54:52] "GET /favicon.ico HTTP/1.1" 404 -
# 运行python程序,成功!访问网址为:http://127.0.0.1:5000/

3 编写dockerfile文件

容器里边也需要下载flask框架,并且需要暴露端口,否则只能本地访问

root@master:/home/hqc/docker_learning/flask-hello-world# vim DockerfileFROM python# LABEL "maintainer=2790051454@qq.com"RUN pip install flaskCOPY flask-hello-world.py /flask-hello-world/ # 将本机中的py文件拷贝到容器的/flask-hello-world/下WORKDIR /flask-hello-world # 切换工作目录为容器中的/flask-hello-world下# 暴露端口,否则只有本地可访问EXPOSE 5000CMD ["python", "flask-hello-world.py"] # 执行程序

4 构建并运行镜像

root@master:/home/hqc/docker_learning/flask-hello-world# docker build -t flask-hello-world:v1.0 .
# 构建镜像1.0版本root@master:/home/hqc/docker_learning/flask-hello-world# docker imagesREPOSITORY                                             TAG       IMAGE ID       CREATED         SIZEflask-hello-world                                      v1.0      6c3d77891c6a   2 minutes ago   928MB
# 查看镜像,存在root@master:/home/hqc/docker_learning/flask-hello-world# docker run flask-hello-world:v1.0* Serving Flask app 'flask-hello-world' (lazy loading)* Environment: productionWARNING: This is a development server. Do not use it in a production deployment.Use a production WSGI server instead.* Debug mode: on* 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)* Restarting with stat* Debugger is active!* Debugger PIN: 127-875-497172.17.0.1 - - [29/Nov/2021 12:12:18] "GET / HTTP/1.1" 200 -172.17.0.1 - - [29/Nov/2021 12:12:18] "GET /favicon.ico HTTP/1.1" 404 -
# 运行该docker镜像。由输出信息可知,访问地址为http://172.17.0.2:5000/

5 浏览器访问验证


成功!!!

【Docker】Dockerfile构建自定义进阶的helloworld镜像-1相关推荐

  1. 【DockerFile构建自定义镜像实操出现问题解决方案Failed to download metadata for repo ‘appstream‘: IPv4 forwarding disa】

    一.知识回顾 之前的内容都帮你整理好了,在这里哟! [0.Docker相关目录文章整理,可自行查看,包含多节内容] [1.Docker详细安装部署&阿里镜像地址配置] [2.Docker架构& ...

  2. 【docker】docker建立最简单最小的helloworld镜像

    本篇博客旨在手动制作一个最简单的myhello镜像,而并非使用用官方的hello-world. 首先运行一下官方的hello-world看看效果: $ docker run hello-worldHe ...

  3. docker| docker/dockerfile 所有知识点,从头开始(更新完)

    1 docker 1.1两个容器间的挂载 root@xiaoni-01:/home# docker run -it --name centos03 --volumes-from centos02 xi ...

  4. Docker Dockerfile

    Docker Dockerfile 转自:https://www.runoob.com/docker/docker-dockerfile.html 什么是 Dockerfile? Dockerfile ...

  5. Docker进阶篇之DockerFile,教你用DockerFile构建镜像

    文章目录 一.DockerFile介绍 二.DockerFile构建过程 三.DockerFile指令 四.实战测试 4.1 对比原生CentOS 五.CMD 和 ENTRYPOINT 的区别 5.1 ...

  6. Docker学习之六:基于Dockerfile构建镜像

    镜像制作 一般镜像的制作,通常需要修改镜像的配置文件,比如nginx的配置文件,可以通过以下的方式: 将配置文件做成存储卷,从宿主机编辑好之后,启动容器时应用程序加载配置文件的路径并和宿主机的目录建立 ...

  7. linux docker查找镜像文件,搜索/下载/构建自定义/删除Docker镜像,运行和删除Docker容器的方法...

    本文为你介绍的内容是Docker入门相关的操作方法:搜索Docker镜像.下载Docker镜像.运行Docker容器.构建自定义Docker镜像.删除容器.删除Docker镜像.以下给出的所有步骤均在 ...

  8. 使用Dockerfile构建Docker镜像

    目录 前言 Dockerfile的书写规则及指令使用方法 创建Dockerfile,构建运行环境 构建镜像 Dockerfile参考示例 Dockerfile最佳实践 1. 前言 Dockfile是一 ...

  9. docker:dockerfile构建镜像

    目录 一.dockerfile概念 1.dockerfile的原理就是镜像分层 二.docker镜像的创建 1.基于现有镜像创建 2.基于本地模板创建 3.基于dockerfile创建 3.1 doc ...

最新文章

  1. baidu mp3竟然还加密,太扯了
  2. 为智能手机VR体验而生,ARM公布最新处理器架构
  3. lda主题模型的可视化_把LDA主题模型作为自己的硕士课题,有什么可以做的?
  4. JD商家后台管理的细节
  5. bzoj 1620: [Usaco2008 Nov]Time Management 时间管理(贪心)
  6. 从零开始实现一个简易的Java MVC框架(四)--实现AOP
  7. win10如何离线安装.NET Framework3.5
  8. 【爬虫】 Squid 3.5.20 安装与配置 高匿代理
  9. LeetCode 13 罗马符号转化为数字(难度: Easy)
  10. BZOJ2069 POI2004ZAW(最短路)
  11. 当年明月 Vs. 阎崇年
  12. 3P21J30 2023年最新版车载版下载
  13. 关于点云数据的包围盒的方法总结
  14. 计算机控制交通灯实验报告,PLC实验专用周实验报告 交通灯
  15. 关于window11下AE2022没有扩展选项或安装svga插件不现实的问题
  16. 我和女朋友是如何从渣二本一起考研逆袭京都985和211的(成长历程)
  17. 免费的minecraft账号
  18. axios拦截设置和错误处理
  19. 从0到1实现单机记账APP原理与细节uniApp内含源码 (二)
  20. 虾皮怎么注册开店,怎么开本土店铺?

热门文章

  1. PHP 连接数据库的3种方式
  2. 经典滤波器的设计原理
  3. Blazor + Ant Design + .NET 5快速开发框架(1) - 项目创建
  4. Unity3dRPG 相机跟随player旋转_【玩码】升降式旋转镜头来袭:三星A80将于7月12日开始预约...
  5. html支持LaTeX嘛,latex.html
  6. 当HUAWEI HiAI遇到淘宝
  7. word批量删除空行方法
  8. oracle yyyy和yy,日期格式 ccyy和yyyy 有什么区别
  9. 新型环保材料集成墙板有什么优点?是用什么材料做成的?
  10. linux路由软件quagga安装