aws / aws/aws-cdk

CodeBuild: Allow DockerImageAssets to be built using buildx

Open
#28,517 12 comments 18 reactions 0 assignees View on GitHub
@aws-cdk/aws-codebuild effort/medium feature-request p3
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

We make use of a CDK [`CodePipeline`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines.CodePipeline.html) 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](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecr_assets.DockerImageAsset.html#platform) 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](https://github.com/aws/aws-codebuild-docker-images/issues/625).

### 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

Contributor guide

Open the contributing guide

Research direction

Start with the DockerImageAsset construct, its existing platform property, and the Docker build invocation shown in the CodeBuild error. Compare that path with the Dockerfile and CDK example in the issue, then determine how buildx or native ARM builds should be selected. Done means an ARM DockerImageAsset can build successfully through CodePipeline and CodeBuild.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, docker, typescript
Domain
build-system, cloud, devops
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.