From 4c4089a4db87d47d49ddc18a5022211e9ccf6ca4 Mon Sep 17 00:00:00 2001 From: crebsy <121096251+crebsy@users.noreply.github.com> Date: Tue, 30 Jul 2024 09:59:33 +0200 Subject: [PATCH] feat: create a minimal docker image and github workflow for building it when pushed to master branch (#60) * feat: create a minimal docker image and github workflow for building it when pushed to master branch * fix: add --platform target * feat: add static build for graphql binary * feat: added Docker section to README and a simple docker-compose.yml that starts all indexing * fix: add link to ghcr package repo * fix: remove postgres volume * fix: only build docker image for merges into master from a release * fix: Dockerfile * fix: build docker image only for merges from release/** branches to master * feat: add docker usage instructions to docs * feat: add docs for creating a new project with docker * feat: build dynamically linked binaries with debian base image --- .dockerignore | 3 + .github/workflows/docker.yml | 56 +++++++++++++++++++ Dockerfile | 26 +++++++++ README.md | 26 +++++++++ docker-compose.yml | 18 ++++++ .../pages/docs/introduction/installation.mdx | 26 ++++++++- graphql/package.json | 3 +- .../events/erc_20_filter_abi_gen.rs | 2 +- .../events/rocket_pool_eth_abi_gen.rs | 2 +- .../events/world_abi_gen.rs | 2 +- 10 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..0b0ed1b5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +Dockerfile +.env diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 00000000..11d665df --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,56 @@ +name: Build & publish docker image + +on: + pull_request: + types: + - closed + branches: + - master + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + if: github.event.pull_request.merged == true && startsWith(github.head_ref, 'release/') + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Login against a Docker registry + # https://github.com/docker/login-action + - name: Log into registry ${{ env.REGISTRY }} + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=sha + type=sha,format=long + # set latest tag for default branch + type=raw,value=latest,enable={{is_default_branch}} + + # Build and push Docker image with Buildx + # https://github.com/docker/build-push-action + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..d0a7af4c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM --platform=linux/amd64 rust:1.79-slim-bookworm as builder +ENV CARGO_NET_GIT_FETCH_WITH_CLI=true +RUN apt update && apt install -y libssl-dev binutils libc-dev libstdc++6 pkg-config + +WORKDIR /app +COPY . . +RUN cargo build --release +RUN strip /app/target/release/rindexer_cli + +FROM --platform=linux/amd64 node:lts-bookworm as node-builder +RUN apt update && apt install -y ca-certificates +WORKDIR /app +COPY . . +RUN cd /app/graphql && npm i && npm run build-linux + +FROM --platform=linux/amd64 debian:bookworm-slim +RUN apt update \ + && apt install -y libssl-dev libc-dev libstdc++6 ca-certificates lsof \ + && apt-get autoremove --yes \ + && apt-get clean --yes \ + && rm -rf /var/lib/apt/lists/* + +COPY --from=node-builder /app/core/resources/rindexer-graphql-linux /app/resources/rindexer-graphql-linux +COPY --from=builder /app/target/release/rindexer_cli /app/rindexer + +ENTRYPOINT ["/app/rindexer"] diff --git a/README.md b/README.md index 99660cd8..c4e2944e 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,32 @@ Options: We have full documentation https://rindexer.xyz/docs/introduction/installation which goes into more detail on how to use rindexer and all the commands available to you. +## Docker + +There's a pre-built docker image which can be used to run `rindexer` inside your dockerized infra: + +[`ghcr.io/joshstevens19/rindexer`](https://github.com/users/joshstevens19/packages/container/package/rindexer) + +### Create new project +To create a new `no-code` project in your current directory, you can run the following: + +`docker run -it -v $PWD:/app/project_path ghcr.io/joshstevens19/rindexer new -p /app/project_path no-code` + +### Use with existing project +To use it with an existing project and a running postgres instance you can simply invoke: + +``` +export PROJECT_PATH=/path/to/your/project +export DATABASE_URL="postgresql://user:pass@postgres/db" + +docker-compose up -d +``` + +This will start all local indexing and if you have enabled the graphql endpoint, it will become exposed under: + +http://localhost:3001 + + ## What can I use rindexer for? - Hackathons: spin up a quick indexer to index events for your dApp with an API without any code needed diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..8327967e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +networks: + default: + name: rindexer_default + +services: + rindexer: + image: ghcr.io/joshstevens19/rindexer + platform: linux/amd64 + command: | + start -p /app/project_path all + environment: + - PROJECT_PATH + - DATABASE_URL + volumes: + - ${PROJECT_PATH}:/app/project_path + ports: + - 3001:3001 + restart: always diff --git a/documentation/docs/pages/docs/introduction/installation.mdx b/documentation/docs/pages/docs/introduction/installation.mdx index 30c2ac13..f5b08bfa 100644 --- a/documentation/docs/pages/docs/introduction/installation.mdx +++ b/documentation/docs/pages/docs/introduction/installation.mdx @@ -65,7 +65,31 @@ rindexerdown rindexer uses docker to spin up postgres databases for you when it runs locally so its recommended to install [docker](https://www.docker.com/products/docker-desktop/) if you don't have it installed already. -**If you are using csv you do not need to install docker, it is only recommended with postgres.** +There's a pre-built docker image which can be used to run `rindexer` inside your dockerized infra: + +[`ghcr.io/joshstevens19/rindexer`](https://github.com/users/joshstevens19/packages/container/package/rindexer) + +### Create new project +To create a new `no-code` project in your current directory, you can run the following: + +`docker run -it -v $PWD:/app/project_path ghcr.io/joshstevens19/rindexer new -p /app/project_path no-code` + +### Use with existing project +To use it with an existing project and a running postgres instance you can simply invoke: + +``` +export PROJECT_PATH=/path/to/your/project +export DATABASE_URL="postgresql://user:pass@postgres/db" + +docker-compose up -d +``` + +This will start all local indexing and if you have enabled the graphql endpoint, it will become exposed under: + +http://localhost:3001 + + +**If you are using csv you do not need to install docker, it is only recommended with postgres or if you're deploying rindexer in cloud environments.** ## Rust - optional diff --git a/graphql/package.json b/graphql/package.json index dae0d35e..e04d83f4 100644 --- a/graphql/package.json +++ b/graphql/package.json @@ -3,7 +3,8 @@ "version": "1.0.0", "main": "index.js", "scripts": { - "build": "pkg index.js --output ../core/resources/rindexer-graphql --targets node16-linux-x64,node16-macos-x64,node16-win-x64" + "build": "pkg index.js --output ../core/resources/rindexer-graphql --targets node16-linux-x64,node16-macos-x64,node16-win-x64", + "build-linux": "pkg --debug index.js --output ../core/resources/rindexer-graphql-linux --targets node18-linux-x64" }, "dependencies": { "@graphile-contrib/pg-simplify-inflector": "^6.1.0", diff --git a/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/erc_20_filter_abi_gen.rs b/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/erc_20_filter_abi_gen.rs index 14331a92..6184f3b0 100644 --- a/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/erc_20_filter_abi_gen.rs +++ b/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/erc_20_filter_abi_gen.rs @@ -12,7 +12,7 @@ pub use rindexer_erc20_filter_gen::*; pub mod rindexer_erc20_filter_gen { const _: () = { ::core::include_bytes!( - "/Users/joshstevens/code/rindexer/rindexer_rust_playground/abis/erc20-abi.json", + "../../../../../abis/erc20-abi.json", ); }; #[allow(deprecated)] diff --git a/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/rocket_pool_eth_abi_gen.rs b/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/rocket_pool_eth_abi_gen.rs index a560fca7..d1d181e2 100644 --- a/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/rocket_pool_eth_abi_gen.rs +++ b/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/rocket_pool_eth_abi_gen.rs @@ -12,7 +12,7 @@ pub use rindexer_rocket_pool_eth_gen::*; pub mod rindexer_rocket_pool_eth_gen { const _: () = { ::core::include_bytes!( - "/Users/joshstevens/code/rindexer/rindexer_rust_playground/abis/erc20-abi.json", + "../../../../../abis/erc20-abi.json", ); }; #[allow(deprecated)] diff --git a/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/world_abi_gen.rs b/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/world_abi_gen.rs index a045208c..b104b7de 100644 --- a/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/world_abi_gen.rs +++ b/rindexer_rust_playground/src/rindexer_lib/typings/rindexer_playground/events/world_abi_gen.rs @@ -12,7 +12,7 @@ pub use rindexer_world_gen::*; pub mod rindexer_world_gen { const _: () = { ::core::include_bytes!( - "/Users/joshstevens/code/rindexer/rindexer_rust_playground/abis/world.abi.json", + "../../../../../abis/world.abi.json", ); }; #[allow(deprecated)]