如何将docker build的架构设置为arm64?

时间:2017-12-14 09:27:08

标签: docker architecture cross-compiling arm64 docker-build

我有一个我在amd64上运行但希望在arm64上运行的Dockerfile。由于go构建工具将GOARCH = arm64作为参数,因此我不需要任何其他交叉编译工具来制作二进制文件。

# Run the build
FROM mojlighetsministeriet/go-polymer-faster-build
ENV WORKDIR /go/src/github.com/mojlighetsministeriet/email
COPY . $WORKDIR
WORKDIR $WORKDIR
RUN go get -t -v ./...
RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build

# Create the final docker image
FROM scratch
COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=0 /go/src/github.com/mojlighetsministeriet/email/email /
ENTRYPOINT ["/email"]

问题是生成的图像标有错误的架构amd64而不是arm64。如何将参数传递给docker build,以便将Architecture设置为arm64?

$ docker image inspect mojlighetsministeriet/email-arm64                            
[
    {
        "Id": "sha256:33bcd7da8631c7a0829d61cc53479a26ab7f31fab1cb039105de415ddc6178f3",
        "RepoTags": [
            "mojlighetsministeriet/email-arm64:latest"
        ],
        "RepoDigests": [
            "mojlighetsministeriet/email-arm64@sha256:ab3f05d5597c3a304953b5c14f795179aa75bdfd458af3dc3cfb8b8d8eb87bc3"
        ],
        "Parent": "sha256:e5888262d93ea0946b5fd8146cf1c19ec3a7bac4d36eb51848ef0aefa75cf8e7",
        "Comment": "",
        "Created": "2017-12-05T18:36:36.273648787Z",
        "Container": "7a226edb3b52aaeeefec9e0fb4dd1da50d84992fb6cc374aeda9d82eec1bb2c8",
        "ContainerConfig": {
            "Hostname": "7a226edb3b52",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "ENTRYPOINT [\"/email\"]"
            ],
            "Image": "sha256:e5888262d93ea0946b5fd8146cf1c19ec3a7bac4d36eb51848ef0aefa75cf8e7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/email"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "17.10.0-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": null,
            "Image": "sha256:e5888262d93ea0946b5fd8146cf1c19ec3a7bac4d36eb51848ef0aefa75cf8e7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/email"
            ],
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 7861466,
        "VirtualSize": 7861466,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/03cb0162bf922636e4e0ec90123b81565a447c6cd511741103551d2f0e7f09f9/diff",
                "MergedDir": "/var/lib/docker/overlay2/091f74815a0caf457df7e57ade43b41c4dd8551388beca44815a7038501742ee/merged",
                "UpperDir": "/var/lib/docker/overlay2/091f74815a0caf457df7e57ade43b41c4dd8551388beca44815a7038501742ee/diff",
                "WorkDir": "/var/lib/docker/overlay2/091f74815a0caf457df7e57ade43b41c4dd8551388beca44815a7038501742ee/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:66f615d03920919b0fa8bc9fed45515bb95636be1837fdd10a82b2c183e2ad5b",
                "sha256:bd6a01b885eb6e3eec38a3fe3a2899646509633730b210cf6987456fd40b8a1c"
            ]
        },
        "Metadata": {
            "LastTagTime": "2017-12-14T10:24:10.796813522+01:00"
        }
    }
]

2 个答案:

答案 0 :(得分:1)

https://docs.docker.com/desktop/multi-arch/

# Shows builders installed
docker buildx ls

# Use builder that supports platform
docker buildx use default

docker buildx build --platform linux/arm64 -t <image_name>:<image_tag> --push .

答案 1 :(得分:0)

我能够解决问题,但不完全是我想要的,但是足够接近。

  1. 具有docker的amd64 Linux计算机

  2. 设置qemu用户静态支持手臂{{3}}

  3. 在您的docker文件中使用支持arm的基础映像。例如。 ubuntu

  4. 使用类似于以下命令的命令构建映像:

    docker build --platform arm --pull -t your_tag。

此命令将强制docker拉起基础映像的arm版本,还将为您的结果映像设置arm体系结构。但是请注意,如果您使用具有多种架构的标签,则上面的命令会将标签设置为arm版本。因此,要运行本机的amd64版本,您将需要再次拉下没有--platform arg的标签。