laitimes

Buildx, a container image building artifact, supports multiple platforms

author:Not bald programmer
Buildx, a container image building artifact, supports multiple platforms

1. Introduction

Docker is a popular deployment tool that allows us to package and run applications. Due to the high adoption rate, the functionality needs to be scaled according to different requirements. Therefore, in order to achieve this, third-party docker plugins [1] can be used.

For example, if you want your data to persist across different hosts, you can use a volume plugin. Another commonly used plugin is Docker buildx[2]. It extends the build capabilities of images by using the BuildKit builder. So, with the plugin, we can build images for different platforms and architectures. In addition, it supports parallel multi-stage production with custom contexts.

In this tutorial, we'll cover Docker buildx.

2. Install buildx

First, to run buildx, we need to install Docker. Support for Docker buildx is available from 19.00.

Check the Docker version first:

$ docker --version
Docker version 19.03.8, build afacb8b           

Next, enable the Docker experimental feature by setting environment variables:

$ export DOCKER_CLI_EXPERIMENTAL=enabled           

To make sure our setup persists after the session ends, we added the variable to $HOME/.bashrc, which should now have access to buildx:

$ docker 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.           

This shows the common commands and the syntax for each.

3. Build with buildx

buildx performs all Docker build[3] functions. So, they can be easily run and executed. For example, specify the target platform, build cache, and output configuration. In addition to that, BuildX also offers additional features.

The first is the ability to build images for multiple platforms at the same time. Second, do a multi-stage build for smaller images in a single dockerfile. Finally, the ability to customize inputs, parameters, or variables during the build process.

Let's dive into one example by creating one:

$ docker buildx create --name ourbuilder
ourbuilder           

This will create an instance of the build called ourbuilder.

Next, set it up as Active Directory:

$ docker buildx use ourbuilder           

Next, create a dockerfile to run a simple node application:

# Base image
FROM node:14-alpine

# Set working directory
WORKDIR /app

# Copy application files
COPY . .

# Install dependencies
RUN npm install --production

# Expose the port
EXPOSE 3000

# Start the application
CMD ["node", "app.js"]           

Here we use the node.js base image and set the working directory to /app. Then copy the application files into the container. Then install all dependencies, expose port 3000 and start the application:

$ docker buildx build --platform linux/amd64,linux/arm64 -t ourapp:latest .
time="2023-06-01T07:13:20+03:00" level=warning msg="No output specified for docker-container driver.
  Build result will only remain in the build cache. To push result image into registry use --push or to load image into docker use --load"
#1 [internal] booting buildkit
#1 pulling image moby/buildkit:buildx-stable-1
#1 pulling image moby/buildkit:buildx-stable-1 73.2s done
#1 creating container buildx_buildkit_ourbuilder0
#1 creating container buildx_buildkit_ourbuilder0 2.1s done
#1 DONE 75.4s

#3 [internal] load .dockerignore
#3 transferring context: 0.0s
#3 transferring context: 2B 0.1s done
#3 DONE 0.3s

#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 294B 0.0s done
#2 DONE 0.4s

#4 [linux/amd64 internal] load metadata for docker.io/library/node:14-alpin...
#4 DONE 4.7s
.... truncated .....           

Use the –platform flag to specify the target platform. In this case, the target is x86 (Linux/amd64) and ARM (Linux/arm64) architectures. We've also provided the tag -T ourApp:Latest to tag the built image with the name ourapp and the latest tag. Specify the build context, which is the current directory.

Docker buildx auto handles multi-platform builds and generates separate images for each target architecture.

4. Conclusion

In this tutorial, we explored Docker buildx, a tool that extends the ability to build and manage Docker images. It streamlines the process by supporting parallel builds, custom build contexts, and multi-stage builds.