Skip to content

CodeBuild: Allow DockerImageAssets to be built using buildx #28517

@James-Coulson

Description

@James-Coulson

Describe the feature

We make use of a CDK CodePipeline construct to build and deploy our backend infrastructure to the development and production accounts. As part of our backend we require the ability to run Dockerised applications as a Service in ECS, specifically these services are built using Java (Corretto 21) and run on the ARM based t4g instances. This requires that the docker images be built using the ARM instructuction set, however, when we specify the docker images within the CDK pipeline including the desired 'linus/arm' platform CodeBuild fails to build the applications (using the existing platform property in the DockerImageAsset construct). Specifically, when CodeBuild attempts to build the image a format exec error is thrown.

Dockerfile

# ---- Build ---- #

# Set up build image
FROM maven:3.9.6-amazoncorretto-21 as BUILDER
WORKDIR /build

# Copy source files to project
COPY . .

# Build the application
RUN mvn -v
RUN mvn clean package -pl app -am -DskipTests -e

# ---- Package ---- #

# Create the runtime image
FROM amazoncorretto:21-alpine-jdk

# Copy the built jar file to the runtime image
COPY --from=BUILDER /build/app/target/app-1.0-SNAPSHOT-jar-with-dependencies.jar /app/app.jar

# Set the entrypoint
CMD ["java", "-jar", "/app/app.jar"]

CDK Code

new DockerImageAsset(this, "AppImage", {
  directory: path.join(__dirname, "../../src/java"),
  file: "./app/Dockerfile",
  platform: Platform.LINUX_ARM65
})

Error in CodeBuild

#10 [builder 4/4] RUN mvn clean package -pl app -am -DskipTests -e
--
121 | #10 0.286 exec /bin/sh: exec format error
122 | #10 ERROR: process "/bin/sh -c mvn clean package -pl app -am -DskipTests -e" did not complete successfully: exit code: 1
123 | ------
124 | > [builder 4/4] RUN mvn clean package -pl app -am -DskipTests -e:
125 | 0.286 exec /bin/sh: exec format error
126 | ------
127 | Dockerfile:11
128 | --------------------
129 | 9  \|
130 | 10 \|     # Build the application
131 | 11 \| >>> RUN mvn clean package -pl app -am -DskipTests -e
132 | 12 \|
133 | 13 \|     # ---- Package ---- #
134 | --------------------
135 | ERROR: failed to solve: process "/bin/sh -c mvn clean package -pl app -am -DskipTests -e" did not complete successfully: exit code: 1
136 | error  : [100%] fail: docker build --tag cdkasset-b3423c72c3adc61b0db07a435e34472b82421b9285c81c14069150311f188aab --file ./app/Dockerfile --platform linux/arm64 . exited with error code 1: #0 building with "default" instance using docker driver

This docker image can be built successfully locally (Mac i7) as I believe that the buildx plugin is used by default for Docker Desktop. Upon reading through the documentation for the DockerImageAsset it can be seen that the use of the platform property is dependent on building using the buildx however I have not been able to locate how we can force the build stage to use this (the above error message shows that only docker build ... is being used).

As such this feature request is to add the ability to build a DockerImageAsset using buildx to allow for cross architecture building of Dockerised applications.

If there is already a method for using buildx or this is simply a case of user error/RTFM please let me know.

Note
It is my understanding that the Docker buildx package which allows for cross architecture compliation to be performed is included within the standard:7.0 codebuild image so ther should be no issue relating to having to install the application see here.

Use Case

By introducing this feature it will allow for ARM based dockerised applications to be built using the DockerImageAsset construct within an CodePipeline.

Proposed Solution

  1. Introduce a new property within the DockerImageAsset construct to allow for building the image using the buildx plugin.
  2. A new property for the DockerImageAsset to build the image natively on an ARM based instance.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.108.0

Environment details (OS name and version, etc.)

Mac i7, AWS CodeBuild

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-codebuildRelated to AWS CodeBuildeffort/mediumMedium work item – several days of effortfeature-requestA feature should be added or improved.p3

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions