1919import java .nio .file .Paths ;
2020import java .nio .file .StandardCopyOption ;
2121import java .text .MessageFormat ;
22+ import java .util .ArrayList ;
2223import java .util .Arrays ;
2324import java .util .Base64 ;
2425import java .util .Collection ;
@@ -422,20 +423,24 @@ public static boolean isEmptyString(String s) {
422423 /**
423424 * Reads the docker image environment variables into Java Properties.
424425 *
425- * @param builder the binary to create the container (like docker)
426- * @param dockerImage the name of the Docker image to read from
427- * @param script the script resource (path to the script in the JAR)
428- * @param contextDir the image build context folder
426+ * @param builder the binary to create the container (like docker)
427+ * @param dockerImage the name of the Docker image to be run
428+ * @param imagePlatform the platform of the Docker image to be run
429+ * @param script the script resource (path to the script in the JAR)
430+ * @param contextDir the image build context folder
429431 * @return The key/value pairs representing the ENV of the Docker image
430432 * @throws IOException when the Docker command fails
431433 * @throws InterruptedException when the Docker command is interrupted
432434 */
433- public static Properties getBaseImageProperties (String builder , String dockerImage , String script ,
435+ public static Properties getBaseImageProperties (String builder ,
436+ String dockerImage ,
437+ String imagePlatform ,
438+ String script ,
434439 String contextDir ) throws IOException , InterruptedException {
435- logger .entering (builder , dockerImage , script , contextDir );
440+ logger .entering (builder , dockerImage , imagePlatform , script , contextDir );
436441 final String scriptToRun = "test-env.sh" ;
437442 Utils .copyResourceAsFile (script , contextDir + File .separator + scriptToRun );
438- List <String > imageEnvCmd = Utils .getDockerRunCmd (builder ,
443+ List <String > imageEnvCmd = Utils .getDockerRunCmd (builder , imagePlatform ,
439444 contextDir + File .separator + scriptToRun , dockerImage );
440445 logger .info ("IMG-0097" , dockerImage );
441446 Properties result = Utils .runDockerCommand (imageEnvCmd );
@@ -447,12 +452,13 @@ public static Properties getBaseImageProperties(String builder, String dockerIma
447452 * Constructs a docker command to run a script in the container with a volume mount.
448453 *
449454 * @param builder docker/podman executable
455+ * @param imagePlatform the platform for selecting the image (multi-arch images)
450456 * @param scriptToRun the local script to encode and run
451457 * @param dockerImage docker image tag
452458 * @return command
453459 */
454- private static List <String > getDockerRunCmd (String builder , String scriptToRun , String dockerImage )
455- throws IOException {
460+ private static List <String > getDockerRunCmd (String builder , String imagePlatform , String scriptToRun ,
461+ String dockerImage ) throws IOException {
456462
457463 // We are removing the volume mount option, -v won't work in remote docker daemon and also
458464 // problematic if the mounted volume source is on a nfs volume as we have no idea what the docker volume
@@ -463,11 +469,21 @@ private static List<String> getDockerRunCmd(String builder, String scriptToRun,
463469
464470 byte [] fileBytes = Files .readAllBytes (Paths .get (scriptToRun ));
465471 String encodedFile = Base64 .getEncoder ().encodeToString (fileBytes );
466- String oneCommand = String .format ("echo %s | base64 -d | /bin/sh" , encodedFile );
467- logger .finest ("running command in image [" + oneCommand + "]" );
468- return Stream .of (
469- builder , "run" , "--rm" ,
470- dockerImage , "/bin/sh" , "-c" , oneCommand ).collect (Collectors .toList ());
472+ String shellCommand = String .format ("echo %s | base64 -d | /bin/sh" , encodedFile );
473+ logger .finest ("running command in image [" + shellCommand + "]" );
474+ List <String > command = new ArrayList <>(10 );
475+ command .add (builder );
476+ command .add ("run" );
477+ if (imagePlatform != null ) {
478+ command .add ("--platform" );
479+ command .add (imagePlatform );
480+ }
481+ command .add ("--rm" );
482+ command .add (dockerImage );
483+ command .add ("/bin/sh" );
484+ command .add ("-c" );
485+ command .add (shellCommand );
486+ return command ;
471487 }
472488
473489 /**
0 commit comments