'Creating a buildx multi-arch container from a multi-arch base
I've been able to build a Docker container from a Go application (using the Beego framework and Bee tool) for each of my 3 target architectures: amd64, arm64 and arm/v7. And, I've been able to build a multi-arch container using docker manifest, building each architecture's container with the appropriate tags and then creating and pushing a manifest. So far, so good. But now I'm hoping to use buildx -- to do it all from one machine, and I can't seem to get the Dockerfile worked out. Here's my workflow for a single container build:
cd ~/go/src/github.com/bnhf/pivpn-tap-web-ui
go mod tidy
bee pack -exr='^vendor|^data.db|^build|^README.md|^docs'
cd build
./build.sh
"bee pack" builds my executable and creates a tar.gz archive for the whole application "build.sh" looks like this:
#!/bin/bash
set -e
PKGFILE=pivpn-tap-web-ui.tar.gz
cp -f ../$PKGFILE ./
docker build -t bnhf/pivpn-tap-web-ui:amd64 .
rm -f $PKGFILE
And my Dockerfile:
FROM debian:bullseye
WORKDIR /opt
EXPOSE 8080
RUN apt-get update && apt-get install -y easy-rsa
RUN chmod 755 /usr/share/easy-rsa/*
ADD assets/start.sh /opt/start.sh
ADD assets/generate_ca_and_server_certs.sh /opt/scripts/generate_ca_and_server_certs.sh
ADD assets/vars.template /opt/scripts/
ADD pivpn-tap-web-ui.tar.gz /opt/openvpn-gui-tap/
RUN rm -f /opt/openvpn-gui-tap/data.db
ADD assets/app.conf /opt/openvpn-gui-tap/conf/app.conf
CMD /opt/start.sh
In order to make this work, it seems like my current pre-container-build workflow needs to happen inside the container so buildx can perform its magic. So, I built a multi-arch container to use as a base with Go, Beego, Bee and Git installed. And, I've successfully built a multi-arch version of my original workflow that can create multi-arch versions of the tar.gz file containing my complete application.
The Dockerfile for that build:
FROM bnhf/go-beego-bee-git
WORKDIR /go/src/github.com/bnhf
RUN git clone https://github.com/bnhf/pivpn-tap-web-ui
WORKDIR /go/src/github.com/bnhf/pivpn-tap-web-ui
RUN go mod tidy
RUN bee pack -exr='^vendor|^data.db|^build|^README.md|^docs'
Using this buildx:
docker buildx build --platform linux/amd64,linux/arm64 -f build/Multi-arch.dockerfile -t bnhf/pivpn-tap-web-ui-test . --push
So, the question is -- how do I proceed from here? I don't want to finish building using that same base, because it's too large. Some sort of multi-stage build?
Edit:
As mentioned in the comments, I believe I've worked it out. Here's the final Dockerfile, which successfully built multi-arch:
FROM bnhf/go-beego-bee-git
WORKDIR /go/src/github.com/bnhf
RUN git clone https://github.com/bnhf/pivpn-tap-web-ui
WORKDIR /go/src/github.com/bnhf/pivpn-tap-web-ui
RUN go mod tidy
RUN bee pack -exr='^vendor|^data.db|^build|^README.md|^docs'
FROM debian:bullseye
WORKDIR /opt
EXPOSE 8080
RUN apt-get update && apt-get install -y easy-rsa
RUN chmod 755 /usr/share/easy-rsa/*
COPY --from=0 /go/src/github.com/bnhf/pivpn-tap-web-ui/build/assets/start.sh /opt/start.sh
COPY --from=0 /go/src/github.com/bnhf/pivpn-tap-web-ui/build/assets/generate_ca_and_server_certs.sh /opt/scripts/generate_ca_and_server_certs.sh
COPY --from=0 /go/src/github.com/bnhf/pivpn-tap-web-ui/build/assets/vars.template /opt/scripts/
COPY --from=0 /go/src/github.com/bnhf/pivpn-tap-web-ui/pivpn-tap-web-ui.tar.gz /opt/openvpn-gui-tap/
RUN rm -f /opt/openvpn-gui-tap/data.db
COPY --from=0 /go/src/github.com/bnhf/pivpn-tap-web-ui/build/assets/app.conf /opt/openvpn-gui-tap/conf/app.conf
CMD /opt/start.sh
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
