10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MacでARM用Dockerイメージを作成する

Last updated at Posted at 2020-05-28

dockerのExperimental機能であるbuildxを利用すると、異なるCPUアーキテクチャ用のDockerイメージを作ることができます。
今までは、ARM用のDockerイメージを作るためにはARM用のマシンでDockerイメージをbuildする必要がありましたが、
buildxを利用することでローカルで作業を完結させることができるようになりました。

環境用意

まずはインストール済みのDocker Desktopのバージョンが以下であることを確認します。

  • Docker Desktop Version 2.0.4.0 (33772) 以上

また、buildxはExperimental機能のため利用するために以下のように設定ファイルを編集する必要があります。

$HOME/.docker/config.json

{
  "stackOrchestrator" : "swarm",
  "credsStore" : "desktop",
  "auths" : {
    "787080764332.dkr.ecr.ap-northeast-1.amazonaws.com" : {

    },
    "https://index.docker.io/v1/" : {

    }
  },
  "HttpHeaders" : {
    "User-Agent" : "Docker-Client/19.03.5 (darwin)"
  },
  "experimental": "enabled" <-- ここを追加
}

次にbuild用のコンテナを作成していきます。

$ docker buildx create --name buildarm --platform linux/arm64 --use
$ docker buildx use buildarm
$ docker buildx inspect --bootstrap
[+] Building 0.8s (1/1) FINISHED                                                                                                                                                                                                                                             
 => [internal] booting buildkit                                                                                                                                                                                                                                         0.8s
 => => starting container buildx_buildkit_buildarm0                                                                                                                                                                                                                     0.8s
Name:   buildarm
Driver: docker-container

Nodes:
Name:      buildarm0
Endpoint:  unix:///var/run/docker.sock
Status:    running
Platforms: linux/arm64, linux/amd64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6

これでARM用のDockerイメージを作る準備ができました。

Dockerイメージを作成

ここではテスト用に簡易的なDockerfileを用意してbuildしていきます。

$ mkdir test && cd test && cat <<EOF > Dockerfile

FROM ubuntu
RUN apt-get update && apt-get install -y curl
WORKDIR /src
COPY . .
EOF

以下イメージ作成になります。

$ docker buildx build --platform linux/arm/v7 -t test-buildx --load .                   
[+] Building 8.3s (10/10) FINISHED                                                                                                  
 => [internal] load build definition from Dockerfile                                                                           0.0s
 => => transferring dockerfile: 31B                                                                                            0.0s
 => [internal] load .dockerignore                                                                                              0.0s
 => => transferring context: 2B                                                                                                0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                               4.7s
 => [internal] load build context                                                                                              0.0s
 => => transferring context: 31B                                                                                               0.0s
 => [1/4] FROM docker.io/library/ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7                0.0s
 => => resolve docker.io/library/ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7                0.0s
 => CACHED [2/4] RUN apt-get update && apt-get install -y curl                                                                 0.0s
 => CACHED [3/4] WORKDIR /src                                                                                                  0.0s
 => CACHED [4/4] COPY . .                                                                                                      0.0s
 => exporting to oci image format                                                                                              3.4s
 => => exporting layers                                                                                                        0.0s
 => => exporting manifest sha256:75f5df7b0a817b040d8e06c4547c17734bf719ce038f91a0f59e4b379a0213b7                              0.0s
 => => exporting config sha256:4815274966cce0c0f0ce84e54cd28b9ee02164bd1d3e4899578d1176e0388506                                0.0s
 => => sending tarball                                                                                                         3.4s
 => importing to docker                                                                                                        0.0s
$ docker images | grep test-buildx                                          
test-buildx                                           latest                         4815274966cc        About an hour ago   82.1MB
10
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?