Skip to content

Instantly share code, notes, and snippets.

@jcaesar
Created May 24, 2019 04:35
Show Gist options
  • Save jcaesar/408dda9a4ffc994537d7fb7f3300c5ee to your computer and use it in GitHub Desktop.
Save jcaesar/408dda9a4ffc994537d7fb7f3300c5ee to your computer and use it in GitHub Desktop.
Cargo in docker with somewhat working build caching
#!/usr/bin/env bash
set -eu
cd "$(dirname "$0")"
DOCKERF="$(cat <<EOF
FROM debian
RUN apt update && apt -yq install sudo build-essential libclang-dev clang curl # extra hoops for rdkafka
RUN echo 'rust ALL=(ALL:ALL) NOPASSWD:ALL' >>/etc/sudoers
RUN groupadd -g $(id -g) rust && useradd -d /home/rust -u $(id -u) -g rust rust && mkdir -p ~rust/.cargo/git ~rust/.cargo/registry ~rust/src && chown -R rust:rust ~rust
RUN echo '#!/usr/bin/env sh\nsudo chown rust:rust /home/rust/.cargo/git /home/rust/.cargo/registry /home/rust/src && exec "\$@"' >/usr/bin/volpermfix && chmod a+x /usr/bin/volpermfix
USER rust
RUN bash -c "set -o pipefail; curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain stable"
ENV PATH=/home/rust/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
WORKDIR /home/rust/src
EOF
)"
#RUN apt update && apt -yq install curl musl-dev musl-tools make python g++ && apt-get clean && rm -rf /var/lib/apt/lists/*
set +e
BUILD="$(echo "$DOCKERF" | docker build --build-arg http_proxy="$http_proxy" --build-arg https_proxy="$https_proxy" -)"
EXIT=$?
set -e
ID="$(echo "$BUILD" | sed -n 's/Successfully built //p')"
if test $EXIT -lt 0 || test -z ${ID:+z}; then
echo "Build failed."
echo "$BUILD"
exit $EXIT
fi
if test "${1-x}" == "test"; then
set -- test --all-targets
else
set run --example "$1" -- "${@:2}"
fi
docker run --rm -i \
-v "$PWD":/home/rust/src \
-v "$PWD"/.cargo-home/registry:/home/rust/.cargo/registry \
-v "$PWD"/.cargo-home/git:/home/rust/.cargo/git \
-e http_proxy="$http_proxy" -e https_proxy="$https_proxy" \
--network=host \
$ID volpermfix cargo "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment