[Devops] Docker buildx build (at M1 Macbook)

inyong_pang·2021년 6월 13일
1

Devops

목록 보기
7/7
post-thumbnail

올해 새로 MacBookPro(13-inch, M1, 2020)을 지급받아서 사용중에 있다.
근래 docker 이미지 생성, registry에 배포 및 관리 등을 하고 있는데,
M1 MacBookPro에서 만든 이미지가 배포서버(Server)에서 pull 받아서 run하는데 문제가 발생했다.

WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
# docker logs container
standard_init_linux.go:219: exec user process caused: exec format error
  • M1의 architecture가 arm 기반으로, docker build로 이미지를 빌드할 시, arm64/v8 기반으로 이미지를 빌딩한다...
  • 따라서 amd64 기반의 이미지를 빌드할 수 있도록 docker buildx(멀티 플랫폼 빌드)가 필요

Multi-Platform Docker Builds

  • docker-buildx 는 docker images를 빌드하는데 있어 다양한 아키텍쳐를 지원
  • docker CLI 확장 플러그인으로 docker-19.03 버전 부터 사용 가능
  • 별도의 Dockerfile 작성이 필요 없음
  • In-Container 드라이버 지원(Docker & Kubernetes)
  • Compose 빌드 지원
  • Moby BuildKit에서 제공되는 모든 기능 지원

Install docker buildx in M1-docker

CLI Experimental Features

  • docker dashboard > Preference
  • enable experiemntal features 활성화

docker buildx 확인

  • 활성화 전 cmd
> docker buildx
docker: 'buildx' is not a docker command.
  • 활성화 후 cmd
> docker buildx 
ocker buildx

Usage:    docker buildx COMMAND

Build with BuildKit

Management Commands:
  imagetools  Commands to work on images in registry

Commands:
  bake        Build from a file
  build       Start a build
  create      Create a new builder instance
  inspect     Inspect current builder instance
  ls          List builder instances
  rm          Remove a builder instance
  stop        Stop builder instance
  use         Set the current builder instance
  version     Show buildx version information

Run 'docker buildx COMMAND --help' for more information on a command.

how to use docker buildx

create builder

  • 기본 builder가 모든 아키텍처를 빌드할 수 있지만, 필요한 아키텍쳐만 선택해서 또다른 builder를 생성할 수 있다

  • builder ls

> docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS  PLATFORMS
default * docker
  default default         running linux/amd64, linux/arm64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
  • create builder
> docker buildx create --name [builder instance 명] --driver [드라이버 이름] --use
> docekr buildx create m1builder --dirver linux/arm64, linux/amd64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
-- use
  • --driver는 여러개 복수로 지정 가능
  • --use는 만든 builder instance를 바로 사용하는 옵션

builder 확인

  • builder inspect
> docker buildx inspect m1builder --bootstrap
Name:   m1builder
Driver: docker-container

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

> docker buildx ls
NAME/NODE    DRIVER/ENDPOINT             STATUS  PLATFORMS
m1builder *  docker-container
  m1builder0 unix:///var/run/docker.sock running linux/arm64, linux/amd64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
default      docker
  default    default                     running linux/arm64, linux/amd64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6

create buildx image

  • builder instance로 미리 작성한 Dockerfile로 image를 만듦
# dockerfile 있는 디렉토리
> docker buildx build --platform linux/amd64 --load --tag registry/test_api:1.0.0 .
[+] Building 36.9s (10/10) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                     0.0s
 => => transferring dockerfile: 32B                                                                                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                        0.0s
 => => transferring context: 2B                                                                                                                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/python:3.9.2-slim                                                                                                                                                                     3.8s
 => [1/4] FROM docker.io/library/python:3.9.2-slim@sha256:ce367bb30b8928efb632c369e3bd4a8dbd7905417bd0a245087a82c250e54a24                                                                                                              14.3s
 => => resolve docker.io/library/python:3.9.2-slim@sha256:ce367bb30b8928efb632c369e3bd4a8dbd7905417bd0a245087a82c250e54a24                                                                                                               0.0s
 => => sha256:e083ad7cc3ca624f4e268a778cb4f8be0d7a258240144862390e0da39082ea60 2.45MB / 2.45MB                                                                                                                                           7.3s
 => => sha256:65d21ac50a56640b50d18bd6c9d52bb967bd63de2e5dcce551340eb9bca26418 234B / 234B                                                                                                                                               1.5s
 => => sha256:229daa3ebd94b66f007a1fce482e8f7ef6b8deaf7e75324332fdfae7a197c67c 10.89MB / 10.89MB                                                                                                                                         7.1s
 => => sha256:edf8b70d3cc4282c43b24efda071ccab93b989a5dccb108b2102b0c2198c6471 2.77MB / 2.77MB                                                                                                                                           4.8s
 => => sha256:75646c2fb4101d306585c9b106be1dfa7d82720baabe1c75b64d759ea8adf341 27.14MB / 27.14MB                                                                                                                                        12.9s
 => => extracting sha256:75646c2fb4101d306585c9b106be1dfa7d82720baabe1c75b64d759ea8adf341                                                                                                                                                0.8s
 => => extracting sha256:edf8b70d3cc4282c43b24efda071ccab93b989a5dccb108b2102b0c2198c6471                                                                                                                                                0.1s
 => => extracting sha256:229daa3ebd94b66f007a1fce482e8f7ef6b8deaf7e75324332fdfae7a197c67c                                                                                                                                                0.4s
 => => extracting sha256:65d21ac50a56640b50d18bd6c9d52bb967bd63de2e5dcce551340eb9bca26418                                                                                                                                                0.0s
 => => extracting sha256:e083ad7cc3ca624f4e268a778cb4f8be0d7a258240144862390e0da39082ea60                                                                                                                                                0.1s
 => [internal] load build context                                                                                                                                                                                                        0.0s
 => => transferring context: 93B                                                                                                                                                                                                         0.0s
 => [2/4] WORKDIR /home/app                                                                                                                                                                                                              0.0s
 => [3/4] ADD . .                                                                                                                                                                                                                        0.0s
 => [4/4] RUN pip install -r requirements.txt                                                                                                                                                                                           13.5s
 => exporting to oci image format                                                                                                                                                                                                        5.1s
 => => exporting layers                                                                                                                                                                                                                  1.0s
 => => exporting manifest sha256:c64d4192989179c14f8e01a173e890e0856348f891db80a01c3ab9f7c124211b                                                                                                                                        0.0s
 => => exporting config sha256:5ee75a529f8732c11b102ef337073268f540f539781e2c2da5c10705090d2958                                                                                                                                          0.0s
 => => sending tarball
  • --platform : builder instance가 사용가능한 platform 즉, 아키텍쳐를 선택하여 이미지를 빌드화한다.
  • --load : 이미지를 만들고 호스트 docker image에 저장
  • --push : --load옵션과 반대로 docker registry로 바로 push 하는 옵션

run image and check

  • 각각 만들어진 이미지(image)를 실행(run)하고 각 컨테이너(container)의 아키텍쳐를 확인한다
  • ex)
    test_api:1.0.0 -> docker build로 빌드화
    test_api:1.0.1 -> docker buildx로 빌드화
> docker images
REPOSITORY                               TAG               IMAGE ID       CREATED         SIZE
registry/test_api 		         1.0.1             5ee75a529f87   3 minutes ago   125MB
registry/test_api  		         1.0.0             79f4125ee89c   2 hours ago     118MB

> docker run -it -d --name test_api_1.0.0 registry/test_api:1.0.0 
> docker exec -it test_api_1.0.0 dpkg -s libc6 | grep Arch
Architecture: arm64
Multi-Arch: same

> docker run -it -d --name test_api_1.0.1 registry/test_api:1.0.1 
> docker exec -it test_api_1.0.1 dpkg -s libc6 | grep Arch
Architecture: amd64
Multi-Arch: same
profile
dev_pang의 pang.log

0개의 댓글