diff --git a/docker/Dockerfile b/docker/Dockerfile index 03fe249e2..58e0c33cd 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,53 +1,34 @@ -FROM python:3.7.6-slim-buster - -ARG RELEASE_BUILD=false +FROM python:3.7.6-slim-buster AS builder -# Install rust nightly +# Install Rust nightly ENV RUSTUP_HOME=/usr/local/rustup \ CARGO_HOME=/usr/local/cargo \ PATH=/usr/local/cargo/bin:$PATH - -RUN set -eux; \ - apt-get update; \ - apt-get install -y --no-install-recommends \ - ca-certificates \ - gcc \ - libc6-dev \ - wget \ - libssl-dev \ - pkg-config \ - ; \ - \ - url="https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init"; \ - wget "$url"; \ - chmod +x rustup-init; \ - ./rustup-init -y --no-modify-path --default-toolchain nightly; \ - rm rustup-init; \ - chmod -R a+w $RUSTUP_HOME $CARGO_HOME; \ - rustup --version; \ - cargo --version; \ - rustc --version; \ - \ - apt-get remove -y --auto-remove \ - wget \ - ; \ - rm -rf /var/lib/apt/lists/*; +COPY docker/install_rust.sh . +RUN bash ./install_rust.sh # Install dev tools -RUN apt-get update; \ - apt-get install -y valgrind \ - ; +RUN apt-get update && apt-get install -y valgrind -WORKDIR /usr/src/coordinator -# https://benjamincongdon.me/blog/2019/12/04/Fast-Rust-Docker-Builds-with-cargo-vendor/ +# First, fetch and build all the dependencies by compiling an empty +# crate that depends on everything COPY rust/Cargo.lock . COPY rust/Cargo.toml . -RUN mkdir .cargo -RUN cargo vendor > .cargo/config +RUN cargo fetch +RUN mkdir -p ./src && \ + touch src/lib.rs && \ + cargo build --lib --all-features +# Now copy the actual source code COPY rust/src src -COPY ./docker/compile.sh . -RUN bash compile.sh +# Fix timestamp. cargo incremental build as issues caused by the above +# trick to compile the deps separately +RUN touch src/lib.rs +RUN cargo build --all-features + +FROM python:3.7.6-slim-buster +COPY --from=builder /target/debug/aggregator /bin/aggregator +COPY --from=builder /target/debug/coordinator /bin/coordinator COPY python/aggregators aggregators/ RUN pip install aggregators/ diff --git a/docker/Dockerfile.release b/docker/Dockerfile.release new file mode 100644 index 000000000..02401a97c --- /dev/null +++ b/docker/Dockerfile.release @@ -0,0 +1,21 @@ +FROM python:3.7.6-slim-buster AS builder + +# Install Rust nightly +ENV RUSTUP_HOME=/usr/local/rustup \ + CARGO_HOME=/usr/local/cargo \ + PATH=/usr/local/cargo/bin:$PATH +COPY docker/install_rust.sh . +RUN bash ./install_rust.sh + +COPY rust/Cargo.lock . +COPY rust/Cargo.toml . +COPY rust/src src +RUN cargo build --release --features=telemetry,influx_metrics + + +FROM python:3.7.6-slim-buster + +COPY --from=builder /target/release/aggregator /bin/aggregator +COPY --from=builder /target/release/coordinator /bin/coordinator +COPY python/aggregators aggregators/ +RUN pip install aggregators/ diff --git a/docker/compile.sh b/docker/compile.sh deleted file mode 100644 index e2346b403..000000000 --- a/docker/compile.sh +++ /dev/null @@ -1,11 +0,0 @@ -set -x - -if [ "$RELEASE_BUILD" == "true" ] ; then - cargo build --release --features=telemetry,influx_metrics - mv ./target/release/aggregator /bin/aggregator - mv ./target/release/coordinator /bin/coordinator -else - cargo build --features=telemetry,influx_metrics - mv ./target/debug/aggregator /bin/aggregator - mv ./target/debug/coordinator /bin/coordinator -fi diff --git a/docker/docker-compose-release.yml b/docker/docker-compose-release.yml index cbffab67f..e562712e6 100644 --- a/docker/docker-compose-release.yml +++ b/docker/docker-compose-release.yml @@ -5,9 +5,7 @@ services: command: coordinator -c /bin/config.toml build: context: .. - dockerfile: docker/Dockerfile - args: - RELEASE_BUILD: "true" + dockerfile: docker/Dockerfile.release image: docker_coordinator:release depends_on: - influxdb @@ -23,7 +21,7 @@ services: command: aggregator -c /bin/config.toml build: context: .. - dockerfile: docker/Dockerfile + dockerfile: docker/Dockerfile.release args: RELEASE_BUILD: "true" image: docker_aggregator:release diff --git a/docker/install_rust.sh b/docker/install_rust.sh new file mode 100644 index 000000000..b360664d5 --- /dev/null +++ b/docker/install_rust.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +set -eux + +apt-get update +apt-get install -y --no-install-recommends \ + ca-certificates \ + gcc \ + libc6-dev \ + wget \ + libssl-dev \ + pkg-config \ + +wget "https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init" + +chmod +x rustup-init + +./rustup-init -y --no-modify-path --default-toolchain nightly +chmod -R a+w $RUSTUP_HOME $CARGO_HOME + +rustup --version +cargo --version +rustc --version