This commit is contained in:
Sayantan Santra 2024-03-25 03:35:38 -05:00 committed by GitHub
commit 20956feb90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 55 additions and 34 deletions

3
.gitignore vendored
View File

@ -1,5 +1,6 @@
# Ignore cargo build outputs
# Ignore build outputs
actix/target
.docker
# Ignore SQLite file
urls.sqlite

View File

@ -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"]

15
Dockerfile.multiarch Normal file
View File

@ -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"]

19
Makefile Normal file
View File

@ -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

View File

@ -75,10 +75,17 @@ 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=<desired-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
```
docker run -p 4567:4567

View File

@ -1,37 +1,16 @@
#!/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')
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 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,linux/arm/v7 -f Dockerfile.multiarch .
fi