Dockerfile optimization for cross-platform image generation process with buildx

Hi everyone, I have some projects with an angularjs front and I’m working on porting the application to ARM and trying to reduce cloud costs. Installing Docker Desktop on Windows 10 I managed to make use of docker buildx to generate images for ARM64 and AMD64, the problem is the following.

I have to run several docker buildx which in the Dockerfile only changes the js and html generation command: “npm run build-{project-name}-app:prod”, in the build phase and takes about 25 minutes when I compile for the 2 architectures, however its output on AMD/x86 or ARM is the same, several different js files.

Is there any way this command can be executed only once instead of 2 and its layer copied to the second architecture?

Here’s my Dockerfile:

##############################################################################

FROM node:14.17.5-alpine3.14 AS build
WORKDIR /app
RUN npm install -g @angular/cli
COPY package*.json ./
RUN npm install --save ngx-jdenticon jdenticon
RUN npm install
COPY . .

RUN npm run  build-{project-name}-app:prod # Each {project-name} is the name of one of the projects with its distinct Dockerfile

FROM nginx:1.21.1-alpine
COPY --from=build /app/dist/{project-name}app/pt-BR/pt-BR/* /usr/share/nginx/html/
COPY --from=build /app/dist/{project-name}-app/pt-BR/pt-BR /usr/share/nginx/html/pt-BR
COPY --from=build /app/dist/{project-name}-app/en-US/en-US /usr/share/nginx/html/en-US

EXPOSE 80

##############################################################################

Any help is welcome, otherwise I’m thinking about aborting the idea of ​​using the buildx docker, because in addition to having a Windows VM (I couldn’t get it to work in an Ubuntu VM), it’s taking more than twice the execution time in one runner shared on Gitlab. =(

mod update: wrapped Dockerfile content in a code block

Please, use the </> button to highlight codes.

As for the Dockerfile you can try to use the --platform parameter after the FROM keyword.

docker build -t amd64app -f Dockerfile.amd64 .

Dockerfile.arm64

FROM nginx:1.21.1-alpine
COPY --from=amd64app /usr/share/nginx/html /usr/share/nginx/html

EXPOSE 80
docker build -t arm64app -f Dockerfile.arm64 .
1 Like