容器中的FROM python:slim的作用

问题来源

Docker一键运行代码:

FROM python:slim

WORKDIR /secgpt-mini

COPY . /secgpt-mini

RUN pip install -i https://mirrors.ustc.edu.cn/pypi/web/simple -r requirements.txt

EXPOSE 7860

CMD ["python", "webdemo.py", "--base_model", "/secgpt-mini/models"]

第一句是什么意思呢,是用来执行镜像的吗

FROM python:slim 解释:

  • Dockerfile中的 FROM 指令并不是用来执行镜像的,而是用来指定需要构建当前Docker镜像时所使用的基础镜像。基础镜像是构建你的Docker镜像的起点。
  • 这行命令 FROM python:slim 的含义是:构建你的Docker镜像时,将会从已经存在的名为 python:slim 的Docker镜像开始。python:slim 是一个官方提供的Python语言环境的轻量级版本镜像,内置了Python运行环境,但相对于标准版本移除了一些不常用的组件和文件,使得镜像体积更小,更适合作为构建和发布应用的基础环境。
  • 所以,当你在Dockerfile中写下 FROM python:slim,你是在告诉Docker:我要构建一个新的镜像,它以 python:slim 镜像为基础,我可能会在这个基础上做一些修改或增加额外的层次(比如复制代码文件、安装依赖包、设置环境变量等)来满足我的应用需求。
  • 在执行 docker build 命令时,Docker首先查找本地是否有这个基础镜像,如果没有的话,Docker则会去Docker Hub或者指定的镜像仓库下载这个基础镜像,然后按照Dockerfile中的其他指令对其进行修改和构建,最终形成一个新的镜像。

WORKDIR /secgpt-mini解释:

WORKDIR /secgpt-mini 这条指令是在设置容器内部的工作目录。这个目录是容器内的文件系统中的位置,而不是宿主机上的目录。也就是说,此指令会在创建的 Docker 容器内部创建一个名为 /secgpt-mini 的目录,并且该目录会被设为工作目录。当容器启
动时,任何运行在容器内的命令默认都会在这个 /secgpt-mini 目录中执行。

完整的解释如下:

  • FROM python:slim
    这条指令设置该镜像的基础镜像为 python:slim,即它告诉 Docker 使用官方 Python 镜像的 slim 版本作为构建新镜像的起点。
  • WORKDIR /secgpt-mini
    WORKDIR 指定 Docker 容器内的工作目录。如果该目录不存在,Docker 会创建它。之后的指令(如 COPY, RUN, CMD 等)都会在这个指定的目录下执行。这里将工作目录设为 /secgpt-mini。
  • COPY . /secgpt-mini
    COPY 指令将从构建上下文中复制文件和目录到容器中的指定路径。这里 . 表示 Dockerfile 所在的目录(也就是所有源代码和相关文件所在的目录),所有的这些内容都会被复制到容器中的 /secgpt-mini 目录下。
  • RUN pip install -i https://mirrors.ustc.edu.cn/pypi/web/simple -r requirements.txt
    RUN 指令将在容器中运行任何命令。这里它运行 pip 安装命令来从中国科技大学开源软件镜像站安装 requirements.txt 文件中指定的依赖包。
  • EXPOSE 7860
    EXPOSE 指令告诉 Docker 容器在运行时要监听的端口。这里表明容器将在 7860 端口上监听连入的网络请求。
  • CMD [“python”, “webdemo.py”, “–base_model”, “/secgpt-mini/models”]
    CMD 指令为容器指定默认要运行的命令。这里当容器启动时,默认运行 webdemo.py 脚本,并将 /secgpt-mini/models 作为参数传递给该脚本。这意味着,当你运行这个 Docker 容器的时候,它会启动该Python脚本,从而启动你的 Web 服务。
    这个 Dockerfile 通过将源代码复制到容器中,并安装所需的依赖包、暴露服务端口,最终配置了一个可以运行 Python web 应用服务的环境。这些服务通过 webdemo.py 脚本提供,并且在容器启动时自动运行。

上述命令要怎么调用呢

  1. 启动容器:
    首先,需要构建并运行 Docker 容器。这可以通过运行 Docker 命令来完成。
  • 构建镜像:
 docker build -t secgpt-mini .

这里 -t secgpt-mini 是为构建的镜像指定一个标签,. 指定 Dockerfile 所在的当前目录作为构建上下文。

  • 运行容器
     docker run -p 8000:7860 secgpt-mini

这里的 -p 8000:7860 是端口映射参数,意味着将宿主机的 8000 端口映射到容器的 7860 端口(容器内部的应用监听 7860 端口)。secgpt-mini 是上一步骤中定义的镜像名称。

  1. 调用 Web 服务:
    一旦 Docker 容器运行起来,你可以通过在浏览器或者使用工具如 curl 命令来调用 Web 服务。
    浏览器中:只需在 Web 浏览器的地址栏输入 http://localhost:8000(或者替换 localhost 为宿主机的 IP 地址,如果从远程系统访问)。
    使用 curl:你可以使用 curl 工具在命令行中访问你的 Web 服务,例如:
     curl http://localhost:8000/

如何查看构建的镜像是否成功

docker images

该命令将列出所有已构建的镜像,包括你刚刚创建的 secgpt-mini 镜像。

如何查看容器是否成功呢

为了从 secgpt-mini 镜像启动一个容器,你需要使用 docker run 命令。例如:

docker run -d -p 8000:7860 secgpt-mini

这将启动一个基于 secgpt-mini 镜像的容器,并将容器的 7860 端口映射到主机的 8000 端口。 -d 参数告诉 Docker 以分离模式运行容器,也就是在后台运行。
一旦容器启动,你就可以使用 docker ps 命令来查看运行中的容器列表,此时你应该能看到你基于 secgpt-mini 镜像创建的容器。如果容器没有在运行中的话,可以使用 docker ps -a 查看所有容器的状态,了解是否是容器启动失败了。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python : The Ultimate Python Quickstart Guide - From Beginner To Expert (Hands On Projects, Machine Learning, Learn Coding Fast, Learning code, Database) by Mark Zacks English | 14 Jul 2016 | ASIN: B01IGLQM0Q | 125 Pages | AZW3/MOBI/EPUB/PDF (conv) | 4.24 MB Learn Python and catch up with the technologically evolving world with this simple yet elegantly crafted guide! Prepare yourself to put forth your first step into the world of making of very own programs! Utilizing Mark Zack’s The Ultimate Python Quick start Guide - From Beginner to Expert (Hands on Projects, Machine Learning, Learn Coding Fast), you will be able to transcend yourself into: Learning one of the most prominent and dynamic programming language to date, which has evolved itself to be as much powerful and versatile as Java or C++, but is much friendlier to new programmers! Getting yourself acquainted with most the most advanced concepts of Python such as object oriented, imperative, functional and procedural programming using a practice oriented learning system to quickly help you master the language! Knowing the importance of Python in today’s corporate world and job market, and knows exactly how and where you will be able to use your newly found skills to shine in your life! Don’t think about it any longer! Quickly go ahead and visit kindle through your PC, Mac, Tablet or Smartphone and take the first step to your success! Going through the book “The Ultimate Python Quick Start Guide”, you will be able to rapidly get a strong grasp of – The very basics of Python Programming The manipulation of various Python Programming Softwares Interactions between the user and computer using Python Method to develop your first software and beyond (including in-depth data manipulation) The future prospects of learning Python After you have learned the language of Python, no more software developing firms or companies will ever show you the door out and you will exponentially increase your value in the job market! This book is the secret to unlocking your programming potentials within a very short amount of time! Learn how to program using Python, and carve your own bright future! Whether you are an expert, looking to discover something new, or an amateur yet thriving programmer hoping to excel in the field of Python, this book is for you!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值