From 2b30c6181f58f4b70aeae9948947c2a3bcd754b2 Mon Sep 17 00:00:00 2001 From: guochao Date: Mon, 20 Nov 2023 21:15:49 +0800 Subject: [PATCH] improve docker build procedure --- .dockerignore | 4 ++++ Docker.static-builder | 3 --- README.md | 17 ++++++----------- build/Dockerfile.static-build | 20 ++++++++++++++++++++ build/Dockerfile.ubuntu-22.04 | 20 ++++++++++++++++++++ 5 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 .dockerignore delete mode 100644 Docker.static-builder create mode 100644 build/Dockerfile.static-build create mode 100644 build/Dockerfile.ubuntu-22.04 diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..57801e2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.direnv +/target +/result +/build diff --git a/Docker.static-builder b/Docker.static-builder deleted file mode 100644 index 7f31300..0000000 --- a/Docker.static-builder +++ /dev/null @@ -1,3 +0,0 @@ -FROM rust:alpine -RUN apk add --no-cache pkgconf libseccomp-static libseccomp-dev musl-dev -ENTRYPOINT ["/usr/local/cargo/bin/cargo", "build"] \ No newline at end of file diff --git a/README.md b/README.md index 0341519..2d331f9 100644 --- a/README.md +++ b/README.md @@ -28,18 +28,13 @@ cargo build ### Build static with container ```bash -docker build -t x2t-sandbox-builder:1 -f Docker.static-builder . -docker run -it --rm \ - -v $PWD:/src --workdir /src \ - -e RUSTFLAGS='-C target-feature=+crt-static' \ - x2t-sandbox-builder:1 \ - --features tracing-mode \ - --target x86_64-unknown-linux-musl \ - --release \ - -./target/x86_64-unknown-linux-musl/release/x2t-sandbox --help -``` +for link_type in static-build ubuntu-22.04; do + docker build -t x2t-sandbox-builder:copy-to-data -f build/Dockerfile.$link_type --target copy-to-data . + docker run -it --rm -v output/$link_type:/data x2t-sandbox-builder:copy-to-data +done +/output/path/x2t-sandbox --help +``` ### Generate syscalls with strace diff --git a/build/Dockerfile.static-build b/build/Dockerfile.static-build new file mode 100644 index 0000000..bf60d3f --- /dev/null +++ b/build/Dockerfile.static-build @@ -0,0 +1,20 @@ +ARG REGISTRY=docker.io +ARG BUILDER_BASE=library/rust:alpine +ARG RUNTIME_BASE=library/alpine:latest + +FROM ${REGISTRY}/${BUILDER_BASE} as builder +RUN apk add --no-cache pkgconf libseccomp-static libseccomp-dev musl-dev +COPY . /src +WORKDIR /src +RUN cargo build --release + +FROM ${REGISTRY}/${RUNTIME_BASE} as base + +FROM base as runtime +COPY --from=builder /src/target/release/x2t-sandbox /usr/local/bin/x2t-sandbox + + +FROM ${REGISTRY}/${RUNTIME_BASE} as copy-to-data +FROM base as copy-to-data +COPY --from=builder /src/target/release/x2t-sandbox /x2t-sandbox +CMD ["cp", "-v", "/x2t-sandbox", "/data/x2t-sandbox"] diff --git a/build/Dockerfile.ubuntu-22.04 b/build/Dockerfile.ubuntu-22.04 new file mode 100644 index 0000000..276f2d5 --- /dev/null +++ b/build/Dockerfile.ubuntu-22.04 @@ -0,0 +1,20 @@ +ARG REGISTRY=docker.io +ARG BASE=library/ubuntu:22.04 + +FROM ${REGISTRY}/${BASE} as base + +FROM base as builder +RUN apt update && apt install build-essential libseccomp-dev -y +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh +COPY . /src +WORKDIR /src +RUN cargo build --release + +FROM base as runtime +RUN apt update && apt install libseccomp -y && rm -rf /var/apt +COPY --from=builder /src/target/release/x2t-sandbox /usr/local/bin/x2t-sandbox + +FROM base as copy-to-data +COPY --from=builder /src/target/release/x2t-sandbox /x2t-sandbox +CMD ["cp", "-v", "/x2t-sandbox", "/data/x2t-sandbox"] +