Skip to content

Commit 8eade63

Browse files
committed
chore: adapt Dockerfile from twilight/http-proxy
1 parent 8855ca1 commit 8eade63

2 files changed

Lines changed: 25 additions & 78 deletions

File tree

.github/workflows/docker.yml

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,20 @@ on:
1010
jobs:
1111
build-images:
1212
name: Build Docker Images
13-
runs-on: ubuntu-latest
13+
1414
strategy:
1515
matrix:
1616
include:
1717
- tag: amd64
18-
arch: amd64
19-
rust-target: x86_64-unknown-linux-musl
20-
musl-target: x86_64-linux-musl
18+
features: ""
19+
image: ubuntu-latest
2120
- tag: armv8
22-
arch: armv8
23-
rust-target: aarch64-unknown-linux-musl
24-
musl-target: aarch64-linux-musl
21+
features: ""
22+
image: ubuntu-24.04-arm
2523

26-
steps:
27-
# Podman 4.x is necessary here because it supports --platform=$BUILDPLATFORM. Otherwise, podman
28-
# would pull the base image for aarch64 when building for aarch64. See https://github.com/containers/buildah/pull/3757
29-
# for the implementation. GitHub actions currently still ship Podman 3.x, even though 4.x has been
30-
# out for over a year.
31-
# The repository used is the same as GitHub actions uses for their source - just that it's the unstable version
32-
# rather than the stable one.
33-
# TODO: Once podman 4.x is available in actions by default (or in the Ubuntu repositories), remove this.
34-
- name: Install podman 4.x
35-
run: |
36-
sudo mkdir -p /etc/apt/keyrings
37-
curl -fsSL https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_$(lsb_release -rs)/Release.key \
38-
| gpg --dearmor \
39-
| sudo tee /etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg > /dev/null
40-
echo \
41-
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg]\
42-
https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_$(lsb_release -rs)/ /" \
43-
| sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:unstable.list > /dev/null
44-
sudo apt -qq -y purge buildah podman
45-
sudo apt -qq -y autoremove --purge
46-
sudo apt update -qq
47-
sudo apt -qq -y install podman
24+
runs-on: ${{ matrix.image }}
4825

26+
steps:
4927
- name: Checkout sources
5028
uses: actions/checkout@v3
5129

@@ -62,13 +40,11 @@ jobs:
6240
run: |
6341
podman build \
6442
--format docker \
65-
--arch ${{ matrix.arch }} \
66-
--build-arg RUST_TARGET=${{ matrix.rust-target }} \
67-
--build-arg MUSL_TARGET=${{ matrix.musl-target }} \
43+
--build-arg FEATURES=${{ matrix.features }} \
6844
-t gateway-queue:${{ matrix.tag }} \
6945
.
7046
71-
- name: Push image to ghcr
47+
- name: Push image
7248
if: github.ref == 'refs/heads/trunk' && github.event_name != 'pull_request'
7349
run: |
7450
podman tag gateway-queue:${{ matrix.tag }} ghcr.io/${REPO}:${{ matrix.tag }}

Dockerfile

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,13 @@
1-
# Rust syntax target, either x86_64-unknown-linux-musl, aarch64-unknown-linux-musl, arm-unknown-linux-musleabi etc.
2-
ARG RUST_TARGET="x86_64-unknown-linux-musl"
3-
# Musl target, either x86_64-linux-musl, aarch64-linux-musl, arm-linux-musleabi, etc.
4-
ARG MUSL_TARGET="x86_64-linux-musl"
1+
# The crate features to build this with
2+
ARG FEATURES=""
53

6-
FROM --platform=$BUILDPLATFORM docker.io/alpine:latest as build
7-
ARG RUST_TARGET
8-
ARG MUSL_TARGET
4+
FROM docker.io/library/alpine:latest as build
5+
ARG FEATURES
96

107
RUN apk upgrade && \
118
apk add curl gcc musl-dev && \
129
curl -sSf https://sh.rustup.rs | sh -s -- --profile minimal --default-toolchain nightly --component rust-src -y
1310

14-
RUN source $HOME/.cargo/env && \
15-
mkdir -p /app/.cargo && \
16-
if [ "$RUST_TARGET" != $(rustup target list --installed) ]; then \
17-
rustup target add $RUST_TARGET && \
18-
curl -L "https://musl.cc/$MUSL_TARGET-cross.tgz" -o /toolchain.tgz && \
19-
tar xf toolchain.tgz && \
20-
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-gcc" "/usr/bin/$MUSL_TARGET-gcc" && \
21-
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-ld" "/usr/bin/$MUSL_TARGET-ld" && \
22-
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-strip" "/usr/bin/actual-strip" && \
23-
GCC_VERSION=$($MUSL_TARGET-gcc --version | grep gcc | awk '{print $3}') && \
24-
echo -e "\
25-
[build]\n\
26-
rustflags = [\"-L\", \"native=/$MUSL_TARGET-cross/$MUSL_TARGET/lib\", \"-L\", \"native=/$MUSL_TARGET-cross/lib/gcc/$MUSL_TARGET/$GCC_VERSION/\", \"-l\", \"static=gcc\", \"-Clink-self-contained=y\", \"-Clinker-flavor=gcc\"]\n\
27-
[target.$RUST_TARGET]\n\
28-
linker = \"$MUSL_TARGET-gcc\"\n\
29-
[unstable]\n\
30-
build-std = [\"std\", \"panic_abort\"]\n\
31-
" > /app/.cargo/config; \
32-
else \
33-
echo "skipping toolchain as we are native" && \
34-
echo -e "\
35-
[build]\n\
36-
rustflags = [\"-L\", \"native=/usr/lib\"]\n\
37-
[unstable]\n\
38-
build-std = [\"std\", \"panic_abort\"]\n\
39-
" > /app/.cargo/config && \
40-
ln -s /usr/bin/strip /usr/bin/actual-strip; \
41-
fi
42-
4311
WORKDIR /app
4412

4513
COPY ./Cargo.lock ./Cargo.lock
@@ -50,8 +18,11 @@ COPY ./Cargo.toml ./Cargo.toml
5018
RUN mkdir src/
5119
RUN echo 'fn main() {}' > ./src/main.rs
5220
RUN source $HOME/.cargo/env && \
53-
cargo build --release \
54-
--target="$RUST_TARGET"
21+
cargo build \
22+
--release \
23+
-Zbuild-std=std,panic_abort \
24+
--target="$(uname -m)-unknown-linux-musl" \
25+
--features="$FEATURES"
5526

5627
# Now, delete the fake source and copy in the actual source. This allows us to
5728
# have a previous compilation step for compiling the dependencies, while being
@@ -63,19 +34,19 @@ RUN source $HOME/.cargo/env && \
6334
# would have to be re-downloaded and re-compiled.
6435
#
6536
# Also, remove the artifacts of building the binaries.
66-
RUN rm -f target/$RUST_TARGET/release/deps/twilight_gateway_queue*
37+
RUN rm -f target/$(uname -m)-unknown-linux-musl/release/deps/twilight_gateway_queue*
6738
COPY ./src ./src
6839

6940
RUN source $HOME/.cargo/env && \
70-
cargo build --release \
71-
--target="$RUST_TARGET" && \
72-
cp target/$RUST_TARGET/release/twilight-gateway-queue /twilight-gateway-queue && \
73-
actual-strip /twilight-gateway-queue
41+
cargo build \
42+
--release \
43+
-Zbuild-std=std,panic_abort \
44+
--target="$(uname -m)-unknown-linux-musl" --features="$FEATURES" && \
45+
cp target/$(uname -m)-unknown-linux-musl/release/twilight-gateway-queue /twilight-gateway-queue && \
46+
strip /twilight-gateway-queue
7447

7548
FROM scratch
7649

77-
# And now copy the binary over from the build container. The build container is
78-
# based on a heavy image.
7950
COPY --from=build /twilight-gateway-queue /twilight-gateway-queue
8051

8152
CMD ["./twilight-gateway-queue"]

0 commit comments

Comments
 (0)