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 1c1fdff..abd13ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,20 +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"] - diff --git a/Dockerfile.multiarch b/Dockerfile.multiarch new file mode 100644 index 0000000..abb460f --- /dev/null +++ b/Dockerfile.multiarch @@ -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 new file mode 100644 index 0000000..c9a0555 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +USERNAME := ${DOCKER_USERNAME} + +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 armv7-unknown-linux-musleabihf + cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl + +docker: build + 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 + +.PHONY: build 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 c3d5156..3e95cd2 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 . 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