From 2b387bdc41af68bfd14d87ac67034aa9dc99cd66 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 24 Apr 2022 18:35:03 -0500 Subject: [PATCH] docker: add Docker image for ARM. * Have CI build Docker images for both x86 and ARM. * Add a `bin/rails danbooru:docker:build-arm` command for building a Docker image locally for ARM. Usage: * Test the image: docker run --rm -it --platform linux/arm64 ghcr.io/danbooru/danbooru bash * Build the image: bin/rails danbooru:docker:build-arm * Build the image by hand: git archive HEAD | docker buildx build - --platform linux/amd64 --build-arg SOURCE_COMMIT=$(git rev-parse HEAD) -t danbooru -f Dockerfile --load --- .github/workflows/docker-build.yaml | 9 +++++++++ config/docker/Dockerfile.danbooru | 2 +- lib/tasks/danbooru.rake | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.github/workflows/docker-build.yaml b/.github/workflows/docker-build.yaml index f62587e99..6e88f393a 100644 --- a/.github/workflows/docker-build.yaml +++ b/.github/workflows/docker-build.yaml @@ -27,6 +27,13 @@ jobs: runs-on: ubuntu-latest steps: + # https://github.com/docker/setup-qemu-action + # https://github.com/docker/setup-buildx-action#with-qemu + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + with: + platforms: arm64 + # https://github.com/docker/setup-buildx-action - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 @@ -73,6 +80,8 @@ jobs: tags: ${{ steps.metadata.outputs.tags }} labels: ${{ steps.metadata.outputs.labels }} + platforms: linux/amd64,linux/arm64 + # https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md#registry-cache # https://dev.to/pst418/speed-up-multi-stage-docker-builds-in-ci-cd-with-buildkit-s-registry-cache-11gi cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache diff --git a/config/docker/Dockerfile.danbooru b/config/docker/Dockerfile.danbooru index 1c5e65c33..c347d7819 100644 --- a/config/docker/Dockerfile.danbooru +++ b/config/docker/Dockerfile.danbooru @@ -61,7 +61,7 @@ RUN \ FROM base as production -ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 +ENV LD_PRELOAD=libjemalloc.so.2 ENV MALLOC_CONF=background_thread:true,narenas:2,dirty_decay_ms:1000,muzzy_decay_ms:0,tcache:false COPY --from=development /root /root diff --git a/lib/tasks/danbooru.rake b/lib/tasks/danbooru.rake index f6d7e605a..a93c968fb 100644 --- a/lib/tasks/danbooru.rake +++ b/lib/tasks/danbooru.rake @@ -104,11 +104,22 @@ namespace :danbooru do namespace :docker do # Usage: bin/rails danbooru:docker:build # - # Build a Docker image. Note that uncommited changes won't be included in - # the image; commit changes first before building the image. - desc "Build a Docker image based on latest commit" + # Build a Docker image. Note that uncommited changes won't be included in the image; commit changes to Git first before building the image. + desc "Build a Docker image" task :build do - system("git archive HEAD | docker buildx build - --build-arg SOURCE_COMMIT=$(git rev-parse HEAD) -t danbooru -f Dockerfile") + system("git archive HEAD | docker buildx build - --platform linux/amd64 --build-arg SOURCE_COMMIT=$(git rev-parse HEAD) --tag danbooru --tag danbooru:x86 --file Dockerfile --load") + end + + # Usage: bin/rails danbooru:docker:build-arm + # + # Build a Docker image for ARM. You may need to install QEMU first if building on x86. + # + # * sudo apt-get install qemu binfmt-support qemu-user-static # Install QEMU + # * docker run --rm -it --platform linux/arm64 ubuntu # Test that QEMU works + # * docker run --rm -it --platform linux/arm64 danbooru:arm -- bash # Test that the Danbooru image works + desc "Build a Docker image for ARM" + task :"build-arm" do + system("git archive HEAD | docker buildx build - --platform linux/arm64 --build-arg SOURCE_COMMIT=$(git rev-parse HEAD) --tag danbooru --tag danbooru:arm --file Dockerfile --load") end end