Skip to content

Commit

Permalink
Switch base image to alpine:edge
Browse files Browse the repository at this point in the history
  • Loading branch information
TerrorJack committed Mar 25, 2024
1 parent df00945 commit 61e1081
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
25 changes: 11 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: build

on:
push:
branches:
- master
pull_request:
workflow_dispatch:

Expand All @@ -14,25 +16,19 @@ jobs:
matrix:
arch:
- amd64
- arm64v8
- arm64/v8
steps:

- name: install-deps
run: |
sudo apt update
sudo apt install -y \
qemu-user-static
- name: setup-podman
uses: TerrorJack/setup-podman@master

- name: checkout
uses: actions/checkout@v3

- name: patch-dockerfile
run: |
sed -i 's@rust:alpine@${{ matrix.arch }}/rust:alpine@' Dockerfile
uses: actions/checkout@v4

- name: build-image
run: |
podman build \
--arch ${{ matrix.arch }} \
--network host \
--pull \
--squash-all \
Expand All @@ -42,13 +38,14 @@ jobs:
- name: test-image
run: |
podman run \
--arch ${{ matrix.arch }} \
--init \
--network host \
--rm \
--tmpfs /tmp:exec \
--volume $PWD:/workspace \
--workdir /workspace \
rust:alpine-mimalloc \
sh -c 'cargo install names && mv $CARGO_HOME/bin/names .'
MIMALLOC_VERBOSE=1 ./names
file ./names
/workspace/test.sh
MIMALLOC_VERBOSE=1 ./bin/names
file ./bin/names
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:alpine
FROM alpine:edge

COPY build.sh mimalloc.diff /tmp/

Expand Down
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
# `rust-alpine-mimalloc`

This Docker image builds upon the official `rust:alpine` image and
replaces the default musl malloc implementation with
[`mimalloc`](https://github.com/microsoft/mimalloc). If you build Rust
or C/C++ static executables in this image, the resulting executables
will automatically link with `mimalloc` without needing any special
build flags.

Supported & tested archs: `amd64` and `arm64v8`.
This Docker image builds upon the `alpine:edge` image, provides
`cargo`/`rustc` and replaces the default musl malloc implementation
with [`mimalloc`](https://github.com/microsoft/mimalloc). If you build
Rust or C/C++ static executables in this image, the resulting
executables will automatically link with `mimalloc` without needing
any special build flags.

Notice: we switched away from `rust:alpine` and now uses
`cargo`/`rust` packaged by alpine. Statically linked executables will
continue to link against `mimalloc` as intended, but you need extra
command-line arguments to ensure they are indeed static:

```sh
$ RUSTFLAGS="-C target-feature=+crt-static" cargo install --target x86_64-alpine-linux-musl foo
```

The `--target` flag is required. The default target is either
`x86_64-alpine-linux-musl` or `aarch64-alpine-linux-musl`, and can
also be extracted from `$(rustc -vV | sed -n "s|host: ||p")`.

Supported & tested archs: `amd64` and `arm64/v8`.

For more details, see this [blog
post](https://www.tweag.io/blog/2023-08-10-rust-static-link-with-mimalloc).
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ apk upgrade --no-cache

apk add --no-cache \
alpine-sdk \
cargo \
cmake \
curl \
mold \
samurai

Expand Down
7 changes: 7 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

set -eu

TARGET=$(rustc -vV | sed -n "s|host: ||p")

RUSTFLAGS="-C target-feature=+crt-static" exec cargo install --root "$PWD" --target "$TARGET" names

0 comments on commit 61e1081

Please sign in to comment.