@@ -137,20 +137,39 @@ def start(self) -> 'Container':
137137 return self
138138
139139 def _pull_or_get_image (self ) -> None :
140- try :
141- self ._docker_client .images .pull (self ._image_name , self ._image_tag )
142- except errors .APIError as e :
143- self ._handle_image_pull_error (e )
144-
145- def _handle_image_pull_error (self , error ):
140+ """Pull Docker image or use local image if pulling fails."""
146141 image_name = f"{ self ._image_name } :{ self ._image_tag } "
142+
143+ # First check if the image is already available locally
147144 try :
148145 self ._docker_client .images .get (image_name )
146+ logging .info ("Using locally available image: %s" , image_name )
147+ return
149148 except errors .ImageNotFound :
150- raise RuntimeError (
151- f'Could not pull image and no local image available: { error } ' ) from error
152-
153- logging .warning ("Warning: Could not pull image: %s. Using locally cached image." , error )
149+ logging .info ("Image not found locally, attempting to pull: %s" , image_name )
150+
151+ # Try to pull the image
152+ try :
153+ # Pull image with default timeout settings
154+ self ._docker_client .api .pull (
155+ self ._image_name ,
156+ tag = self ._image_tag
157+ )
158+ except (
159+ errors .APIError ,
160+ requests .exceptions .Timeout ,
161+ requests .exceptions .ConnectionError
162+ ) as e :
163+ # Try to use locally cached image as fallback
164+ try :
165+ self ._docker_client .images .get (image_name )
166+ logging .warning (
167+ "Warning: Failed to pull image: %s. Using locally cached image." , e
168+ )
169+ except errors .ImageNotFound :
170+ raise RuntimeError (
171+ f'Could not pull image { image_name } and no local image available: { e } '
172+ ) from e
154173
155174 def stop (self ) -> None :
156175 self ._stop_and_remove_container ()
0 commit comments