Skip to content

Commit 0886e3a

Browse files
authored
build: optimize cross-platform Docker build process (#6)
* build: use `xx` to cross-compile * docs: remove deprecated version for docker-compose.yml * fix: set up `compiler-base` without specifying platform * fix: use `ubuntu:24.04` as runner * fix: properly cross-compile * fix: install glslang-tools before * build: set up proper caching
1 parent 6eb4e08 commit 0886e3a

File tree

4 files changed

+71
-41
lines changed

4 files changed

+71
-41
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ node_modules
44
main
55
dist
66
.svelte-kit
7+
Dockerfile
8+
docker-compose.yml

.github/workflows/ci.yml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: ci
33
on:
44
push:
55
branches:
6-
- "master"
6+
- "*"
77
tags:
88
- "v*"
99
pull_request:
@@ -27,15 +27,15 @@ jobs:
2727
- name: Set up Docker Buildx
2828
uses: docker/setup-buildx-action@v3
2929

30-
- name: Log in to the Container registry
30+
- name: Log in to the Container Registry
3131
if: github.event_name != 'pull_request'
3232
uses: docker/login-action@v3
3333
with:
3434
registry: ${{ env.REGISTRY }}
3535
username: ${{ github.actor }}
3636
password: ${{ secrets.GITHUB_TOKEN }}
3737

38-
- name: Extract metadata for docker
38+
- name: Extract metadata for Docker
3939
id: meta
4040
uses: docker/metadata-action@v5
4141
with:
@@ -45,13 +45,19 @@ jobs:
4545
tags: |
4646
type=sha
4747
48-
- name: Build and push
48+
- name: Build and push multi-platform image
4949
uses: docker/build-push-action@v5
5050
with:
5151
context: .
5252
platforms: linux/amd64,linux/arm64
53-
push: ${{ github.event_name != 'pull_request' }}
53+
push: ${{ github.ref == 'refs/heads/master' && github.event_name != 'pull_request' }}
5454
tags: ${{ steps.meta.outputs.tags }}
5555
labels: ${{ steps.meta.outputs.labels }}
56-
cache-from: type=gha
57-
cache-to: type=gha,mode=max
56+
cache-from: |
57+
type=gha,scope=downloader-${{ github.ref }}
58+
type=gha,scope=compiler-${{ github.ref }}
59+
cache-to: |
60+
type=gha,mode=max,scope=downloader-${{ github.ref }}
61+
type=gha,mode=max,scope=compiler-${{ github.ref }}
62+
build-args: |
63+
BUILDKIT_INLINE_CACHE=1

Dockerfile

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,89 @@
11
# Download stage for Real-ESRGAN models
2-
FROM alpine:3.19 AS downloader
2+
FROM --platform=$BUILDPLATFORM ubuntu:24.04 AS downloader
33
WORKDIR /download
44
ARG REALESRGAN_URL="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesrgan-ncnn-vulkan-20220424-ubuntu.zip"
5-
RUN apk add --no-cache wget unzip \
6-
&& mkdir -p upscaler \
7-
&& wget -q "${REALESRGAN_URL}" -O upscaler/realesrgan.zip \
8-
&& unzip -j upscaler/realesrgan.zip "*models*" -d upscaler/models-realesrgan \
9-
&& rm -rf upscaler/*.zip
5+
RUN --mount=type=cache,target=/download/cache,id=realesrgan-download \
6+
apt-get update && apt-get install -y wget unzip && \
7+
if [ ! -f /download/cache/realesrgan.zip ]; then \
8+
mkdir -p /download/cache && \
9+
wget -q "${REALESRGAN_URL}" -O /download/cache/realesrgan.zip; \
10+
fi && \
11+
mkdir -p upscaler && \
12+
cp /download/cache/realesrgan.zip upscaler/ && \
13+
unzip -j upscaler/realesrgan.zip "*models*" -d upscaler/models-realesrgan && \
14+
rm -rf upscaler/*.zip
1015

1116
# Base compiler stage with common dependencies
12-
FROM alpine:3.19 AS compiler-base
13-
RUN apk add --no-cache git vulkan-headers vulkan-loader-dev glslang cmake make gcc g++
17+
FROM --platform=$BUILDPLATFORM tonistiigi/xx AS xx
18+
FROM --platform=$BUILDPLATFORM ubuntu:24.04 AS compiler-base
19+
COPY --from=xx / /
20+
RUN apt-get update -y && apt-get install -y \
21+
git \
22+
cmake \
23+
make \
24+
lld \
25+
clang \
26+
pkg-config \
27+
crossbuild-essential-arm64 \
28+
libgcc-12-dev-arm64-cross \
29+
libc6-dev-arm64-cross \
30+
glslang-tools
31+
32+
ARG TARGETPLATFORM
33+
RUN xx-apt-get install -y libvulkan-dev
1434

1535
# Compile stage for waifu2x
16-
FROM compiler-base AS waifu2x-compiler
36+
FROM --platform=$BUILDPLATFORM compiler-base AS waifu2x-compiler
1737
WORKDIR /app
18-
RUN git clone --depth 1 https://github.com/nihui/waifu2x-ncnn-vulkan.git waifu2x-ncnn-vulkan
38+
RUN --mount=type=cache,target=/root/.cache/git \
39+
git clone --depth 1 https://github.com/nihui/waifu2x-ncnn-vulkan.git waifu2x-ncnn-vulkan
1940
WORKDIR /app/waifu2x-ncnn-vulkan
20-
RUN git submodule update --init --recursive \
41+
RUN --mount=type=cache,target=/root/.cache/git \
42+
git submodule update --init --recursive \
2143
&& mkdir build && cd build \
22-
&& cmake ../src && cmake --build . -j "$(nproc)"
44+
&& cmake -DNCNN_SSE2=OFF \
45+
$(xx-clang --print-cmake-defines) ../src && cmake --build . -j "$(nproc)"
2346

2447
# Compile stage for Real-ESRGAN
25-
FROM compiler-base AS realesrgan-compiler
48+
FROM --platform=$BUILDPLATFORM compiler-base AS realesrgan-compiler
2649
WORKDIR /app
27-
RUN git clone --depth 1 https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan Real-ESRGAN-ncnn-vulkan
50+
RUN --mount=type=cache,target=/root/.cache/git \
51+
git clone --depth 1 https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan Real-ESRGAN-ncnn-vulkan
2852
WORKDIR /app/Real-ESRGAN-ncnn-vulkan
29-
RUN sed -i 's|git@github.com:|https://github.com/|g' .gitmodules \
53+
RUN --mount=type=cache,target=/root/.cache/git \
54+
sed -i 's|git@github.com:|https://github.com/|g' .gitmodules \
3055
&& git submodule update --init --recursive \
3156
&& mkdir build && cd build \
32-
&& cmake ../src && cmake --build . -j "$(nproc)"
57+
&& cmake -DNCNN_SSE2=OFF \
58+
$(xx-clang --print-cmake-defines) ../src && cmake --build . -j "$(nproc)"
3359

3460
# Build stage for web application
35-
FROM oven/bun:1-alpine AS webbuilder
61+
FROM --platform=$BUILDPLATFORM oven/bun:1-alpine AS webbuilder
3662
WORKDIR /app/web
3763
COPY web/package.json web/bun.lockb ./
3864
RUN bun install --frozen-lockfile
3965
COPY web/ .
40-
ARG NODE_ENV=production
41-
ENV NODE_ENV=${NODE_ENV}
4266
RUN bun run build
4367

4468
# Build stage for Go API
45-
FROM golang:1.22-alpine AS apibuilder
69+
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS apibuilder
70+
COPY --from=xx / /
4671
WORKDIR /app
4772
COPY go.* ./
4873
RUN go mod download
4974
COPY . .
50-
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o miyo cmd/main.go
51-
52-
# Final stage
53-
FROM alpine:3.19 AS runner
54-
5575
ARG TARGETPLATFORM
76+
RUN xx-go --wrap
77+
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -a -installsuffix cgo -o miyo cmd/main.go
5678

57-
RUN apk update && \
58-
if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
59-
apk add --no-cache libgomp vulkan-tools mesa-vulkan-ati mesa-vulkan-layers libgcc; \
60-
else \
61-
apk add --no-cache libgomp vulkan-tools mesa-vulkan-ati mesa-vulkan-intel mesa-vulkan-layers libgcc; \
62-
fi
79+
# Final stage
80+
FROM ubuntu:24.04 AS runner
81+
RUN apt-get update && apt-get install -y --no-install-recommends \
82+
libgomp1 \
83+
vulkan-tools \
84+
mesa-vulkan-drivers \
85+
vulkan-validationlayers \
86+
&& rm -rf /var/lib/apt/lists/*
6387

6488
WORKDIR /app
6589

@@ -69,5 +93,5 @@ COPY --from=downloader /download/upscaler/. upscaler/
6993
COPY --from=apibuilder /app/miyo .
7094
COPY --from=apibuilder /app/out out/
7195
COPY --from=webbuilder /app/dist dist/
72-
EXPOSE 9452
96+
EXPOSE 9452/tcp
7397
CMD ["/app/miyo"]

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ This is the simplest way to deploy `miyo` using `docker-compose`.
2929
However, NVIDIA GPUs are not supported in this deployment method.
3030

3131
```yaml
32-
version: "3.8"
33-
3432
services:
3533
miyo:
3634
container_name: miyo

0 commit comments

Comments
 (0)