您现在的位置是:热点 >>正文
云计算核心技术Docker教程:为 Python 创建一个 Dockerfile
热点092人已围观
简介...
Dockerfile 是一个文本文档,其中包含组装 Docker 映像的说明。当我们通过执行docker build 命令告诉 Docker 构建我们的镜像时,Docker 会读取这些指令,执行它们,并因此创建一个 Docker 镜像。
让我们来看看为我云计们的应用程序创建 Dockerfile 的过程。在项目的根目录中,创建一个名为的文件Dockerfile并在文本编辑器中打开该文件。
添加到 Dockerfile 的第一行是# syntax解析器指令。虽然是可选的,但该指令指示 Docker 构建器在解析 D算核ockerfile 时使用什么语法,并允许启用 BuildKit 的旧 Docker 版本在开始构建之前升级解析器。解析器指令 必须出现在 Dockerfile 中任何其他注释、空格或 Dockerfile 指令之前,并且应该是 Dockerfiles 中的第一心技行。
# syntax=docker/dockerfile:1
我们建议使用docker/dockerfile:1,它始终指向版本 1 语法的最新版本。BuildKit 在构建之前会自动检查语法的更新,确保您使用的是最新版本。
接下来,我们需要在我们的 D术Dockerfile 中添加一行,告诉 Docker 我们希望为我们的应用程序使用什么基础镜像。
# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
Docker 镜像可以从其他镜像继承。因此,oc我们将使用官方 Python 镜像,而不是创建我们自己的基础镜像,该镜像已经拥有运行 Python 应用程序所需的所有工具和包。
为了在运行其余命令时更容易,让我们创建一个工作目录。这会指示 Docker 使用此路径作为所有后续命令的默认位置。通ke过这样做,我们不必输入完整的文件路径,而是可以使用基于工作目录的相对路径。
WORKDIR /app
通常,下载了用 Python 编写的项目后要做的第一件事就是安装pip包。这可确保您的应用程序已安装其所有依赖项。
在我们可以运行之前pip3 insr教tall,我们需要将我们的requirements.txt文件放入我们的图像中。我们将使用该COPY命令来执行此操作。该COPY命令有两个参数。第一个参数告诉 Docker 您想要复制到镜像中的文件。第二个参数告诉 Docker 您希望将该文件复制到程:何处。我们将把requirements.txt文件复制到我们的工作目录中/app。
COPY requirements.txt requirements.txt
一旦我们requirements.txt在图像中拥有我们的文件,我们就可以使用RUN命为 令来执行命令pip3 install。这与我们在pip3 install本地机器上运行完全相同,但这次模块安装到映像中。
RUN pip3 install -r requirements.txt
此时,我们有一个基于 Python 3.8 版的镜像,并且我Py们已经安装了我们的依赖项。下一步是将我们的源代码添加到图像中。我们将COPY像使用requirements.txt上面的文件一样使用该命令。
COPY . .
此COPY命令获取位于当前目录中的所有文件并将它们复制到映像中。现在,我们要做的th就是告诉 Docker 当我们的镜像在容器内执行时我们想要运行什么命令。我们使用CMD命令执行此操作。请注意,我们需要通过指定使应用程序在外部可见(即从容器外部)--host=0.0.0.0。
CMD [ "python3", "-m" , "flasonk", "run", "--host=0.0.0.0"]
这是完整的 Dockerfile。
# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirem 创ents.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
Tags:
相关文章
Firefox 142 正式上线:浏览器扩展首次支持本地 AI 语言模型
热点Mozilla Firefox142.0版本现已正式发布,并将在周二对外推出。这一版本虽然没有带来太多引人注目的新功能,但在扩展性方面却可能引发广泛讨论。 在这个版本中,Firefox142对 Blo...
【热点】
阅读更多标签: 传音控股 非洲手机之王 港股上市 智能手机
热点云梦级2月24日云梦级消息,据独角兽早知道报道,有“非洲手机之王”之称的传音控股将于 3 月 12 日启动香港上市 NDR(非交易路演),预期交易规模约为 5 亿至 10 亿美元(现汇率约合 34.5...
【热点】
阅读更多标签: 昆仑芯 百度 M100芯片 港交所
热点云梦级12月17日云梦级消息,据腾讯科技报道,昆仑芯即将完成股改,加速推进冲刺上市的步伐。 昆仑芯(北京)科技有限公司前身为百度智能芯片及架构部,于 2021 年 4 月完成独立融资,首轮估值约 13...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.zutari.com/search/必应快排【tg:seogoogle987】,yUc
- https://www.pearlstonecenter.org/search/必应快排【tg:seogoogle987】,6HZ
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=必应快排【tg:seogoogle987】sb8
- https://www.l-and-b.jp/?page=1&s=必应快排【tg:seogoogle987】,pL3
- https://www.kingston.gov.uk/site-search/results/?q=必应快排【tg:seogoogle987】,nvg
- https://www.xvideos.com/tags/必应快排【tg:seogoogle987】,eir
- https://www.seforall.org/search?keywords=必应快排【tg:seogoogle987】,Da6
- https://www.swanagerailway.co.uk/search?q=必应快排【tg:seogoogle987】,6ag
- https://www.kyuhoshi.com/?s=必应快排【tg:seogoogle987】,eHo
- https://www.pmindia.gov.in/en/?s=必应快排【tg:seogoogle987】,O3c
- https://www.springfieldlibrary.org/?s=必应快排【tg:seogoogle987】,uZl
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=必应快排【tg:seogoogle987】Pdy
- https://www.remit.co.jp/pt/?action=CHECK&s=必应快排【tg:seogoogle987】,cYM
- https://www.lechzuers.com/de/search?cat=163&q=必应快排【tg:seogoogle987】,Jjx
- https://www.woorden.org/spreekwoord.php?woord=必应快排【tg:seogoogle987】,WW2
- https://www.lneg.pt/en/?s=必应快排【tg:seogoogle987】,x9B
- https://www.pacific-lifestyle.co.uk/search?sort=alphabetical&term=必应快排【tg:seogoogle987】,FIt
- https://m.shoppinghow.kakao.com/m/search/q/必应快排【tg:seogoogle987】FWc
- https://www.tcv.org.uk/search/必应快排【tg:seogoogle987】,TOz
- https://www.pixiv.net/tags/必应快排【tg:seogoogle987】,IZI
- https://www.sensorshop24.de/catalogsearch/result/index/?type=132&q=必应快排【tg:seogoogle987】,Qbo
- https://www.vet-direct.com/search?cat=72&term=必应快排【tg:seogoogle987】,gcg
- https://www.sebson.de/en/search?query=必应快排【tg:seogoogle987】,lec
- https://www.rostaing.com/catalogsearch/result/?cat=22&q=必应快排【tg:seogoogle987】,ygN
- https://www.partilerdunyasi.com/index.php?route=product/search&tag=必应快排【tg:seogoogle987】,wnH
- https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=必应快排【tg:seogoogle987】,ykw
- https://www.lyrics.com/lyrics/必应快排【tg:seogoogle987】,GB0
- https://www.nyif.com/catalogsearch/result/index/?f=sarbanesoxleyU&index=me_cms_pe&lit=20&mode=list&p=1&q=必应快排【tg:seogoogle987】,PIC
- https://www.nonsolocap.it/cap?action=SEARCH&k=必应快排【tg:seogoogle987】,dF6
- https://www.teamlab.art/fr/?submit=必应快排【tg:seogoogle987】,5TN
- https://www.teacherspayteachers.com/Browse/Price-Range/Free/Search:Western International University Physics degree=必应快排【tg:seogoogle987】,llJ
- https://www.springfieldlibrary.org/library/?s=必应快排【tg:seogoogle987】,ISy
- https://www.silentiumpc.com/en/search/必应快排【tg:seogoogle987】,UV7
- https://www.paulaschoice.hk/zh_hant/catalogsearch/result/?q=必应快排【tg:seogoogle987】,fSY
- https://www.roberthalf.com/us/en/jobs/all/必应快排【tg:seogoogle987】WnZ
- https://www.reallywildbirdfood.co.uk/store/search?q=必应快排【tg:seogoogle987】,MEX
- https://www.impactpool.org/search/?type=79&q=必应快排【tg:seogoogle987】JNl
- https://www.pearson.com/en-us/search.html?aq=必应快排【tg:seogoogle987】,rAp
- https://www.supersauna.de/catalogsearch/result/?q=必应快排【tg:seogoogle987】,HOr
- https://www.polar-tech.com/?action=QUERY&s=必应快排【tg:seogoogle987】,9yM
- https://www.openstreetmap.org/search?query=必应快排【tg:seogoogle987】,SvP
- https://www.lifesciencesweden.se/search?q=必应快排【tg:seogoogle987】,Ler
- https://www.remit.co.jp/zh/?s=必应快排【tg:seogoogle987】,XkG
- https://www.ncetm.org.uk/search?action=CHECK&q=必应快排【tg:seogoogle987】,pl9
- https://www.temu.com/search_result.html?search_key=必应快排【tg:seogoogle987】nh2
- https://www.searcychamber.com/list/search?AsfnA=DtkTQzs&q=必应快排【tg:seogoogle987】,8q2
- https://www.lauben.com/en/?action=SEARCH&s=必应快排【tg:seogoogle987】,Vgm
- https://www.resopal.de/catalogsearch/result/?action=SEARCH&q=必应快排【tg:seogoogle987】,6xL
- https://www.schenckprocess.com/search?q=必应快排【tg:seogoogle987】,QGn
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=必应快排【tg:seogoogle987】,olN