docker_practice/buildx/multi-arch-images.md

127 lines
3.8 KiB
Go
Raw Normal View History

# 使 buildx Docker
Docker 使使 [`$ docker manifest`](../image/manifest.md)
Docker 19.03+ 使 `$ docker buildx build` 使 `BuildKit` `--platform` Docker
## `builder`
Docker for Linux `arm` Docker
```bash
2020-09-06 08:52:31 +08:00
$ docker run --rm --privileged tonistiigi/binfmt:latest --install all
```
Docker `builder` `--platform` `builder` 使 [](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md) 的 [`dockerpracticesig/buildkit:master`](https://github.com/docker-practice/buildx) 镜像替换官方镜像。
> https://github.com/docker-practice/buildx 构建自己的 buildkit 镜像并使用它。
```bash
#
$ docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master
# (coding.net )
$ docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master-tencent
# $ docker buildx create --name mybuilder --driver docker-container
$ docker buildx use mybuilder
```
##
Dockerfile
```docker
FROM --platform=$TARGETPLATFORM alpine
RUN uname -a > /os.txt
CMD cat /os.txt
```
使 `$ docker buildx build` `myusername` Docker Hub
`--push` Docker
```bash
$ docker buildx build --platform linux/arm,linux/arm64,linux/amd64 -t myusername/hello . --push
#
$ docker buildx imagetools inspect myusername/hello
```
```bash
# arm
$ docker run -it --rm myusername/hello
Linux buildkitsandbox 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 armv7l Linux
# arm64
$ docker run -it --rm myusername/hello
Linux buildkitsandbox 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 aarch64 Linux
# amd64
$ docker run -it --rm myusername/hello
Linux buildkitsandbox 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 Linux
```
2020-09-06 08:52:31 +08:00
##
`Dockerfile`
**TARGETPLATFORM**
`linux/amd64`, `linux/arm/v7`, `windows/amd64`
**TARGETOS**
`TARGETPLATFORM` OS `linux`, `windows`
**TARGETARCH**
`TARGETPLATFORM` `amd64`, `arm`
**TARGETVARIANT**
`TARGETPLATFORM` `v7`
**BUILDPLATFORM**
`linux/amd64`
**BUILDOS**
`BUILDPLATFORM` OS `linux`
**BUILDARCH**
`BUILDPLATFORM` `amd64`
**BUILDVARIANT**
`BUILDPLATFORM` `v7`
### 使
`linux/arm/v7` `linux/amd64`
* bin/dist-linux-arm
* bin/dist-linux-amd64
`Dockerfile`
```bash
FROM scratch
# 使
ARG TARGETOS
ARG TARGETARCH
COPY bin/dist-${TARGETOS}-${TARGETARCH} /dist
ENTRYPOINT ["dist"]
```