33#
44
55import json
6+ import logging
7+ import platform
68import uuid
7- from typing import Callable , Dict , List , Optional , Union
9+ from typing import Any , Callable , Coroutine , Dict , List , Optional , Union
810
911from dagger import Client , Container , File , Service
1012from dagger import Secret as DaggerSecret
@@ -56,13 +58,17 @@ def get_base_dockerd_container(dagger_client: Client) -> Container:
5658 )
5759 )
5860 # Expose the docker host port.
61+ .with_exec (["adduser" , "-u" , "1000" , "-S" , "-H" , "airbyte" ])
5962 .with_exposed_port (DOCKER_HOST_PORT )
6063 # We cache /tmp for file sharing between client and daemon.
61- .with_mounted_cache ("/tmp" , dagger_client .cache_volume (DOCKER_TMP_VOLUME_NAME ))
64+ .with_mounted_cache ("/tmp" , dagger_client .cache_volume (DOCKER_TMP_VOLUME_NAME ), owner = "airbyte" )
65+ .with_exec (["chmod" , "777" , "/tmp" ])
6266 )
6367
6468 # We cache /var/lib/docker to avoid downloading images and layers multiple times.
65- base_container = base_container .with_mounted_cache ("/var/lib/docker" , dagger_client .cache_volume (DOCKER_VAR_LIB_VOLUME_NAME ))
69+ base_container = base_container .with_mounted_cache (
70+ "/var/lib/docker" , dagger_client .cache_volume (DOCKER_VAR_LIB_VOLUME_NAME ), owner = "airbyte"
71+ )
6672 return base_container
6773
6874
@@ -75,8 +81,10 @@ def get_daemon_config_json(registry_mirror_url: Optional[str] = None) -> str:
7581 Returns:
7682 str: The json representation of the docker daemon config.
7783 """
84+ storage_driver = "vfs" if platform .system () == "Darwin" else STORAGE_DRIVER
85+ logging .info (f"Using storage driver: { storage_driver } " )
7886 daemon_config : Dict [str , Union [List [str ], str ]] = {
79- "storage-driver" : STORAGE_DRIVER ,
87+ "storage-driver" : storage_driver ,
8088 }
8189 if registry_mirror_url :
8290 daemon_config ["registry-mirrors" ] = ["http://" + registry_mirror_url ]
@@ -152,7 +160,7 @@ def with_global_dockerd_service(
152160 ).as_service ()
153161
154162
155- def with_bound_docker_host (
163+ async def with_bound_docker_host (
156164 context : ConnectorContext ,
157165 container : Container ,
158166) -> Container :
@@ -165,21 +173,22 @@ def with_bound_docker_host(
165173 Container: The container bound to the docker host.
166174 """
167175 assert context .dockerd_service is not None
176+ current_user = (await container .with_exec (["whoami" ]).stdout ()).strip ()
168177 return (
169178 container .with_env_variable ("DOCKER_HOST" , f"tcp://{ DOCKER_HOST_NAME } :{ DOCKER_HOST_PORT } " )
170179 .with_service_binding (DOCKER_HOST_NAME , context .dockerd_service )
171- .with_mounted_cache ("/tmp" , context .dagger_client .cache_volume (DOCKER_TMP_VOLUME_NAME ))
180+ .with_mounted_cache ("/tmp" , context .dagger_client .cache_volume (DOCKER_TMP_VOLUME_NAME ), owner = current_user )
172181 )
173182
174183
175- def bound_docker_host (context : ConnectorContext ) -> Callable [[Container ], Container ]:
176- def bound_docker_host_inner (container : Container ) -> Container :
177- return with_bound_docker_host (context , container )
184+ def bound_docker_host (context : ConnectorContext ) -> Callable [[Container ], Coroutine [ Any , Any , Container ] ]:
185+ async def bound_docker_host_inner (container : Container ) -> Container :
186+ return await with_bound_docker_host (context , container )
178187
179188 return bound_docker_host_inner
180189
181190
182- def with_docker_cli (context : ConnectorContext ) -> Container :
191+ async def with_docker_cli (context : ConnectorContext ) -> Container :
183192 """Create a container with the docker CLI installed and bound to a persistent docker host.
184193
185194 Args:
@@ -189,7 +198,7 @@ def with_docker_cli(context: ConnectorContext) -> Container:
189198 Container: A docker cli container bound to a docker host.
190199 """
191200 docker_cli = context .dagger_client .container ().from_ (consts .DOCKER_CLI_IMAGE )
192- return with_bound_docker_host (context , docker_cli )
201+ return await with_bound_docker_host (context , docker_cli )
193202
194203
195204async def load_image_to_docker_host (context : ConnectorContext , tar_file : File , image_tag : str ) -> str :
@@ -202,7 +211,7 @@ async def load_image_to_docker_host(context: ConnectorContext, tar_file: File, i
202211 """
203212 # Hacky way to make sure the image is always loaded
204213 tar_name = f"{ str (uuid .uuid4 ())} .tar"
205- docker_cli = with_docker_cli (context ).with_mounted_file (tar_name , tar_file )
214+ docker_cli = ( await with_docker_cli (context ) ).with_mounted_file (tar_name , tar_file )
206215
207216 image_load_output = await docker_cli .with_exec (["docker" , "load" , "--input" , tar_name ], use_entrypoint = True ).stdout ()
208217 # Not tagged images only have a sha256 id the load output shares.
0 commit comments