Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Architecture for docker build to arm64?

I have a Dockerfile that I run on amd64 but want to run on arm64. Since go build tool takes GOARCH=arm64 as argument I don't need any other cross compilation tool to make the binary.

# 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"]

The problem is that the resulting image gets marked with the wrong Architecture amd64 instead of arm64. How can I pass an argument to docker build so that it sets Architecture to 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"
        }
    }
]
like image 317
tirithen Avatar asked Dec 14 '17 09:12

tirithen


People also ask

Does docker work on arm64?

We now have a single command to create the Docker image with multi-architecture support for the hello world PHP application for amd64, arm64, and arm32, and to store the image in Docker Hub.

Can I run x86 docker image on ARM?

Although the M1 version docker desktop allows users to run x86 docker images under emulation, it will be a more efficient solution to offer your software as a “universal” Multi-Arch docker image that can serve both your ARM (M1) and x86 users.

Can you run docker on ARM?

Docker Desktop provides binfmt_misc multi-architecture support, which means you can run containers for different Linux architectures such as arm , mips , ppc64le , and even s390x .

Can you build arm on x86?

Setting Up ARM Emulation on x86 QEMU is an open source machine emulator and virtualizer. It allows users to to build ARM CUDA binaries on your x86 machine without needing a cross compiler. The installation was successful, the emulation is working.


2 Answers

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 .
like image 97
Alex W Avatar answered Nov 14 '22 22:11

Alex W


I was able to solve the problem, not exactly as I wanted, but close enough.

  1. Have an amd64 Linux machine with docker

  2. Setup qemu user static for arm support https://hub.docker.com/r/multiarch/qemu-user-static/

  3. In your docker file use base image with support for arm. E.g. ubuntu

  4. Build your image with command similar to the following:

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

This command will force docker to pull arm version of the base image and will also set arm architecture to your result image. But be careful, if you use tags with multiple architectures, the command above will set the tag to arm version. So to run the native amd64 version you will need to pull the tag again without --platform arg.

like image 45
Vladimir Perevalov Avatar answered Nov 14 '22 21:11

Vladimir Perevalov