본문 바로가기

기타

Docker Buildx를 사용하여 다중 아키텍처 이미지를 빌드하는 방법

728x90

Docker Buildx를 사용하여 다중 아키텍처 이미지를 빌드하는 방법

테스트 환경

$ docker --version
Docker version 24.0.2, build cb74dfc

Docker Desktop 설정

Docker Desktop을 실행한 후, Docker Desktop 메뉴를 열고 "Settings"를 선택합니다. "Settings"에서 "Docker Engine" 탭을 선택하고 experimental 옵션을 추가합니다.

"experimental": true

다중 아키텍처 이미지 빌드

buildx 활성화된 상태

docker buildx
$ docker buildx

Usage:  docker buildx [OPTIONS] COMMAND

Extended build capabilities with BuildKit

Options:
      --builder string   Override the configured builder instance

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
  du          Disk usage
  inspect     Inspect current builder instance
  ls          List builder instances
  prune       Remove build cache
  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.

buildx 버전 확인

$ docker buildx version
github.com/docker/buildx v0.10.5 86bdced7766639d56baa4c7c449a4f6468490f87

builder instance 확인

docker buildx ls
$ docker buildx ls

NAME/NODE       DRIVER/ENDPOINT STATUS  BUILDKIT                              PLATFORMS
default                         error
desktop-linux * docker
  desktop-linux desktop-linux   running v0.11.7-0.20230525183624-798ad6b0ce9f linux/arm64, linux/amd64, linux/amd64/v2, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6

Cannot load builder default: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
  • PLATFORMS: linux/arm64, linux/amd64, linux/amd64/v2, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6

Buildx 설치

Buildx는 Docker CLI의 확장 기능입니다. 다음 명령어를 사용하여 Buildx를 설치합니다.

docker buildx install

Buildx 초기화

Buildx를 초기화하여 멀티 플랫폼 빌드를 위한 빌더 인스턴스를 생성합니다. 다음 명령어를 사용하여 Buildx를 초기화합니다.

docker buildx create --name mybuilder
docker buildx use mybuilder
728x90

 

현재 builder instance 정보

docker buildx inspect --bootstrap
$ docker buildx inspect --bootstrap
[+] Building 2.6s (1/1) FINISHED
 => [internal] booting buildkit                                                                                                                                   2.6s
 => => pulling image moby/buildkit:buildx-stable-1                                                                                                                2.2s
 => => creating container buildx_buildkit_mybuilder0                                                                                                              0.4s
Name:          mybuilder
Driver:        docker-container
Last Activity: 2023-06-02 15:08:41 +0000 UTC

Nodes:
Name:      mybuilder0
Endpoint:  desktop-linux
Status:    running
Buildkit:  v0.11.6
Platforms: linux/arm64, linux/amd64, linux/amd64/v2, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6

이미지를 빌드 명령 실행: Dockerfile을 실행하여 이미지를 빌드합니다.

docker buildx build --platform linux/amd64,linux/arm64 --tag anti1346/ubuntu2204:latest . --no-cache

이미지를 레지스트리에 푸시: 이미지를 빌드하고 나서 --push 옵션을 추가하여 이미지를 레지스트리에 푸시합니다.

docker buildx build --platform linux/amd64,linux/arm64 --tag anti1346/ubuntu2204:latest --push .

이미지를 도커에 로드: 이미지를 빌드하고 나서 --load 옵션을 추가하여 이미지를 도커에 로드합니다.

docker buildx build --platform linux/amd64,linux/arm64 --tag anti1346/ubuntu2204:latest --load .

다중 아키텍처 이미지 확인

docker buildx imagetools inspect anti1346/ubuntu2204:latest
$ docker buildx imagetools inspect anti1346/ubuntu2204:latest
Name:      docker.io/anti1346/ubuntu2204:latest
MediaType: application/vnd.oci.image.index.v1+json
Digest:    sha256:162699366d77864a5daf85acdab184d0db897e602aa589d54a8f84bb4bebf7d9

Manifests:
  Name:        docker.io/anti1346/ubuntu2204:latest@sha256:32d0ac5d7b7b00a90f2bd62554ca5aef1d52f8a896c2f2f2acb017ca59784cb7
  MediaType:   application/vnd.oci.image.manifest.v1+json
  Platform:    linux/amd64

  Name:        docker.io/anti1346/ubuntu2204:latest@sha256:ed0bf1c7b143dbddff475a24dbabcde330b953cf81d0c6895e596d69ab672642
  MediaType:   application/vnd.oci.image.manifest.v1+json
  Platform:    linux/arm64

  Name:        docker.io/anti1346/ubuntu2204:latest@sha256:195c96fb9f4d6648f5ecfe1ad41efd6b9fd6a570fc26bc855e51f0620a8f418d
  MediaType:   application/vnd.oci.image.manifest.v1+json
  Platform:    unknown/unknown
  Annotations:
    vnd.docker.reference.type:   attestation-manifest
    vnd.docker.reference.digest: sha256:32d0ac5d7b7b00a90f2bd62554ca5aef1d52f8a896c2f2f2acb017ca59784cb7

  Name:        docker.io/anti1346/ubuntu2204:latest@sha256:f372b9c88ad6bdd61e86de9e76aecd9173edc2f31ce67a7d2169d73bc46f9adc
  MediaType:   application/vnd.oci.image.manifest.v1+json
  Platform:    unknown/unknown
  Annotations:
    vnd.docker.reference.digest: sha256:ed0bf1c7b143dbddff475a24dbabcde330b953cf81d0c6895e596d69ab672642
    vnd.docker.reference.type:   attestation-manifest

도커 이미지 풀(Pull)

docker pull anti1346/ubuntu2204:latest

도커 컨테이너 실행

docker run -it --rm --name ubuntu anti1346/ubuntu2204:latest bash

Buildx instance 삭제

docker buildx rm mybuilder

Docker Buildx 캐시 초기화

docker buildx prune --force

 

728x90