@@ -159,6 +159,58 @@ private static List<String> getPaths(String entryPoint) {
159159 return new ArrayList <>(res );
160160 }
161161
162+ /**
163+ * Login into dockerhub with the given credentials.
164+ *
165+ * @param registry Docker registry URL
166+ * @param username Docker registry username
167+ * @param password Docker registry password
168+ * @return True if login was successful, false otherwise
169+ * @throws IOException IO exception
170+ */
171+ public static boolean login (String registry , String username , String password ) throws IOException {
172+ if (StringUtils .isEmpty (username ) || StringUtils .isEmpty (password )) {
173+ throw new IllegalArgumentException ("Username and password are required" );
174+ }
175+ if (registry == null ) {
176+ // Leave it empty (defaults to dockerhub)
177+ registry = "" ;
178+ }
179+
180+ String commandLine = "docker login " + registry + " --username " + username + " --password-stdin" ;
181+
182+ LOGGER .info ("Running docker login command" );
183+ Command cmd = new Command (commandLine , password );
184+ cmd .run ();
185+
186+ if (cmd .getExitValue () != 0 ) {
187+ LOGGER .error ("Docker login failed: {}" , cmd .getError ());
188+ return false ;
189+ }
190+
191+ return true ;
192+ }
193+
194+ /**
195+ * Create and run the command line to execute the docker image with optional registry authentication.
196+ *
197+ * @param image Docker image name
198+ * @param inputBindings Array of bind mounts for docker input volumes (source-target)
199+ * @param outputBinding Bind mount for docker output volume (source-target)
200+ * @param cmdParams Image command parameters
201+ * @param dockerParams Docker parameters
202+ * @param registry Docker registry URL
203+ * @param username Optional Docker registry username
204+ * @param password Optional Docker registry password
205+ * @return The command line
206+ * @throws IOException IO exception
207+ */
208+ public static String run (String image , List <AbstractMap .SimpleEntry <String , String >> inputBindings ,
209+ AbstractMap .SimpleEntry <String , String > outputBinding , String cmdParams ,
210+ Map <String , String > dockerParams , String registry , String username , String password ) throws IOException {
211+ return run (image , inputBindings , outputBinding != null ? Collections .singletonList (outputBinding ) : null ,
212+ cmdParams , dockerParams , registry , username , password );
213+ }
162214
163215 /**
164216 * Create and run the command line to execute the docker image.
@@ -191,8 +243,37 @@ public static String run(String image, List<AbstractMap.SimpleEntry<String, Stri
191243 public static String run (String image , List <AbstractMap .SimpleEntry <String , String >> inputBindings ,
192244 List <AbstractMap .SimpleEntry <String , String >> outputBindings , String cmdParams ,
193245 Map <String , String > dockerParams ) throws IOException {
246+ return run (image , inputBindings , outputBindings , cmdParams , dockerParams , null , null , null );
247+ }
248+
249+ /**
250+ * Create and run the command line to execute the docker image with optional registry authentication.
251+ *
252+ * @param image Docker image name
253+ * @param inputBindings Array of bind mounts for docker input volumes (source-target)
254+ * @param outputBindings Array of bind mount for docker output volume (source-target)
255+ * @param cmdParams Image command parameters
256+ * @param dockerParams Docker parameters
257+ * @param registry Optional Docker registry URL (null if not using private registry)
258+ * @param username Optional Docker registry username
259+ * @param password Optional Docker registry password
260+ * @return The command line
261+ * @throws IOException IO exception
262+ */
263+ public static String run (String image , List <AbstractMap .SimpleEntry <String , String >> inputBindings ,
264+ List <AbstractMap .SimpleEntry <String , String >> outputBindings , String cmdParams ,
265+ Map <String , String > dockerParams , String registry , String username , String password ) throws IOException {
266+
194267 checkDockerDaemonAlive ();
195268
269+ // Login to registry if credentials provided
270+ if (StringUtils .isNotEmpty (username ) && StringUtils .isNotEmpty (password )) {
271+ boolean loginSuccess = login (registry , username , password );
272+ if (!loginSuccess ) {
273+ throw new IOException ("Failed to authenticate to Dockerhub" );
274+ }
275+ }
276+
196277 String commandLine = buildCommandLine (image , inputBindings , outputBindings , cmdParams , dockerParams );
197278
198279 LOGGER .info ("Run docker command line" );
@@ -222,7 +303,6 @@ public static String run(String image, List<AbstractMap.SimpleEntry<String, Stri
222303 return commandLine ;
223304 }
224305
225-
226306 public static void checkDockerDaemonAlive () throws IOException {
227307 int maxAttempts = 12 ;
228308 for (int i = 0 ; i < maxAttempts ; i ++) {
0 commit comments