In this tutorial, you will learn how to create Docker images that can run on different architectures using Docker Buildx and Multi-Arch Builds. Docker Buildx is a CLI plugin that extends the docker command with the full support of the features provided by Moby BuildKit builder toolkit. It provides the same user experience as docker build with many new features like creating scoped builder instances and building against multiple nodes concurrently.
By the end of this tutorial, you should be able to:
Prerequisites:
Enable Docker Buildx: Docker Buildx is included in Docker 19.03 and is also bundled with the following Edge versions 18.09.3 or higher. Check if Docker Buildx is installed and accessible by running docker buildx version
.
Create a New Builder Instance: To start using Docker Buildx, you need to create and boot a new builder instance with a single docker-container driver. Use the create
command: docker buildx create --use
.
Inspect the Current Builder Instance: To verify that the builder instance is running correctly, use the inspect
command with --bootstrap
option: docker buildx inspect --bootstrap
.
Building Multi-Arch Images: With Docker Buildx and QEMU you can build images that support different architectures. Docker Buildx automatically uses QEMU to emulate the target architecture when you use the docker buildx build
command.
# Create a new builder instance
docker buildx create --use
# Inspect the current builder instance
docker buildx inspect --bootstrap
The output should be something like this:
Name: mybuilder
Driver: docker-container
Nodes:
Name: mybuildern0
Endpoint: unix:///var/run/docker.sock
Status: running
Platforms: linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
# Build and push the image to Docker Hub
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t username/multiarch-example --push .
Replace username
with your Docker Hub username. After running this command, Docker will build the image for the specified architectures and push them to Docker Hub.
In this tutorial, we've covered the basics of Docker Buildx, how to create a new builder instance, inspect it, and how to build a Docker image for multiple architectures. For further learning, you can explore how to use Docker Buildx with a CI/CD system or how to build images for even more architectures.
Solutions:
Solution to Exercise 1:
```bash
docker buildx build --platform linux/arm64 -t username/arm64-example --push .
```
Replace username
with your Docker Hub username.
Solution to Exercise 2:
Dockerfile:
Dockerfile
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "app.js" ]
Building the image:
bash
docker buildx build --platform linux/amd64,linux/arm64 -t username/nodejs-example --push .
Replace username
with your Docker Hub username.
Remember, practice is key when it comes to mastering new concepts. Keep building different Docker images for different architectures, and you'll get the hang of Docker Buildx in no time!