web3-proxy/Dockerfile

229 lines
7.8 KiB
Docker
Raw Permalink Normal View History

2023-11-14 14:48:37 -08:00
FROM debian:bullseye-slim as mold
ENV SHELL /bin/bash
SHELL [ "/bin/bash", "-c" ]
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
set -eux -o pipefail; \
\
apt-get update; \
apt-get install --no-install-recommends --yes \
ca-certificates \
git \
;
RUN { set -eux; \
\
git clone https://github.com/rui314/mold.git; \
mkdir mold/build; \
cd mold/build; \
git checkout v2.3.3; \
../install-build-deps.sh; \
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=c++ ..; \
cmake --build . -j $(nproc); \
cmake --build . --target install; \
}
COPY ./docker/cargo-config.toml /root/.cargo/config.toml
FROM debian:bullseye-slim as rust
2022-04-23 18:00:03 +00:00
2023-01-30 13:53:58 -08:00
WORKDIR /app
2023-07-28 12:58:23 -07:00
# sccache cannot cache incrementals, but we use --mount=type=cache and import caches so it should be helpful
ENV CARGO_INCREMENTAL true
ENV CARGO_TERM_COLOR always
2023-08-18 16:39:35 -07:00
ENV CARGO_UNSTABLE_SPARSE_REGISTRY true
2023-06-25 21:26:17 -07:00
ENV PATH "/root/.foundry/bin:/root/.cargo/bin:${PATH}"
2023-08-18 16:39:35 -07:00
ENV SHELL /bin/bash
SHELL [ "/bin/bash", "-c" ]
2023-01-30 13:53:58 -08:00
# install rustup dependencies
2023-11-14 14:48:37 -08:00
# install clang for mold
2023-06-25 16:26:54 -07:00
# also install web3-proxy system dependencies. most things are rust-only, but not everything
2023-06-25 21:26:17 -07:00
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
2023-07-28 10:43:02 -07:00
set -eux -o pipefail; \
2023-06-25 21:26:17 -07:00
\
apt-get update; \
2023-06-25 21:26:17 -07:00
apt-get install --no-install-recommends --yes \
build-essential \
2023-06-25 21:26:17 -07:00
ca-certificates \
2023-11-14 14:48:37 -08:00
clang \
2023-06-25 16:26:54 -07:00
cmake \
curl \
git \
2023-06-25 16:26:54 -07:00
liblz4-dev \
libpthread-stubs0-dev \
libsasl2-dev \
libzstd-dev \
make \
pkg-config \
;
2023-06-25 21:26:17 -07:00
# install rustup
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
set -eux -o pipefail; \
2023-06-26 23:21:05 -07:00
\
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain none --profile=minimal
# run a cargo command to install our desired version of rust
# it is expected to exit code 101 since no Cargo.toml exists
# the rm is there because `cargo clean` can't run without a Cargo.toml, but a new version of rust likely needs a clean target dir
2023-06-25 21:26:17 -07:00
COPY rust-toolchain.toml ./
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
--mount=type=cache,target=/app/target \
--mount=type=cache,target=/app/target_test \
set -eux -o pipefail; \
\
cargo check || [ "$?" -eq 101 ]; \
2023-09-20 13:55:54 -07:00
[ -e /app/target/rust-toolchain.toml ] && [ "$(cat /app/target/rust-toolchain.toml)" != "$(cat ./rust-toolchain.toml)" ] && rm -rf /app/target/*; \
[ -e /app/target_test/rust-toolchain.toml ] && [ "$(cat /app/target_test/rust-toolchain.toml)" != "$(cat ./rust-toolchain.toml)" ] && rm -rf /app/target_test/*; \
cp ./rust-toolchain.toml /app/target/rust-toolchain.toml; \
cp ./rust-toolchain.toml /app/target_test/rust-toolchain.toml
2023-11-14 14:48:37 -08:00
# install mold (a faster linker)
COPY --link --from=mold /usr/local/bin/mold /usr/local/bin/mold
COPY --link --from=mold /root/.cargo/config.toml /root/.cargo/config.toml
2023-10-12 16:11:38 -07:00
# cargo binstall makes it fast to install binaries
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
set -eux -o pipefail; \
\
curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh >/tmp/install-binstall.sh; \
bash /tmp/install-binstall.sh; \
rm -rf /tmp/*
2023-10-12 16:11:38 -07:00
# flamegraph/tokio-console are used for debugging
2023-10-13 00:26:29 -07:00
FROM rust as rust_flamegraph
2023-10-12 16:11:38 -07:00
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
set -eux -o pipefail; \
\
2023-10-13 00:26:29 -07:00
cargo binstall -y flamegraph
# FROM rust as rust_tokio_console
# RUN --mount=type=cache,target=/root/.cargo/git \
# --mount=type=cache,target=/root/.cargo/registry \
# set -eux -o pipefail; \
# \
# cargo binstall -y tokio-console
2023-10-12 16:11:38 -07:00
# nextest runs tests in parallel (done its in own FROM so that it can run in parallel)
# TODO: i'd like to use binaries for these, but i had trouble with arm and binstall
FROM rust as rust_nextest
2023-07-28 10:43:02 -07:00
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
set -eux -o pipefail; \
\
cargo binstall -y cargo-nextest
2023-01-30 13:53:58 -08:00
# foundry/anvil are needed to run tests (done its in own FROM so that it can run in parallel)
FROM rust as rust_foundry
2023-07-28 10:43:02 -07:00
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
set -eux -o pipefail; \
2023-06-25 21:26:17 -07:00
\
2023-07-28 12:58:23 -07:00
curl -L https://foundry.paradigm.xyz | bash && foundryup
2023-01-30 13:53:58 -08:00
FROM rust as rust_with_env
# changing our features doesn't change any of the steps above
2023-10-16 15:01:22 -07:00
# TODO: i think this should be an ARG
ENV WEB3_PROXY_FEATURES "stripe"
2023-09-12 15:07:30 -07:00
# copy the app
COPY . .
# fill the package caches
2023-09-20 13:35:33 -07:00
# TODO: clean needed because of rust upgrade and jenkins caches :'(
2023-09-12 15:07:30 -07:00
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
set -eux -o pipefail; \
\
[ -e "$(pwd)/payment-contracts/src/contracts/mod.rs" ] || touch "$(pwd)/payment-contracts/build.rs"; \
2023-10-07 00:54:15 -07:00
cargo --locked fetch
# build tests (done its in own FROM so that it can run in parallel)
FROM rust_with_env as build_tests
2023-07-05 12:25:33 -07:00
2023-10-12 16:11:38 -07:00
COPY --link --from=rust_foundry /root/.foundry/bin/anvil /root/.foundry/bin/
COPY --link --from=rust_nextest /root/.cargo/bin/cargo-nextest* /root/.cargo/bin/
# test the application with cargo-nextest
2023-09-12 15:07:30 -07:00
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
2023-10-06 22:54:18 -07:00
--mount=type=cache,target=/app/target_test \
set -eux -o pipefail; \
2023-06-25 21:26:17 -07:00
\
2023-10-07 00:52:34 -07:00
export CARGO_TARGET_DIR=target_test; \
2023-07-10 18:09:58 -07:00
[ -e "$(pwd)/payment-contracts/src/contracts/mod.rs" ] || touch "$(pwd)/payment-contracts/build.rs"; \
2023-07-05 12:52:10 -07:00
RUST_LOG=web3_proxy=trace,info \
cargo \
2023-08-18 17:14:15 -07:00
--frozen \
--offline \
2023-07-05 12:52:10 -07:00
nextest run \
--features "$WEB3_PROXY_FEATURES" --no-default-features \
; \
2023-06-25 21:26:17 -07:00
touch /test_success
2022-04-23 18:00:03 +00:00
FROM rust_with_env as build_app
2023-07-28 13:06:40 -07:00
# build the release application
# using a "release" profile (which install does by default) is **very** important
# TODO: use the "faster_release" profile which builds with `codegen-units = 1` (but compile is SLOW)
2023-09-12 15:07:30 -07:00
RUN --mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
--mount=type=cache,target=/app/target \
set -eux -o pipefail; \
2023-06-25 21:26:17 -07:00
\
2023-07-10 18:09:58 -07:00
[ -e "$(pwd)/payment-contracts/src/contracts/mod.rs" ] || touch "$(pwd)/payment-contracts/build.rs"; \
cargo install \
--features "$WEB3_PROXY_FEATURES" \
2023-08-18 17:14:15 -07:00
--frozen \
--offline \
--no-default-features \
2023-08-03 00:16:54 -07:00
--path ./web3_proxy_cli \
2023-07-05 11:40:43 -07:00
--root /usr/local \
; \
2023-06-27 12:57:16 -07:00
/usr/local/bin/web3_proxy_cli --help | grep 'Usage: web3_proxy_cli'
2022-05-06 01:25:49 +00:00
2023-07-05 12:57:20 -07:00
# copy this file so that docker actually creates the build_tests container
# without this, the runtime container doesn't need build_tests and so docker build skips it
2023-10-12 16:11:38 -07:00
COPY --link --from=build_tests /test_success /
2023-06-27 09:45:16 -07:00
2023-01-30 13:53:58 -08:00
#
2023-07-05 12:57:20 -07:00
# We do not need the Rust toolchain or any deps to run the binary!
2023-01-30 13:53:58 -08:00
#
FROM debian:bullseye-slim AS runtime
2023-01-17 21:26:10 -08:00
2023-02-06 13:46:45 -08:00
# Create llama user to avoid running container with root
RUN set -eux; \
\
mkdir /llama; \
adduser --home /llama --shell /sbin/nologin --gecos '' --no-create-home --disabled-password --uid 1001 llama; \
2023-07-05 11:40:43 -07:00
chown -R llama /llama
2023-02-06 13:46:45 -08:00
USER llama
ENTRYPOINT ["web3_proxy_cli"]
2023-01-19 02:32:24 -08:00
CMD [ "--config", "/web3-proxy.toml", "proxyd" ]
2022-05-12 19:58:26 +00:00
2023-10-12 19:23:22 -07:00
ENV PATH "/root/.cargo/bin:${PATH}"
2022-05-12 19:58:26 +00:00
# TODO: lower log level when done with prototyping
ENV RUST_LOG "warn,ethers_providers::rpc=off,web3_proxy=debug,web3_proxy::rpcs::consensus=info,web3_proxy_cli=debug"
2023-01-30 13:53:58 -08:00
2023-06-25 21:26:17 -07:00
# we copy something from build_tests just so that docker actually builds it
2023-10-13 00:29:01 -07:00
COPY --link --from=rust_flamegraph /root/.cargo/bin/* /root/.cargo/bin/
2023-10-12 16:11:38 -07:00
COPY --link --from=build_app /usr/local/bin/* /usr/local/bin/
# make sure the app works
2023-05-30 17:38:45 -07:00
RUN web3_proxy_cli --help