Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use buildx to support multi-arch images #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lexbritvin
Copy link

@lexbritvin lexbritvin commented May 10, 2023

To provide multi-arch images, we have to use buildx in the builds.
Due to standard build command doesn't support multi-arch builds, and buildx --load doesn't allow making multi-arch images locally also, make build must only be used for building images locally for the current arch.
New target make buildx-push is introduced to create multi-arch images and is intended to build and push images to Docker Hub as a default way.

We have to agree on the best build flow here and maybe introduce github actions to simplify the build procedure.
We must also rebuild the images for php74 to support older projects.

Makefile Show resolved Hide resolved
@@ -14,7 +14,6 @@ build:
@echo "Building images for tags: $(TAGS)"
set -e; for i in $(TAGS); do printf "\nBuilding $(NAME):$$i \n\n"; cd php$$i; \
DOCKER_BUILDKIT=$(DOCKER_BUILDKIT) docker build -t $(NAME):$$i \
--platform $(PLATFORM) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this option is available because DOCKER_BUILDKIT=1 means that build is using buildx

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise it needs just to replace s/build/buildx and remove there DOCKER_BUILDKIT

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my experience, simple build is like buildx build --load which doesn't provide multiarch.
BTW, since Docker 23 it's always buildkit, but it still doesn't support multiarch for build

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean we just need to remove usage of DOCKER_BUILDKIT

@andypost
Copy link
Contributor

andypost commented May 10, 2023

Used following patch to try build

diff --git a/Makefile b/Makefile
index 306cf99..dd658d0 100644
--- a/Makefile
+++ b/Makefile
@@ -3,18 +3,18 @@ TAGS ?= 81 81-fpm 81-unit 82 82-fpm 82-unit
 
 COMPOSER_HASH ?= 55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae
 DRUSH_VERSION ?= 8.4.12
-DOCKER_BUILDKIT ?= 1
-PLATFORM ?= linux/amd64
+PLATFORM ?= linux/amd64,linux/arm64
 
-.PHONY: all build push
+.PHONY: all build push prepare
 
 all: build push
 
 build:
 	@echo "Building images for tags: $(TAGS)"
 	set -e; for i in $(TAGS); do printf "\nBuilding $(NAME):$$i \n\n"; cd php$$i; \
-		DOCKER_BUILDKIT=$(DOCKER_BUILDKIT) docker build -t $(NAME):$$i \
+		docker buildx build -t $(NAME):$$i \
 		--platform $(PLATFORM) \
+		--push \
 		--no-cache --progress=plain \
 		--build-arg COMPOSER_HASH=$(COMPOSER_HASH) \
 		--build-arg DRUSH_VERSION=$(DRUSH_VERSION) \
@@ -28,3 +28,7 @@ push:
 
 unit:
 	make -C unit-php-builder/dev build
+
+prepare:
+	docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
+	docker buildx create --use

It means the builder needs setup (qemu)

Moreover the problem is that you can only push image (it's not stored locally)

@andypost
Copy link
Contributor

@ lexbritvin I pushed this way the skilldlabs/php:81 image, please try to run it on your Mac https://hub.docker.com/layers/skilldlabs/php/81/images/sha256-b6e83bde7da6166c16fcf27cbce54e5ff6b6308fe84b8805bef5e1ff921fb0c9?context=explore

@andypost
Copy link
Contributor

I think we need 2 makefile targets :

  1. build tagged release for specific PHP version
  2. build&push multiarch

@lexbritvin
Copy link
Author

@ lexbritvin I pushed this way the skilldlabs/php:81 image, please try to run it on your Mac

  1. Successfully pulled arm version
  2. Tested make all on SDC project, installing php and composer dependencies - works as expected

I think we need 2 makefile targets :

I don't know/see how you tag specific versions except upstream tags.
For me, we definitely need buildx build --push all the time, and build tagged release is only for testing purposes because it's not multi-arch and building manifests manually is not advised for prod use.
I think that's what I proposed initially, but I'm not sure about your build flow, so I don't think I understand what you mean.

Please, correct me if I'm wrong or describe the build flow more precisely.

@andypost
Copy link
Contributor

Yes, you're right, so one target to push tagged release and another for latest ones

@andypost
Copy link
Contributor

btw as I got docker buildx create --use each time creating some container so probably we can re-use it to push tagged releases, just need more RTFM)

@lexbritvin
Copy link
Author

For the buildx create usage
With buildx create, you create a new docker-container driver by default.
I think you need to create a build environment only once:

docker buildx create --name my_builder --use
docker builldx ls
docker builldx inspect

FYI You can actually attach different contexts to a builder to support native builds on remote machines (rpi4 or vps) without QEMU, if you are an enthusiast 😉

For me, a preparing step is strange to have in Makefile because it must be done once in the environment, like installing Docker.

@lexbritvin
Copy link
Author

Hello @andypost !
Do you think you can proceed on this issue to provide arm based images?
It will improve performance on the projects drastically.

@lexbritvin
Copy link
Author

Oh, I see you already push to 81 and 82.
Can you please also push once php74 and php80 for older projects?

@andypost
Copy link
Contributor

andypost commented Jun 6, 2023

I can rebuild php:80 but not 7.4 as it finally removed and unsupported (basically not all dependencies are present in Alpinelinux repository to run Drupal)

@andypost
Copy link
Contributor

@lexbritvin please try pull fresh 81-fpm and 81-unit on arm64 machine, I just pushed 8.1.20 for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants