为Python项目构建Docker映像时如何避免重新安装软件包?

为Python项目构建Docker映像时如何避免重新安装软件包?,第1张

为Python项目构建Docker映像时如何避免重新安装软件包?

尝试使用以下Dockerfile进行构建。

FROM my/baseWORKDIR /srvADD ./requirements.txt /srv/requirements.txtRUN pip install -r requirements.txtADD . /srvRUN python setup.py installENTRYPOINT ["run_server"]

如果

.
(您的项目)有某些更改,则docker
pip install
通过使用缓存跳过行。

Docker仅

pip install
在您编辑requirements.txt文件时在构建上运行。


我写简单的

Hello, World!
程序。

$ tree.├── Dockerfile├── requirements.txt└── run.py0 directories, 3 file# DockerfileFROM dockerfile/pythonWORKDIR /srvADD ./requirements.txt /srv/requirements.txtRUN pip install -r requirements.txtADD . /srvCMD python /srv/run.py# requirements.txtpytest==2.3.4# run.pyprint("Hello, World")

下面是输出

Step 1 : WORKDIR /srv---> Running in 22d725d22e10---> 55768a00fd94Removing intermediate container 22d725d22e10Step 2 : ADD ./requirements.txt /srv/requirements.txt---> 968a7c3a4483Removing intermediate container 5f4e01f290fdStep 3 : RUN pip install -r requirements.txt---> Running in 08188205e92bDownloading/unpacking pytest==2.3.4 (from -r requirements.txt (line 1))  Running setup.py (path:/tmp/pip_build_root/pytest/setup.py) egg_info for package pytest....Cleaning up...---> bf5c154b87c9Removing intermediate container 08188205e92bStep 4 : ADD . /srv---> 3002a3a67e72Removing intermediate container 83defd1851d0Step 5 : CMD python /srv/run.py---> Running in 11e69b887341---> 5c0e7e3726d6Removing intermediate container 11e69b887341Successfully built 5c0e7e3726d6

我只更新run.py并尝试再次构建。

# run.pyprint("Hello, Python")

下面是输出。

Sending build context to Docker daemon  5.12 kBSending build context to Docker daemon Step 0 : FROM dockerfile/python---> f86d6993fc7bStep 1 : WORKDIR /srv---> Using cache---> 55768a00fd94Step 2 : ADD ./requirements.txt /srv/requirements.txt---> Using cache---> 968a7c3a4483Step 3 : RUN pip install -r requirements.txt---> Using cache---> bf5c154b87c9Step 4 : ADD . /srv---> 9cc7508034d6Removing intermediate container 0d7cf71eb05eStep 5 : CMD python /srv/run.py---> Running in f25c21135010---> 4ffab7bc66c7Removing intermediate container f25c21135010Successfully built 4ffab7bc66c7

正如您在上面看到的,Docker使用构建缓存。这次我更新了requirements.txt。

# requirements.txtpytest==2.3.4ipython

下面是输出。

Sending build context to Docker daemon  5.12 kBSending build context to Docker daemon Step 0 : FROM dockerfile/python---> f86d6993fc7bStep 1 : WORKDIR /srv---> Using cache---> 55768a00fd94Step 2 : ADD ./requirements.txt /srv/requirements.txt---> b6c19f0643b5Removing intermediate container a4d9cb37dff0Step 3 : RUN pip install -r requirements.txt---> Running in 4b7a85a64c33Downloading/unpacking pytest==2.3.4 (from -r requirements.txt (line 1))  Running setup.py (path:/tmp/pip_build_root/pytest/setup.py) egg_info for package pytestDownloading/unpacking ipython (from -r requirements.txt (line 2))Downloading/unpacking py>=1.4.12 (from pytest==2.3.4->-r requirements.txt (line 1))  Running setup.py (path:/tmp/pip_build_root/py/setup.py) egg_info for package pyInstalling collected packages: pytest, ipython, py  Running setup.py install for pytestInstalling py.test script to /usr/local/binInstalling py.test-2.7 script to /usr/local/bin  Running setup.py install for pySuccessfully installed pytest ipython pyCleaning up...---> 23a1af3df8edRemoving intermediate container 4b7a85a64c33Step 4 : ADD . /srv---> d8ae270eca35Removing intermediate container 7f003ebc3179Step 5 : CMD python /srv/run.py---> Running in 510359cf9e12---> e42fc9121a77Removing intermediate container 510359cf9e12Successfully built e42fc9121a77

码头工人不使用构建缓存。如果它不起作用,请检查您的码头工人版本。

Client version: 1.1.2Client API version: 1.13Go version (client): go1.2.1Git commit (client): d84a070Server version: 1.1.2Server API version: 1.13Go version (server): go1.2.1Git commit (server): d84a070


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5047295.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存