docker_practice/buildx/multi-arch-images.md

127 lines
3.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 使 buildx Docker
Docker 使使 [`$ docker manifest`](../image/manifest.md)
Docker 19.03+ 使 `$ docker buildx build` 使 `BuildKit` `--platform` Docker
## `builder`
Docker for Linux `arm` Docker
```bash
$ 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
```
##
`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`
```docker
FROM scratch
# 使用变量必须申明
ARG TARGETOS
ARG TARGETARCH
COPY bin/dist-${TARGETOS}-${TARGETARCH} /dist
ENTRYPOINT ["dist"]
```