From c5cfba85f99d3dfc7ea9bd3cfde86744da0f18ef Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 19:12:38 -0500 Subject: [PATCH 01/12] chg: Allow specifying target in the Dockerfile --- Dockerfile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1c1fdff..d79faac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,6 @@ +ARG target=x86_64-unknown-linux-musl +ENV target=$target + FROM lukemathwalker/cargo-chef:latest-rust-slim AS chef WORKDIR /chhoto-url @@ -12,15 +15,15 @@ RUN rustup target add x86_64-unknown-linux-musl COPY --from=planner /chhoto-url/recipe.json recipe.json # Build dependencies - this is the caching Docker layer -RUN cargo chef cook --release --target=x86_64-unknown-linux-musl --recipe-path recipe.json +RUN cargo chef cook --release --target=$target --recipe-path recipe.json COPY ./actix/Cargo.toml ./actix/Cargo.lock . COPY ./actix/src ./src # Build application -RUN cargo build --release --target=x86_64-unknown-linux-musl --locked --bin chhoto-url +RUN cargo build --release --target=$target --locked --bin chhoto-url FROM scratch -COPY --from=builder /chhoto-url/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url +COPY --from=builder /chhoto-url/target/$target/release/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] From 9221c3e3711dcaa26cf477a4d83ae14ac38eb411 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 19:38:33 -0500 Subject: [PATCH 02/12] chg: Add support for specifying the architecture --- Dockerfile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index d79faac..b2f6b89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,3 @@ -ARG target=x86_64-unknown-linux-musl -ENV target=$target - FROM lukemathwalker/cargo-chef:latest-rust-slim AS chef WORKDIR /chhoto-url @@ -10,8 +7,9 @@ COPY ./actix/src ./src RUN cargo chef prepare --recipe-path recipe.json FROM chef as builder +ARG target=x86_64-unknown-linux-musl RUN apt-get update && apt-get install -y musl-tools -RUN rustup target add x86_64-unknown-linux-musl +RUN rustup target add $target COPY --from=planner /chhoto-url/recipe.json recipe.json # Build dependencies - this is the caching Docker layer @@ -23,6 +21,7 @@ COPY ./actix/src ./src RUN cargo build --release --target=$target --locked --bin chhoto-url FROM scratch +ARG target=x86_64-unknown-linux-musl COPY --from=builder /chhoto-url/target/$target/release/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] From 5bd174d287500e39e31654c28750e4dd3936402e Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 23:03:18 -0500 Subject: [PATCH 03/12] build: Switched to a cross-rt based multi-arch build process --- Dockerfile | 28 ++++------------------------ Makefile | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 24 deletions(-) create mode 100644 Makefile diff --git a/Dockerfile b/Dockerfile index b2f6b89..567cb8e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,8 @@ -FROM lukemathwalker/cargo-chef:latest-rust-slim AS chef -WORKDIR /chhoto-url - -FROM chef as planner -COPY ./actix/Cargo.toml ./actix/Cargo.lock . -COPY ./actix/src ./src -RUN cargo chef prepare --recipe-path recipe.json - -FROM chef as builder -ARG target=x86_64-unknown-linux-musl -RUN apt-get update && apt-get install -y musl-tools -RUN rustup target add $target - -COPY --from=planner /chhoto-url/recipe.json recipe.json -# Build dependencies - this is the caching Docker layer -RUN cargo chef cook --release --target=$target --recipe-path recipe.json - -COPY ./actix/Cargo.toml ./actix/Cargo.lock . -COPY ./actix/src ./src -# Build application -RUN cargo build --release --target=$target --locked --bin chhoto-url - FROM scratch -ARG target=x86_64-unknown-linux-musl -COPY --from=builder /chhoto-url/target/$target/release/chhoto-url /chhoto-url +ARG ARCH=linux/amd64 + +COPY $ARCH/chhoto-url /chhoto-url COPY ./resources /resources + ENTRYPOINT ["/chhoto-url"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..62da911 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +setup: + cargo install cross + +build: + cross build --release --locked --manifest-path=actix/Cargo.toml --target aarch64-unknown-linux-musl + cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl + +docker: build + mkdir -p linux/amd64 linux/arm64 + cp actix/target/aarch64-unknown-linux-musl/release/chhoto-url linux/aarch64/ + cp actix/target/x86_64-unknown-linux-musl/release/chhoto-url linux/amd64/ + docker buildx -t chhoto-url --platform linux/amd64,linux/arm64,linux/arm/v7 . + rm -rf linux + +clean: + cargo clean --manifest-path=actix/Cargo.toml + +.PHONY: build clean docker From ffb48462390cfc6c8dc4b656dd575fb5c4e13bc0 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 23:27:31 -0500 Subject: [PATCH 04/12] fix: Multi-arch upload --- .gitignore | 3 ++- Dockerfile | 4 ++-- Makefile | 12 +++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index b8d6911..4fd7ce8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -# Ignore cargo build outputs +# Ignore build outputs actix/target +.docker # Ignore SQLite file urls.sqlite diff --git a/Dockerfile b/Dockerfile index 567cb8e..8560074 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM scratch -ARG ARCH=linux/amd64 +ARG TARGETARCH -COPY $ARCH/chhoto-url /chhoto-url +COPY .docker/$TARGETARCH/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] diff --git a/Makefile b/Makefile index 62da911..5a2e232 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,20 @@ setup: cargo install cross + docker buildx create --use --platform=linux/arm64,linux/amd64 --name multi-platform-builder + docker buildx inspect --bootstrap build: cross build --release --locked --manifest-path=actix/Cargo.toml --target aarch64-unknown-linux-musl cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl docker: build - mkdir -p linux/amd64 linux/arm64 - cp actix/target/aarch64-unknown-linux-musl/release/chhoto-url linux/aarch64/ - cp actix/target/x86_64-unknown-linux-musl/release/chhoto-url linux/amd64/ - docker buildx -t chhoto-url --platform linux/amd64,linux/arm64,linux/arm/v7 . - rm -rf linux + mkdir -p .docker/amd64 .docker/arm64 + cp actix/target/aarch64-unknown-linux-musl/release/chhoto-url .docker/arm64/ + cp actix/target/x86_64-unknown-linux-musl/release/chhoto-url .docker/amd64/ + docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64 . clean: cargo clean --manifest-path=actix/Cargo.toml + rm -rf .docker .PHONY: build clean docker From 2baa481040f9bdba21f21af5d8ea27519c345ec1 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 23:33:41 -0500 Subject: [PATCH 05/12] chg: Change the push script to work with the new setup --- Makefile | 2 +- docker_push_script.sh | 28 +++------------------------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 5a2e232..8aa8dac 100644 --- a/Makefile +++ b/Makefile @@ -17,4 +17,4 @@ clean: cargo clean --manifest-path=actix/Cargo.toml rm -rf .docker -.PHONY: build clean docker +.PHONY: build diff --git a/docker_push_script.sh b/docker_push_script.sh index c3d5156..e5d7eba 100755 --- a/docker_push_script.sh +++ b/docker_push_script.sh @@ -1,10 +1,7 @@ #!/bin/env bash if [ "$1" == "dev" ]; then - name="chhoto-url" - docker build -t $name . - docker tag $name sintan1729/$name:dev - docker push sintan1729/$name:dev + docker buildx build --push --tag sintan1729/$name:dev --platform linux/amd64 elif [ "$1" == "release" ]; then v_patch=$(cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$/\1/p') @@ -12,26 +9,7 @@ elif [ "$1" == "release" ]; then v_major=$(echo $v_minor | sed -rn 's/^(.+)\..+$/\1/p') name="chhoto-url" - - docker build -t $name . - - for tag in $v_major $v_minor $v_patch latest - do - docker tag $name sintan1729/$name:$tag - done - - echo "Do you want to push these to Docker Hub?" - select yn in "Yes" "No"; - do - if [ "$yn"="Yes" ]; then - for tag in $v_major $v_minor $v_patch latest - do - docker push sintan1729/$name:$tag - done - else - echo "Okay! Not pushing." - fi - break - done + docker buildx build --push --tag sintan1729/$name:$v_major --tag sintan1729/$v_minor: \ + --tag sintan1729/$name:$v_patch --tag sintan1729/$name:latest --platform linux/amd64,linux/arm64 fi From 82559d38fd194ff1ca8afd80ab17c6678056756e Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 00:56:39 -0500 Subject: [PATCH 06/12] chg: Nicer multi-arch builds and also add armv7 --- Dockerfile | 8 -------- Dockerfile-for-push-script | 15 +++++++++++++++ Makefile | 6 ++---- 3 files changed, 17 insertions(+), 12 deletions(-) delete mode 100644 Dockerfile create mode 100644 Dockerfile-for-push-script diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 8560074..0000000 --- a/Dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -FROM scratch -ARG TARGETARCH - -COPY .docker/$TARGETARCH/chhoto-url /chhoto-url -COPY ./resources /resources - -ENTRYPOINT ["/chhoto-url"] - diff --git a/Dockerfile-for-push-script b/Dockerfile-for-push-script new file mode 100644 index 0000000..abb460f --- /dev/null +++ b/Dockerfile-for-push-script @@ -0,0 +1,15 @@ +FROM scratch as builder-amd64 +COPY ./actix/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url + +FROM scratch as builder-arm64 +COPY ./actix/target/aarch64-unknown-linux-musl/release/chhoto-url /chhoto-url + +FROM scratch as builder-arm +COPY ./actix/target/armv7-unknown-linux-musleabihf/release/chhoto-url /chhoto-url + +ARG TARGETARCH +FROM builder-$TARGETARCH +COPY ./resources /resources + +ENTRYPOINT ["/chhoto-url"] + diff --git a/Makefile b/Makefile index 8aa8dac..55480f1 100644 --- a/Makefile +++ b/Makefile @@ -5,13 +5,11 @@ setup: build: cross build --release --locked --manifest-path=actix/Cargo.toml --target aarch64-unknown-linux-musl + cross build --release --locked --manifest-path=actix/Cargo.toml --target armv7-unknown-linux-musleabihf cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl docker: build - mkdir -p .docker/amd64 .docker/arm64 - cp actix/target/aarch64-unknown-linux-musl/release/chhoto-url .docker/arm64/ - cp actix/target/x86_64-unknown-linux-musl/release/chhoto-url .docker/amd64/ - docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64 . + docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 . clean: cargo clean --manifest-path=actix/Cargo.toml From e54aa3b33b2e14e061af6caa67cee49e5592620c Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:00:25 -0500 Subject: [PATCH 07/12] build: Use different dockerfiles for single and multi-arch builds --- Dockerfile | 26 +++++++++++++++++++ ...le-for-push-script => Dockerfile.multiarch | 0 Makefile | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 Dockerfile rename Dockerfile-for-push-script => Dockerfile.multiarch (100%) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1c1fdff --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM lukemathwalker/cargo-chef:latest-rust-slim AS chef +WORKDIR /chhoto-url + +FROM chef as planner +COPY ./actix/Cargo.toml ./actix/Cargo.lock . +COPY ./actix/src ./src +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef as builder +RUN apt-get update && apt-get install -y musl-tools +RUN rustup target add x86_64-unknown-linux-musl + +COPY --from=planner /chhoto-url/recipe.json recipe.json +# Build dependencies - this is the caching Docker layer +RUN cargo chef cook --release --target=x86_64-unknown-linux-musl --recipe-path recipe.json + +COPY ./actix/Cargo.toml ./actix/Cargo.lock . +COPY ./actix/src ./src +# Build application +RUN cargo build --release --target=x86_64-unknown-linux-musl --locked --bin chhoto-url + +FROM scratch +COPY --from=builder /chhoto-url/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url +COPY ./resources /resources +ENTRYPOINT ["/chhoto-url"] + diff --git a/Dockerfile-for-push-script b/Dockerfile.multiarch similarity index 100% rename from Dockerfile-for-push-script rename to Dockerfile.multiarch diff --git a/Makefile b/Makefile index 55480f1..457dd34 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ build: cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl docker: build - docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 . + docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . clean: cargo clean --manifest-path=actix/Cargo.toml From db5d1f72bd56eabfc86ba0dba844605f503df0aa Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:05:06 -0500 Subject: [PATCH 08/12] chg: Use username as a variable in Makefile --- Dockerfile | 1 - Makefile | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1c1fdff..070b64d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,4 +23,3 @@ FROM scratch COPY --from=builder /chhoto-url/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] - diff --git a/Makefile b/Makefile index 457dd34..7b56ab2 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +USERNAME := ${USERNAME} + setup: cargo install cross docker buildx create --use --platform=linux/arm64,linux/amd64 --name multi-platform-builder @@ -9,10 +11,9 @@ build: cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl docker: build - docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . + docker buildx build --push --tag ${USERNAME}/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . clean: cargo clean --manifest-path=actix/Cargo.toml - rm -rf .docker .PHONY: build From ce76f04f35e725dd8a2cbd8698f7703ed931b849 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:10:09 -0500 Subject: [PATCH 09/12] build: Support specifying targets with build-arg --- Dockerfile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 070b64d..abd13ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,19 +7,20 @@ COPY ./actix/src ./src RUN cargo chef prepare --recipe-path recipe.json FROM chef as builder +ARG target=x86_64-unknown-linux-musl RUN apt-get update && apt-get install -y musl-tools -RUN rustup target add x86_64-unknown-linux-musl +RUN rustup target add $target COPY --from=planner /chhoto-url/recipe.json recipe.json # Build dependencies - this is the caching Docker layer -RUN cargo chef cook --release --target=x86_64-unknown-linux-musl --recipe-path recipe.json +RUN cargo chef cook --release --target=$target --recipe-path recipe.json COPY ./actix/Cargo.toml ./actix/Cargo.lock . COPY ./actix/src ./src # Build application -RUN cargo build --release --target=x86_64-unknown-linux-musl --locked --bin chhoto-url +RUN cargo build --release --target=$target --locked --bin chhoto-url FROM scratch -COPY --from=builder /chhoto-url/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url +COPY --from=builder /chhoto-url/target/$target/release/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] From 629e66a57c9657fe234b974016bd90fba7a0ba1b Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:22:54 -0500 Subject: [PATCH 10/12] docs: Updated build instructions --- Makefile | 2 +- README.md | 10 ++++++++-- docker_push_script.sh | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 7b56ab2..c9a0555 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -USERNAME := ${USERNAME} +USERNAME := ${DOCKER_USERNAME} setup: cargo install cross diff --git a/README.md b/README.md index 993442f..b70e5a6 100644 --- a/README.md +++ b/README.md @@ -75,10 +75,16 @@ place, resulting in possibly unwanted behavior. ## Building and running with docker ### `docker run` method -0. (Only if you really want to) Build the image +0. (Only if you really want to) Build the image for the default `x86_64-unknown-linux-musl` target: ``` -docker build . -t chhoto-url:latest +docker build . -t chhoto-url ``` +For building on `arm64` or `arm/v7`, use the following: +``` +docker build . -t chhoto-url --build-arg target= +``` +For cross-compilation, take a look at the `Makefile`. It builds and pushes for `linux/amd64`, `linux/aarch64` +and `linux/arm/v7` architectures. For any other architectures, open a discussion, and I'll try to help you out. 1. Run the image ``` docker run -p 4567:4567 diff --git a/docker_push_script.sh b/docker_push_script.sh index e5d7eba..3e95cd2 100755 --- a/docker_push_script.sh +++ b/docker_push_script.sh @@ -1,7 +1,7 @@ #!/bin/env bash if [ "$1" == "dev" ]; then - docker buildx build --push --tag sintan1729/$name:dev --platform linux/amd64 + docker buildx build --push --tag sintan1729/$name:dev . elif [ "$1" == "release" ]; then v_patch=$(cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$/\1/p') From 6d3d220cff74027ebd1cd7928737a4984e9d088d Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:26:13 -0500 Subject: [PATCH 11/12] build: Fix the docker_push_script --- docker_push_script.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docker_push_script.sh b/docker_push_script.sh index 3e95cd2..7a1787b 100755 --- a/docker_push_script.sh +++ b/docker_push_script.sh @@ -7,9 +7,10 @@ elif [ "$1" == "release" ]; then v_patch=$(cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$/\1/p') v_minor=$(echo $v_patch | sed -rn 's/^(.+\..+)\..+$/\1/p') v_major=$(echo $v_minor | sed -rn 's/^(.+)\..+$/\1/p') - + + make build name="chhoto-url" docker buildx build --push --tag sintan1729/$name:$v_major --tag sintan1729/$v_minor: \ - --tag sintan1729/$name:$v_patch --tag sintan1729/$name:latest --platform linux/amd64,linux/arm64 + --tag sintan1729/$name:$v_patch --tag sintan1729/$name:latest --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . fi From dfefff2703074fc8e76c90f710be0d28b56f0295 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:40:10 -0500 Subject: [PATCH 12/12] docs: Added a note about building --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b70e5a6..0e019be 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ For building on `arm64` or `arm/v7`, use the following: ``` docker build . -t chhoto-url --build-arg target= ``` +Make sure that the desired target is a `musl` one, since the docker image is built from `scratch`. For cross-compilation, take a look at the `Makefile`. It builds and pushes for `linux/amd64`, `linux/aarch64` and `linux/arm/v7` architectures. For any other architectures, open a discussion, and I'll try to help you out. 1. Run the image