Reputation: 143
I'm trying to replicate this command with the Docker SDK for Python:
docker build -f path/to/dockerfile/Dockerfile.name -t image:version path/to/context/.
path/to/dockerfile and path/to/context are different paths, ie: /opt/project/dockerfile and /opt/project/src/app/.
The directory structure is the following:
opt
├── project
│ ├── dockerfile
│ │ └── Dockerfile.name
│ └── src
│ └── app
│ └── target
│ └── app-0.0.1-SNAPSHOT.jar
└── script.py
The command is working correctly from the CLI, but I'm not able to make it work with the SDK.
From the documentation, the images build method has the following parameters:
When I use the method like this:
client.images.build(
path = path_to_context,
fileobj=open(path_to_file, 'rb'),
custom_context=True,
tag='image:version'
)
I get this error:
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'}
The content of the Dockerfile is the following:
FROM openjdk:16-alpine
COPY target/app-0.0.1-SNAPSHOT.jar app.jar
CMD ["java", "-jar", "-Dspring.profiles.active=docker", "/app.jar"]
but I'm guessing the error is not due to that for the command correctly works with the CLI, it's only breaking with the SDK.
Am I doing something wrong?
Thanks!
Edit:
Simply removing custom_context=True does not solve the problem, for the context and build paths are different. In fact it causes another error, relative to the fact that the file does not exist in the current path:
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
Upvotes: 6
Views: 3632
Reputation: 143
Even if the documentation refers to the "path within the build context to the Dockerfile", it works for a Dockerfile outside the build context if an absolute path is specified.
Using my project tree:
client.images.build(
path = '/opt/project/src/',
dockerfile = '/opt/project/dockerfile/Dockerfile.name',
tag = 'image:version'
)
Upvotes: 5
Reputation: 116
I removed custom_context=True,
and the problem went away.
EDIT:
Using your project tree:
import docker
client = docker.from_env()
client.images.build(
path = './project/src/app/target/',
dockerfile = '../../../Dockerfile/Dockerfile.name',
tag='image:version',
)
Upvotes: 2