Alibaba Cloud Container Registry Enterprise Edition allows you to build container images for one or more architectures in an image build. This topic describes how to build container images for multiple architectures.

Prerequisites

Background information

  • If you select only one architecture, an image is built only for the architecture and pushed with the specified tag.
  • If you select multiple architectures, the images are built for each architecture and pushed to a specific image repository with the same tag. In this case, a client such as the Docker client and containerd pulls an image based on the architecture of the client from the remote image repository.

The following table describes the supported architectures.

Operating system Architecture Supported
Linux amd64 Yes (Default)
Linux amd64 Yes
Linux arm/v7 Yes
Linux arm/v6 Yes
Windows amd64 No (If you need to build images for this architecture, .)

Step 1: Create a project

Create a project as the source code repository based on which the image is built. In this example, you can use the following sample Golang file and Dockerfile to create your project.

// Save the file and name the file hello.go.

package main
import (
        "fmt"
        "runtime"
)
func main() {
        fmt.Printf("Hello, %s!\n", runtime.GOARCH)
}
FROM golang:alpine AS builder
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go build -o hello hello.go
FROM alpine
RUN mkdir /app
WORKDIR /app
COPY --from=builder /app/hello .
CMD ["./hello"]

Step 2: Create an image repository

Create an image repository and bind it to a code repository. All image builds triggered by the code repository are pushed to the image repository.

  1. Log on to the Container Registry console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Instances.
  4. On the Instances page, click the required Container Registry Enterprise Edition instance.
  5. On the management page of the Container Registry Enterprise Edition instance, choose Repositories > Repositories in the left-side navigation pane.
  6. On the Repositories page, click Create Repository.
  7. In the Repository Info step, set the Namespaces, Repository Name, Repository Type, Tags, Summary, and Description parameters, and click Next.
    Note If you select Immutable next to Tags, all images except for those with the latest tag in the repository cannot be overwritten. After you set an image tag as immutable, you can rely on the tag to correlate the deployed image version with the build that produced the image.
  8. In the Code Source step, set the Code Source, Build Settings, and Build Rules parameters, and click Create Repository.
    Parameter Description
    Code Source The code source.
    Build Settings
    • Automatically Build Images When Code Changes: If you select this option, an image is automatically built when code is committed from a branch.
    • Build With Servers Deployed Outside Mainland China: If you select this option, images are built in a data center outside mainland China and then pushed to the image repository.
    • Build Without Cache: If you select this option, the system pulls the dependent base image for every image to be built. This may prolong the build time.
    Build Rules After the repository is created, you can go to the Build page to create build rules. For more information, see Create a repository and build images.
    On the Repositories page, click the created image repository. If you can view Build in the left-side navigation pane of the repository management page, the image repository is bound to the source code repository.

Step 3: Create an image build rule and select architectures

In this example, select linux/amd64 and linux/arm64 for the Build Architecture parameter when you configure the image build rule.

  1. Log on to the Container Registry console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Instances.
  4. On the Instances page, click the required Container Registry Enterprise Edition instance.
  5. On the management page of the Container Registry Enterprise Edition instance, choose Repositories > Repositories in the left-side navigation pane.
  6. On the Repositories page, find the created image repository and click Manage in the Actions column.
  7. In the left-side navigation pane, click Build. In the Build Rules section, click Add Build Rule. In the Build Information step of the Add Build Rule dialog box, set the following parameters and click Next.
    Parameter Description
    Type Specify the type of the source code repository. Valid values: Branch and Tag.
    Branch/Tag Select or enter a branch or a tag. Regular expressions are supported. If you specify the release-(?<imageTag>\w*) regular expression, the system automatically builds an image of V1 when the source code under the release-v1 branch is updated. The image cannot be built immediately. For more information about how to specify regular expressions, see Use regular expressions in named capturing groups.
    Note After you specify regular expressions, images can be built only by the system. You cannot manually build images.
    Dockerfile Directory The directory where the Dockerfile is stored. You must specify a relative directory. The parent directory is the root directory of the code branch.
    Dockerfile Filename The name of the Dockerfile. The default name is Dockerfile.
  8. In the Build Configurations step, set the following parameters and click Confirm.
    Parameter Description
    Tags Set the tag of the image, for example, latest.
    • Image Tag: Set the tag of the image, for example, latest. You can enable named capturing groups. For example, you can use the captured content if you specify a named capturing group for Branch/Tag.
    • Build Time: the time when source code is pushed in UTC+8 format, for example, 20201015 or 202010151613.
      Note This parameter is optional. If you set this parameter, images can be built only by the system. You cannot manually build images.
    • Commit ID: the commit ID of the most recent push. By default, the first six bits are used. You can drag the slider to change the number of bits.
      Note This parameter is optional. If you set this parameter, images can be built only by the system. You cannot manually build images.
    Build Parameters The runtime parameters of the image build. Each build parameter is a key-value pair that is case-sensitive. You can set a maximum of 20 build parameters.
  9. Trigger the image build rule.
    You can use one of the following methods to trigger an image build rule:
    • In the Build Rules section of the Build page, find the image build rule and click Build in the Actions column.
    • Submit code to the master branch of the source code repository to trigger the build rule.
    Note
    • In the Build Log section of the Build page, find the triggered image build and click Cancel in the Actions column to cancel the image build task.
    • In the Build Log section of the Build page, find the triggered image build and click Log in the Actions column to view the image build log.

Step 4: Verify the results

  1. On the management page of the created repository, click Tags in the left-side navigation pane. On the page that appears, view the images that are built.
    In this example, you can view two images with the image tag main. One image supports the Linux operating system and the AMD64 architecture and the other one supports the Linux operating system and the ARM64 architecture. Images for multiple architectures
  2. Verify that the images can be used on a machine with the Linux operating system and the AMD64 architecture and a machine with the Linux operating system and the ARM64 architecture.
    • Run the following command on a machine whose operating system and architecture are Linux and AMD64.
      docker run --rm xxx-registry.cn-hangzhou.cr.aliyuncs.com/test/golang-test:main

      Expected output:

      Hello, amd64!
    • Run the following command on a machine whose operating system and architecture are Linux and ARM64.
      docker run --rm xxx-registry.cn-hangzhou.cr.aliyuncs.com/test/golang-test:main
      Expected output:
      Hello, arm!

    The built images can be used by machines with different architectures.