Skip to content

Commit

Permalink
Add function to build docker from in-memory dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-grodek-dsai committed Sep 20, 2023
1 parent c5b8c67 commit 88455be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
27 changes: 27 additions & 0 deletions libs/langchain/langchain/utilities/docker_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ def from_dockerfile(

return cls(name=img_name)

@classmethod
def from_dockerfile_content(
cls,
dockerfile_str: str,
name: Union[str, Callable[[], str]] = generate_langchain_container_tag,
**kwargs: Any,
) -> "DockerImage":
"""Build a new image from Dockerfile given a string with Dockerfile content."""

img_name = (
name
if isinstance(name, str) and name
else generate_langchain_container_tag()
)
import io

buff = io.BytesIO(dockerfile_str.encode("utf-8"))

docker_client = get_docker_client()
from pathlib import Path

docker_client.images.build(
fileobj=buff, tag=img_name, rm=True, path=str(Path.cwd()), **kwargs
)

return cls(name=img_name)


class DockerContainer:
"""An isolated environment for running commands, based on docker container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def test_build_image_from_dockerfile_dirpath() -> None:
run_container_cowsay(image)


@pytest.mark.requires("docker")
def test_build_image_from_dockerfile_string_content() -> None:
dockerfile_str = "FROM alpine\nRUN touch /animal.txt"
DockerImage.from_dockerfile_content(dockerfile_str)


@pytest.mark.requires("docker")
def test_docker_spawn_run_works() -> None:
container = DockerContainer(DockerImage.from_tag("alpine"))
Expand Down

0 comments on commit 88455be

Please sign in to comment.