[英]Docker SDK for Python: how to build an image with custom Dockerfile AND custom context
我正在嘗試使用 Docker SDK 為 Python 復制此命令:
docker build -f path/to/dockerfile/Dockerfile.name -t image:version path/to/context/.
path/to/dockerfile和path/to/context是不同的路徑,即: /opt/project/dockerfile和/opt/project/src/app/。
目錄結構如下:
opt
├── project
│ ├── dockerfile
│ │ └── Dockerfile.name
│ └── src
│ └── app
│ └── target
│ └── app-0.0.1-SNAPSHOT.jar
└── script.py
該命令在 CLI 中正常工作,但我無法使其與 SDK 一起工作。
從文檔中,圖像構建方法具有以下參數:
當我使用這樣的方法時:
client.images.build(
path = path_to_context,
fileobj=open(path_to_file, 'rb'),
custom_context=True,
tag='image:version'
)
我收到此錯誤:
Traceback (most recent call last):
File "script.py", line 33, in <module>
client.images.build(
File "/Library/Python/3.8/site-packages/docker/models/images.py", line 298, in build
raise BuildError(last_event or 'Unknown', result_stream)
docker.errors.BuildError: {'message': 'unexpected EOF'}
Dockerfile的內容如下:
FROM openjdk:16-alpine
COPY target/app-0.0.1-SNAPSHOT.jar app.jar
CMD ["java", "-jar", "-Dspring.profiles.active=docker", "/app.jar"]
但我猜該錯誤不是因為該命令與 CLI 一起正常工作,它只是與 SDK 中斷有關。
難道我做錯了什么?
謝謝!
編輯:
簡單地刪除custom_context=True並不能解決問題,因為上下文和構建路徑不同。 事實上,它會導致另一個錯誤,相對於文件在當前路徑中不存在的事實:
Traceback (most recent call last):
File "script.py", line 33, in <module>
client.images.build(
File "/Library/Python/3.8/site-packages/docker/models/images.py", line 287, in build
raise BuildError(chunk['error'], result_stream)
docker.errors.BuildError: COPY failed: file not found in build context or excluded by .dockerignore: stat target/app-0.0.1-SNAPSHOT.jar: file does not exist
我刪除custom_context=True,
問題就消失了。
編輯:
使用您的項目樹:
import docker
client = docker.from_env()
client.images.build(
path = './project/src/app/target/',
dockerfile = '../../../Dockerfile/Dockerfile.name',
tag='image:version',
)
即使文檔提到“構建上下文中到 Dockerfile 的路徑” ,如果指定了絕對路徑,它也適用於構建上下文之外的 Dockerfile。
使用我的項目樹:
client.images.build(
path = '/opt/project/src/',
dockerfile = '/opt/project/dockerfile/Dockerfile.name',
tag = 'image:version'
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.