Docker文件和Python

9cbw7uwe  于 2022-10-23  发布在  Docker
关注(0)|答案(2)|浏览(108)

抱歉,我对码头很陌生。我有以下Docker文件,其中包含以下命令(见下文)。我不确定我听懂了所有的命令,我希望能给你解释一下。我注解了我理解的所有行,但在其他行中打了一个问号。请见下文


# That this line means that python will be our base. Can some comment here please and explain this line more?

FROM python:3.9 as base

# create a working directory in the virtual machine (VM)

WORKDIR /code    

# copy all the python requirements stored in requirements.txt into the new directoy (in the VM)

COPY ./requirements.txt /code/requirements.txt    

# activate the package manager pip. But why use no-cache-dir?

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# copy all files to the new directory (in the VM)

COPY ./ /code/

# I don't understand the line below. Please explain? why uvicorn? app.main:app is the

 #location of the fastapi
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "180"]

谢谢

rekjcdws

rekjcdws1#

Docker文件说明了Docker在创建映像时将执行的所有步骤。根据该图像,可以创建一个容器。


# That this line means that python will be our base. Can some comment here please and explain this line more?

FROM python:3.9 as base

这是非常基本的东西,遵循一个(初学者)教程,你将学到更多的东西,而不仅仅是一个人用勺子灌输一些知识。


# create a working directory in the virtual machine (VM)

WORKDIR /code

您要创建的是容器映像,而不是VM。这是一个类似但非常不同的概念,不应混为一谈。


# copy all the python requirements stored in requirements.txt into the new directoy (in the VM)

COPY ./requirements.txt /code/requirements.txt

这会将所有文件复制到镜像


# activate the package manager pip. But why use no-cache-dir?

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

运行是一个映像构建步骤,结果将提交给Docker映像。因此,在本步骤中,您将告诉docker,您需要一个映像,该映像按照requirements.txtpip中的说明安装了所有内容。没有缓存,默认情况下,PIP保存您正在安装的包的WHL,但这只会增加映像,不再需要。所以,没有缓存。


# copy all files to the new directory (in the VM)

COPY ./ /code/

同样,不是VM而是IMAGE,这是一个稍后将用于创建容器的镜像。


# I don't understand the line below. Please explain? why uvicorn? app.main:app is the

 #location of the fastapi
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "180"]

因为您正在尝试运行一个FastAPI项目,而FastAPI只是一个应用程序;您需要一台服务器才能真正触发对FastAPI的请求。This is explained on the very first page of the FastAPI documentation actually.

fjaof16o

fjaof16o2#

“app.main:app”EXPRESS您的项目有这样的python文件:

<Project Root Dir>
   app - folder
     main.py -- python file

main.py中,初始化一个FastAPI示例并命名为app,如下所示:


# main.py

....
app = FastAPI()
...

unicorn使用上述规则获取FastAPI示例app,然后加载它。

相关问题