10
10
11
11
from lib .base_logger import logger
12
12
13
+ DEFAULT_BUILDER_NAME = "multiarch" # Default buildx builder name
14
+
13
15
14
16
def ecr_login_boto3 (region : str , account_id : str ):
15
17
"""
@@ -38,16 +40,18 @@ def ecr_login_boto3(region: str, account_id: str):
38
40
logger .debug (f"ECR login succeeded: { status } " )
39
41
40
42
41
- def ensure_buildx_builder (builder_name : str = "multiarch" ) -> str :
43
+ def ensure_buildx_builder (builder_name : str = DEFAULT_BUILDER_NAME ) -> str :
42
44
"""
43
45
Ensures a Docker Buildx builder exists for multi-platform builds.
46
+ This function is safe for concurrent execution across multiple processes.
44
47
45
48
:param builder_name: Name for the buildx builder
46
49
:return: The builder name that was created or reused
47
50
"""
48
51
49
52
docker_cmd = python_on_whales .docker
50
53
54
+ logger .info (f"Ensuring buildx builder '{ builder_name } ' exists..." )
51
55
existing_builders = docker_cmd .buildx .list ()
52
56
if any (b .name == builder_name for b in existing_builders ):
53
57
logger .info (f"Builder '{ builder_name } ' already exists – reusing it." )
@@ -63,14 +67,27 @@ def ensure_buildx_builder(builder_name: str = "multiarch") -> str:
63
67
)
64
68
logger .info (f"Created new buildx builder: { builder_name } " )
65
69
except DockerException as e :
70
+ # Check if this is a race condition (another process created the builder)
71
+ if hasattr (e , 'stderr' ) and 'existing instance for' in str (e .stderr ):
72
+ logger .info (f"Builder '{ builder_name } ' was created by another process – using it." )
73
+ docker .buildx .use (builder_name )
74
+ return builder_name
75
+
76
+ # Otherwise, it's a real error
66
77
logger .error (f"Failed to create buildx builder: { e } " )
67
78
raise
68
79
69
80
return builder_name
70
81
71
82
72
83
def execute_docker_build (
73
- tag : str , dockerfile : str , path : str , args : Dict [str , str ], push : bool , platforms : list [str ]
84
+ tag : str ,
85
+ dockerfile : str ,
86
+ path : str , args :
87
+ Dict [str , str ],
88
+ push : bool ,
89
+ platforms : list [str ],
90
+ builder_name : str = DEFAULT_BUILDER_NAME ,
74
91
):
75
92
"""
76
93
Build a Docker image using python_on_whales and Docker Buildx for multi-architecture support.
@@ -83,6 +100,7 @@ def execute_docker_build(
83
100
:param platforms: List of target platforms (e.g., ["linux/amd64", "linux/arm64"])
84
101
"""
85
102
# Login to ECR before building
103
+ # TODO CLOUDP-335471: use env variables to configure AWS region and account ID
86
104
ecr_login_boto3 (region = "us-east-1" , account_id = "268558157000" )
87
105
88
106
docker_cmd = python_on_whales .docker
@@ -101,8 +119,8 @@ def execute_docker_build(
101
119
if len (platforms ) > 1 :
102
120
logger .info (f"Multi-platform build for { len (platforms )} architectures" )
103
121
104
- # We need a special driver to handle multi-platform builds
105
- builder_name = ensure_buildx_builder ("multiarch" )
122
+ # Ensure buildx builder exists (safe for concurrent execution)
123
+ ensure_buildx_builder (builder_name )
106
124
107
125
# Build the image using buildx
108
126
docker_cmd .buildx .build (
0 commit comments