Skip to content

Commit 808f771

Browse files
committed
Stabilize and automate the dev container build
This commit resolves a series of build and runtime errors to create a stable, portable, and fully automated dev container environment that works on both `arm64` and `x86_64` architectures out-of-the-box. - Stabilize Dockerfile Build: - Upgrades the base image from `bullseye` to `bookworm`. - Consolidates all `apt-get` dependencies into a single, correctly ordered layer, installing necessary tools for cross-compilation (`gcc-x86-64-linux-gnu`, `libc6-dev-amd64-cross`). - Fixes `rustup` permission errors by installing the toolchain as `root` and granting ownership to the `vscode` user. - Adds `--break-system-packages` to the `pip install` command to comply with Debian `bookworm`'s package management policies. - Improve Architecture Portability: - Makes the `bin/build` and `bin/test` scripts architecture-aware, allowing them to run seamlessly on both `arm64` and `x86_64` hosts without manual configuration. - Fixes a bug that caused inconsistent naming of the shared library (`.so`) file between build and test runs. - Fix Container Startup on ARM64: - Centralizes QEMU and `binfmt` setup within the `Dockerfile` build, creating an architecture-aware initialization process. - This allows for the removal of legacy, conflicting setup methods that caused startup failures on `arm64` hosts: - Removes the privileged `docker run` command for `qemu-user-static` from the `postCreate` script. - Disables the redundant QEMU setup in the `docker-in-docker` feature by configuring `install-qemu: false` for the feature.
1 parent 1d0afd4 commit 808f771

File tree

6 files changed

+82
-37
lines changed

6 files changed

+82
-37
lines changed

.devcontainer/Dockerfile

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1-
FROM mcr.microsoft.com/devcontainers/rust:1-1-bullseye
1+
FROM mcr.microsoft.com/devcontainers/rust:2-1-bookworm
22

3-
RUN sudo apt-get update -y \
4-
&& sudo apt-get upgrade -y
3+
RUN apt-get update -y \
4+
&& apt-get upgrade -y \
5+
&& apt-get install -y --fix-missing --no-install-recommends \
6+
zip \
7+
qemu-system \
8+
binfmt-support \
9+
qemu-user-static \
10+
nodejs \
11+
ruby \
12+
php \
13+
php-common \
14+
python3-pip \
15+
gcc-x86-64-linux-gnu \
16+
libc6-dev-amd64-cross \
17+
&& rm -rf /var/lib/apt/lists/* \
18+
&& update-binfmts --enable qemu-aarch64
519

6-
RUN sudo apt-get install -y --fix-missing zip
20+
# Easy way to install Python.
21+
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1
722

8-
RUN sudo apt-get update -y \
9-
&& sudo apt-get upgrade -y \
10-
&& sudo apt-get install -y zip ltrace
23+
# Switch to root to install rust targets and fix permissions
24+
USER root
1125

12-
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
26+
# Install rust targets for cross-compilation
1327
RUN rustup update \
14-
&& rustup target add aarch64-unknown-linux-gnu
28+
&& rustup default stable \
29+
&& rustup target add aarch64-unknown-linux-gnu \
30+
&& rustup target add x86_64-unknown-linux-gnu
1531

16-
RUN rustup default stable
17-
# x86_64 to arm64 support.
18-
RUN sudo apt-get install -y \
19-
qemu \
20-
binfmt-support \
21-
qemu-user-static
32+
# Grant vscode user ownership of rustup and cargo directories
33+
RUN chown -R vscode:vscode /usr/local/rustup /usr/local/cargo
2234

23-
# Easy way to install node, ruby, and php
24-
RUN apt-get -y install nodejs ruby php php-common
25-
26-
# Easy way to install Python.
27-
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1
35+
# Switch back to vscode user
36+
USER vscode
2837

2938
# Multi-platform SAM CLI. https://github.com/aws/aws-sam-cli/issues/3908
30-
RUN apt-get install -y pip && pip install aws-sam-cli
39+
RUN pip install aws-sam-cli --break-system-packages

.devcontainer/devcontainer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
},
66
"features": {
77
"ghcr.io/devcontainers/features/aws-cli:latest": {},
8-
"ghcr.io/devcontainers/features/docker-in-docker:latest": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:latest": {
9+
"install-qemu": false
10+
},
911
"ghcr.io/customink/codespaces-features/docker-log-level": {},
1012
"ghcr.io/devcontainers/features/sshd:latest": {}
1113
},

.devcontainer/postCreate

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/sh
22
set -e
33

4-
docker run \
5-
--rm \
6-
--privileged \
7-
multiarch/qemu-user-static \
8-
--reset -p yes
4+
# Wait for docker to be ready
5+
while ! docker info > /dev/null 2>&1; do
6+
echo "Waiting for docker daemon..."
7+
sleep 1
8+
done

bin/build

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,23 @@ set -e
33

44
# Sync with bin/build, bin/test, & test/libcrypteia.sh.
55
export CRYPTEIA_BUILD_OS="${CRYPTEIA_BUILD_OS:=debian}"
6-
export CRYPTEIA_BUILD_TARGET="${CRYPTEIA_BUILD_TARGET:=x86_64-unknown-linux-gnu}"
6+
7+
# Auto-detect build target if not already set
8+
if [ -z "${CRYPTEIA_BUILD_TARGET}" ]; then
9+
case "$(uname -m)" in
10+
aarch64)
11+
export CRYPTEIA_BUILD_TARGET="aarch64-unknown-linux-gnu"
12+
;;
13+
x86_64)
14+
export CRYPTEIA_BUILD_TARGET="x86_64-unknown-linux-gnu"
15+
;;
16+
*)
17+
echo "Unsupported architecture: $(uname -m)"
18+
exit 1
19+
;;
20+
esac
21+
fi
22+
723
if [ "${CRYPTEIA_BUILD_TARGET}" = "aarch64-unknown-linux-gnu" ]; then
824
export CRYPTEIA_BUILD_SUFFIX="-arm64"
925
fi

bin/build-arch

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ cargo build \
1616
--target "${CRYPTEIA_BUILD_TARGET}"
1717

1818
cp "./target/${CRYPTEIA_BUILD_TARGET}/release/crypteia" "./build/${BIN}"
19-
cp ./target/${CRYPTEIA_BUILD_TARGET}/release/libcrypteia.so "./build/${LIB}"
19+
cp "./target/${CRYPTEIA_BUILD_TARGET}/release/libcrypteia.so" "./build/${LIB}"
2020

2121
cd ./build
22-
strip "$BIN"
22+
if [ "${CRYPTEIA_BUILD_TARGET}" = "aarch64-unknown-linux-gnu" ]; then
23+
strip "$BIN"
24+
else
25+
x86_64-linux-gnu-strip "$BIN"
26+
fi
2327
chmod +x "$BIN"
2428
zip -r "${BIN}.zip" "$BIN"
2529
zip -r "libcrypteia-${CRYPTEIA_BUILD_OS}${CRYPTEIA_BUILD_SUFFIX}.zip" "$LIB"

bin/test

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,23 @@ set -e
33

44
# Sync with bin/build, bin/test, & test/libcrypteia.sh.
55
export CRYPTEIA_BUILD_OS="${CRYPTEIA_BUILD_OS:=debian}"
6-
export CRYPTEIA_BUILD_TARGET="${CRYPTEIA_BUILD_TARGET:=x86_64-unknown-linux-gnu}"
6+
7+
# Auto-detect build target if not already set
8+
if [ -z "${CRYPTEIA_BUILD_TARGET}" ]; then
9+
case "$(uname -m)" in
10+
aarch64)
11+
export CRYPTEIA_BUILD_TARGET="aarch64-unknown-linux-gnu"
12+
;;
13+
x86_64)
14+
export CRYPTEIA_BUILD_TARGET="x86_64-unknown-linux-gnu"
15+
;;
16+
*)
17+
echo "Unsupported architecture: $(uname -m)"
18+
exit 1
19+
;;
20+
esac
21+
fi
22+
723
if [ "${CRYPTEIA_BUILD_TARGET}" = "aarch64-unknown-linux-gnu" ]; then
824
export CRYPTEIA_BUILD_SUFFIX="-arm64"
925
fi
@@ -12,9 +28,7 @@ if [ ! "${SKIP_CARGO_TEST}" = "1" ]; then
1228
cargo test --target "${CRYPTEIA_BUILD_TARGET}" --quiet
1329
fi
1430

15-
if [ ! "${CRYPTEIA_BUILD_TARGET}" = "aarch64-unknown-linux-gnu" ]; then
16-
TEST_LANG=node ./test/libcrypteia.sh
17-
TEST_LANG=ruby ./test/libcrypteia.sh
18-
TEST_LANG=php ./test/libcrypteia.sh
19-
TEST_LANG=python ./test/libcrypteia.sh
20-
fi
31+
TEST_LANG=node ./test/libcrypteia.sh
32+
TEST_LANG=ruby ./test/libcrypteia.sh
33+
TEST_LANG=php ./test/libcrypteia.sh
34+
TEST_LANG=python ./test/libcrypteia.sh

0 commit comments

Comments
 (0)