Use docker buildx Implement multi platform compilation
docker registry api v2
Support more CPU Schema mirroring .
meanwhile harbor v2
It has also been realized. docker registry api v2
Support for .
0x01 Get ready
docker
Turn on the experimental modebuildx
plug-in unitqemu
SimulatorLinux kernel >= 4.8
linux system kernel
because binfmt_misc
Mechanism opening needs to rely on Linux kernel >= 4.8
. therefore , In the face of linux There are certain requirements for system operation and selection .
It is recommended to use Distribution version The operating system that the demoted kernel has met the requirements . Instead of choosing to upgrade the system kernel yourself .
ubuntu:18.04 LTS
And abovedebian:10
And above
Reference article :
Turn on the experimental mode
At present buildx It's also an experimental model , If you need support , The following configuration is required
- Turn on the experimental mode . modify
/etc/docker/daemon.json
, increaseexperimental
Field , as follows .
# vi /etc/docker/daemon.json
{
"experimental": true
}
install buildx
- take
buildx
Put it in~/.docker/cli-plugins/
Under the table of contents
# https://github.com/docker/buildx/blob/master/README.md#docker-ce
BUILDX_VERSION=v0.4.1
ARCH=$(uname -m)
[ "${ARCH}" == "x86_64" ] && ARCH=amd64
[ "${ARCH}" == "aarch64" ] && ARCH=arm64
mkdir -p ~/.docker/cli-plugins
wget -c https://github.com/docker/buildx/releases/download/${BUILDX_VERSION}/buildx-${BUILDX_VERSION}.linux-${ARCH} -O docker-buildx\
&& chmod +x docker-buildx \
&& mv docker-buildx ~/.docker/cli-plugins/
install qemu
Download and install qumu
To /usr/bin/qemu-$(uname -m)-static
# https://github.com/multiarch/qemu-user-static#multiarchqemu-user-static-images
QEMU_VERSION=v5.0.0-2
wget -c https://github.com/multiarch/qemu-user-static/releases/download/${QEMU_VERSION}/qemu-$(uname -m)-static -O qemu-$(uname -m)-static \
&& chmod +x qemu-$(uname -m)-static \
&& mv qemu-$(uname -m)-static /usr/local/bin/qemu-$(uname -m)-static
0x02 example
establish buildx builder
## 1. Create the specified name and the specified platform builder
docker buildx create --use --platform=linux/amd64,linux/arm64 --name localbuilder
### Create random name of Default parameters builder
# docker buildx create --use
establish Dockerfile and bakefile
The directory structure is as follows
# tree
# .
# ├── alpine-bake
# │ └── alpine.Dockerfile
# └── bake.hcl
alpine-bake/alpine.Dockerfile
FROM alpine:3.12
ARG TARGETARCH
RUN echo "TARGETARCH is $TARGETARCH"
docker-bake.hcl
group "default" {
targets = ["alpine"]
}
target "alpine" {
context = "./alpine-bake"
dockerfile = "alpine.Dockerfile"
tags = ["docker.io/tangx/alpine:buildx-bake-hcl"]
platforms = ["linux/amd64", "linux/arm64", "linux/arm/v6", "linux/arm/v7", "linux/s390x"]
## push to registry
output = ["type=registry"]
## pull base image always
pull = true
}
target "debian" {
context = "./debian-bake"
## default: Dockerfile
# dockerfile = "Dockerfile"
tags = ["docker.io/tangx/debian:buildx-bake-hcl"]
platforms = ["linux/amd64", "linux/arm64", "linux/arm/v6", "linux/arm/v7", "linux/s390x"]
## push to registry
output = ["type=registry"]
## pull base image always
pull = true
}
Use bake
compiling
docker buildx bake -f bake.hcl # -f alias to bake
When omitted from the command -f
When parameters are , Default bake File for :
- docker-bake.json
- docker-bake.hcl
- docker-compose.yaml
Execution results
Can be in tangx/alpine:buildx-bake-hcl notice , The generated image , Support 5 individual cpu framework .
- Execution process
# docker buildx bake -f bake.hcl
[+] Building 46.9s (19/19) FINISHED
=> [internal] booting buildkit 3.3s
=> [internal] load build definition from alpine.Dockerfile 0.1s
=> [internal] load .dockerignore 0.1s
=> [linux/arm/v6 internal] load metadata for docker.io/library/alpine:3.12 13.1s
=> [linux/arm64 internal] load metadata for docker.io/library/alpine:3.12 10.4s
=> [linux/amd64 internal] load metadata for docker.io/library/alpine:3.12 13.1s
=> [linux/s390x internal] load metadata for docker.io/library/alpine:3.12 13.1s
=> [linux/arm/v7 internal] load metadata for docker.io/library/alpine:3.12 13.1s
=> [linux/amd64 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 6.1s
=> [linux/arm/v7 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 5.9s
=> [linux/arm/v6 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 6.0s
=> [linux/arm64 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 6.1s
=> [linux/s390x 1/2] FROM docker.io/library/alpine:3.12@sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a 6.3s
=> [linux/arm/v7 2/2] RUN echo "TARGETARCH is arm" 0.9s
=> [linux/arm/v6 2/2] RUN echo "TARGETARCH is arm" 0.8s
=> [linux/amd64 2/2] RUN echo "TARGETARCH is amd64" 0.7s
=> [linux/arm64 2/2] RUN echo "TARGETARCH is arm64" 0.5s
=> [linux/s390x 2/2] RUN echo "TARGETARCH is s390x" 0.4s
=> exporting to image 22.9s
Use the command line to perform compilation
# docker buildx build --platform=linux/amd64,linux/arm64 --file alpine-bake/alpine.Dockerfile --tag tangx/alpine:multi-arch --push alpine-bake/
Parameter interpretation
docker buildx build
: Master and sub commands--platform=linux/amd64,linux/arm64
: perform cpu framework--file
: AppointDockerfile
File and path of . Omission means In the current directoryDockerfile
--tag
: Mirror name and TAG--push
: Complete and push to warehousealpine-bake
: docker context Directory address of .
Execution results
A little
The related documents
buildx
- buildx README.md in
Github.com
- buildx Documents in
docs.docker.com
- buildx README.md in
buildx bake
docker
buildx hcl Supported variable values
type Target struct {
Name string `json:"-" hcl:"name,label"`
// Inherits is the only field that cannot be overridden with --set
Inherits []string `json:"inherits,omitempty" hcl:"inherits,optional"`
Context *string `json:"context,omitempty" hcl:"context,optional"`
Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional"`
Args map[string]string `json:"args,omitempty" hcl:"args,optional"`
Labels map[string]string `json:"labels,omitempty" hcl:"labels,optional"`
Tags []string `json:"tags,omitempty" hcl:"tags,optional"`
CacheFrom []string `json:"cache-from,omitempty" hcl:"cache-from,optional"`
CacheTo []string `json:"cache-to,omitempty" hcl:"cache-to,optional"`
Target *string `json:"target,omitempty" hcl:"target,optional"`
Secrets []string `json:"secret,omitempty" hcl:"secret,optional"`
SSH []string `json:"ssh,omitempty" hcl:"ssh,optional"`
Platforms []string `json:"platforms,omitempty" hcl:"platforms,optional"`
Outputs []string `json:"output,omitempty" hcl:"output,optional"`
Pull *bool `json:"pull,omitempty" hcl:"pull,optional"`
NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional"`
// IMPORTANT: if you add more fields here, do not forget to update newOverrides and README.
}
stay github action Upper use buildx
https://github.com/marketplace/actions/customizable-docker-buildx
troubleshoot
multiple platforms feature is currently not supported for docker driver
The current mode does not support buildx : https://github.com/docker/cli/blob/master/experimental/README.md
# Start experimental mode
# ~/docker/daemon.json
{
"experimental": true
}
# docker version -f '{{.Server.Experimental}}'
## true
auto-push is currently not implemented for docker driver
The lack of builder
# Create a builder
docker buildx create --use # a random name
docker buildx create --user --name specified_name # specified name
failed to solve: rpc error: code = Unknown desc = failed to load LLB: runtime execution on platform linux/arm64 not supported
Reference resources qemu-user-static #getting-started Execute the following command . If you return normally , That means success
$ uname -m
x86_64
$ docker run --rm -t arm64v8/ubuntu uname -m
standard_init_linux.go:211: exec user process caused "exec format error"
$ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
$ docker run --rm -t arm64v8/ubuntu uname -m
aarch64
- https://github.com/multiarch/qemu-user-static#getting-started
- https://github.com/docker/buildx/issues/132#issuecomment-521759117
This article is originally contained in the author's personal blog https://tangx.in
If you find this helpful , You can pay attention to the author's official account of WeChat
Please have a cup of coffee