Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alpine build #236

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
- name: Setup buildx
run: docker buildx create --use
- name: build binaries
run: docker buildx bake --set *.cache-from=type=gha,scope=buildkit-release-${CRATE} --set *.cache-to=type=gha,scope=buildkit-release-${CRATE} release-tars
run: docker buildx bake --set *.cache-from=type=gha,scope=buildkit-release-${CRATE} --set *.cache-to=type=gha,scope=buildkit-release-${CRATE} tar-cross
env:
CRATE: ${{ needs.generate.outputs.crate }}
- name: upload binary as GitHub artifact
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 49 additions & 46 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,71 +1,74 @@
# syntax=docker/dockerfile:1

# Make sure to keep in sync with the version in rust-toolchain.toml
ARG RUST_VERSION=1.71

# Make sure to keep RUST_VERSION in sync with the version in rust-toolchain.toml
ARG BASE_IMAGE="bullseye"
ARG RUST_VERSION=1.71.1
jprendes marked this conversation as resolved.
Show resolved Hide resolved
ARG XX_VERSION=1.2.1
ARG CRATE="containerd-shim-wasmtime,containerd-shim-wasmedge"

FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx

FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION} AS base
FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-${BASE_IMAGE} AS base
COPY --from=xx / /
RUN apt-get update -y && apt-get install --no-install-recommends -y clang pkg-config dpkg-dev jq

COPY ./scripts/dockerfile-utils.sh /usr/bin/dockerfile-utils

# Install host dependencies
RUN dockerfile-utils install_host

# See https://github.com/tonistiigi/xx/issues/108
RUN sed -i -E 's/xx-clang --setup-target-triple/XX_VENDOR=\$vendor xx-clang --setup-target-triple/' $(which xx-cargo) && \
sed -i -E 's/\$\(xx-info\)-/\$\(XX_VENDOR=\$vendor xx-info\)-/g' $(which xx-cargo)

FROM base AS build
SHELL ["/bin/bash", "-c"]
ARG BUILD_TAGS TARGETPLATFORM
RUN xx-apt-get install -y gcc g++ libc++6-dev zlib1g
RUN xx-apt-get install -y libsystemd-dev libdbus-1-dev libseccomp-dev
WORKDIR /src

RUN --mount=type=bind,target=/src,rw,source=. \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/cache \
--mount=type=cache,target=/usr/local/cargo/registry/index \
CARGO_NET_GIT_FETCH_WITH_CLI="true" \
cargo fetch

WORKDIR /build/src
COPY --link crates ./crates
COPY --link benches ./benches
COPY --link Cargo.toml ./
COPY --link Cargo.lock ./
ARG CRATE=""
ARG TARGETOS TARGETARCH TARGETVARIANT
ARG TARGETPLATFORM

RUN --mount=type=cache,target=/usr/local/cargo/git/db \
RUN dockerfile-utils install_target

ARG CRATE

RUN --mount=type=bind,target=/src,rw,source=. \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/cache \
--mount=type=cache,target=/usr/local/cargo/registry/index \
--mount=type=cache,target=/build/src/target,id=runwasi-cargo-build-cache-${TARGETOS}-${TARGETARCH}${TARGETVARIANT} <<EOT
set -e
export "CARGO_NET_GIT_FETCH_WITH_CLI=true"
if [ -n "${CRATE}" ]; then
package="--package=${CRATE}"
fi
xx-cargo build --release ${package}
--mount=type=cache,target=/build,id=runwasi-cargo-build-cache-${CRATE}-${BASE_IMAGE}-${TARGETPLATFORM} <<EOT
set -ex
. dockerfile-utils setup_build
for crate in $(echo $CRATE | tr ',' ' '); do
package="$package --package=${crate}"
done
xx-cargo build --release ${package} ${CARGO_FLAGS} --target-dir /build
EOT
COPY scripts ./scripts
RUN --mount=type=cache,target=/usr/local/cargo/git/db \

FROM build AS package
RUN --mount=type=bind,target=/src,rw,source=. \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/cache \
--mount=type=cache,target=/usr/local/cargo/registry/index \
--mount=type=cache,target=/build/src/target,id=runwasi-cargo-build-cache-${TARGETOS}-${TARGETARCH}${TARGETVARIANT} <<EOT
set -e
mkdir /build/bin
bins="$(scripts/bins.sh ${CRATE} | jq -r 'join(" ")')"
echo "Copying binaries: ${bins}"
for bin in ${bins}; do
cp target/$(xx-cargo --print-target-triple)/release/${bin} /build/bin/${bin}
--mount=type=cache,target=/build,id=runwasi-cargo-build-cache-${CRATE}-${BASE_IMAGE}-${TARGETPLATFORM} <<EOT
set -ex
mkdir -p /release/tar /release/bin
export BUILD_DIR="/build/$(xx-cargo --print-target-triple)/release/"
export VARIANT="$(xx-info os)-$(xx-info arch)$(xx-info variant)-$(xx-info libc)"
for crate in $(echo $CRATE | tr ',' ' '); do
BINS="$(scripts/bins.sh ${crate} | jq -r 'join(" ")')"
if [ -n "${BINS}" ]; then
tar -C "${BUILD_DIR}" -czf "/release/tar/${crate}-${VARIANT}.tar.gz" ${BINS}
tar -C /release/bin -xzf "/release/tar/${crate}-${VARIANT}.tar.gz"
fi
done
EOT

FROM build AS build-tar
WORKDIR /build/release
ARG CRATE
ARG TARGETOS TARGETARCH TARGETVARIANT
RUN <<EOF
if [ -n "$(find /build/bin -type f -exec echo {} \;)" ]; then
tar -C /build/bin -czf "/build/release/${CRATE}-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}.tar.gz" .
fi
EOF

FROM scratch AS release-tar
COPY --link --from=build-tar /build/release/* /
COPY --link --from=package /release/tar/* /

FROM scratch AS release
COPY --link --from=build /build/bin/* /
COPY --link --from=package /release/bin/* /
2 changes: 2 additions & 0 deletions crates/containerd-shim-wasmedge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde_json = { workspace = true }
nix = { workspace = true }
libc = { workspace = true }
libcontainer = { workspace = true }
dbus = { version = "*", optional = true }
jprendes marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
tempfile = "3.7"
Expand All @@ -31,6 +32,7 @@ default = ["standalone", "static"]
standalone = ["wasmedge-sdk/standalone"]
static = ["wasmedge-sdk/static"]
wasi_nn = ["wasmedge-sdk/wasi_nn"]
vendored_dbus = ["dbus/vendored"]

[[bin]]
name = "containerd-shim-wasmedge-v1"
Expand Down
4 changes: 4 additions & 0 deletions crates/containerd-shim-wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ thiserror = { workspace = true }
serde_json = { workspace = true }
nix = { workspace = true }
libcontainer = { workspace = true }
dbus = { version = "*", optional = true }
serde = { workspace = true }
libc = { workspace = true }

Expand All @@ -43,6 +44,9 @@ libc = { workspace = true }
pretty_assertions = "1"
env_logger = "0.10"

[features]
vendored_dbus = ["dbus/vendored"]

[[bin]]
name = "containerd-shim-wasmtime-v1"
path = "src/bin/containerd-shim-wasmtime-v1/main.rs"
Expand Down
48 changes: 36 additions & 12 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "CRATE" {
default = ""
default = null
}

# special target: https://github.com/docker/metadata-action#bake-definition
Expand All @@ -19,10 +19,15 @@ target "image" {

target "image-cross" {
inherits = ["image"]
platforms = [
"linux/amd64",
"linux/arm64"
]
platforms = ["linux/${arch}"]
name = "image-cross-${image}-${arch}"
matrix = {
image = ["bullseye", "alpine"]
arch = ["amd64", "arm64"]
}
args = {
"BASE_IMAGE" = "${image}"
}
}

target "bins" {
Expand All @@ -32,14 +37,33 @@ target "bins" {

target "bins-cross" {
inherits = ["bins"]
platforms = [
"linux/amd64",
"linux/arm64"
]
output= ["type=local,dest=bin/${image}-${arch}"]
platforms = ["linux/${arch}"]
name = "bins-cross-${image}-${arch}"
matrix = {
image = ["bullseye", "alpine"]
arch = ["amd64", "arm64"]
}
args = {
"BASE_IMAGE" = "${image}"
}
}

target "release-tars" {
inherits = ["bins-cross"]
target "tar" {
inherits = ["bins"]
output = ["type=local,dest=release/"]
target = "release-tar"
}
}

target "tar-cross" {
inherits = ["tar"]
platforms = ["linux/${arch}"]
name = "tar-cross-${image}-${arch}"
matrix = {
image = ["bullseye", "alpine"]
arch = ["amd64", "arm64"]
}
args = {
"BASE_IMAGE" = "${image}"
}
}
3 changes: 2 additions & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[toolchain]
# Make sure to this in sync with RUST_VERSION in the Dockerfile
channel="1.71.1"
jprendes marked this conversation as resolved.
Show resolved Hide resolved
profile="default"
channel="1.71.0"
61 changes: 61 additions & 0 deletions scripts/dockerfile-utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Runs a recipe for a step in the Dockerfile.
# Usage:
# dockerfile-utils <step>
#
# where step is one of `install_host`, `install_target` or `build_setup`.
# * install_host: installs build tools and other host environment dependencies (things that run in the host).
# * install_target: installs build dependencies like libraries (things that will run in the target).
# * build_setup: setup to customize the build process (mainly setting env-vars)
#
# This script choosses the correct recipe based on the linux distribution of the base image.
# Supported distributions are `debian` and `alpine`.

dockerfile_utils_alpine() {
# recipes for alpine

install_host() {
apk add g++ bash clang pkgconf git protoc jq
apk add --repository=https://dl-cdn.alpinelinux.org/alpine/edge/main rust-bindgen
}

install_target() {
xx-apk add \
gcc g++ musl-dev zlib-dev zlib-static \
ncurses-dev ncurses-static libffi-dev \
libseccomp-dev libseccomp-static
}

setup_build() {
export WASMEDGE_DEP_STDCXX_LINK_TYPE="static"
export WASMEDGE_DEP_STDCXX_LIB_PATH="$(xx-info sysroot)usr/lib"
export WASMEDGE_RUST_BINDGEN_PATH="$(which bindgen)"
export LIBSECCOMP_LINK_TYPE="static"
export LIBSECCOMP_LIB_PATH="$(xx-info sysroot)usr/lib"
export RUSTFLAGS="-Cstrip=symbols -Clink-arg=-lgcc"
export CARGO_FLAGS="--features=vendored_dbus"
}

}

dockerfile_utils_debian() {
# recipes for debian

install_host() {
apt-get update -y
apt-get install --no-install-recommends -y clang pkg-config dpkg-dev git jq
}

install_target() {
xx-apt-get install -y \
gcc g++ libc++6-dev zlib1g \
libsystemd-dev libdbus-1-dev libseccomp-dev
}

setup_build() {
export RUSTFLAGS="-Cstrip=symbols"
}

}

dockerfile_utils_$(xx-info vendor)
$1