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'
)
Answer from Marco Miduri on Stack OverflowEven 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'
)
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',
)
Videos
Currently I'm using Dockerfiles to build docker images, but I want to parameterize the building process so I'm trying to write a python script that takes as input a folder and dockerizes. I checked the docker-py library but it only supports managing docker images and containers, not building new ones. Is there another library or another way to dockerize directories by automatically creating the required Dockerfile?
From doc here, you can build an docker image and tag its using "tag" parameter.
tag (str) – A tag to add to the final image
client.images.build(path="./", tag={image_tag})
this code above actually like you are typing docker build -t {image_tag} . command in the docker cli.
then, using the specific image tag to run the docker container.
client.containers.run({image_tag}, name={container_name}, detach=True)
I was able to pull out the ID from the image, and then pass that in when creating the container.
import docker
import re
def main():
folder_path = './node-sample'
client = docker.from_env()
try:
image = client.images.build(path=folder_path)[0]
image_id = re.sub(r'(sha256:)', '', image.short_id)
client.containers.run(image_id, detach=True, ports={'8080/tcp': 8080})
except RuntimeError as e:
print e
if __name__ == '__main__':
main()