diff --git a/docker-bake.hcl b/docker-bake.hcl index ec3b7cd469..b10d1547ea 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -22,7 +22,7 @@ group "default" { target "flow-api" { context = "." - dockerfile = "stacks/flow.Dockerfile" + dockerfile = "stacks/Dockerfile" target = "flow-api" platforms = [ "linux/amd64", @@ -36,7 +36,7 @@ target "flow-api" { target "flow-snapshot-worker" { context = "." - dockerfile = "stacks/flow.Dockerfile" + dockerfile = "stacks/Dockerfile" target = "flow-snapshot-worker" platforms = [ "linux/amd64", @@ -50,7 +50,7 @@ target "flow-snapshot-worker" { target "flow-worker" { context = "." - dockerfile = "stacks/flow.Dockerfile" + dockerfile = "stacks/Dockerfile" target = "flow-worker" platforms = [ "linux/amd64", @@ -64,7 +64,8 @@ target "flow-worker" { target "peerdb" { context = "." - dockerfile = "stacks/peerdb-server.Dockerfile" + dockerfile = "stacks/Dockerfile" + target = "peerdb-server" platforms = [ "linux/amd64", "linux/arm64", @@ -77,7 +78,8 @@ target "peerdb" { target "peerdb-ui" { context = "." - dockerfile = "stacks/peerdb-ui.Dockerfile" + dockerfile = "stacks/Dockerfile" + target = "peerdb-ui" platforms = [ "linux/amd64", "linux/arm64", @@ -87,3 +89,17 @@ target "peerdb-ui" { "${REGISTRY}/peerdb-ui:${SHA_SHORT}", ] } + +target "peerdb-unimage" { + context = "." + dockerfile = "stacks/Dockerfile" + target = "peerdb-unimage" + platforms = [ + "linux/amd64", + "linux/arm64", + ] + tags = [ + "${REGISTRY}/peerdb-unimage:${TAG}", + "${REGISTRY}/peerdb-unimage:${SHA_SHORT}", + ] +} diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index 6dbb170740..dcaa534109 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -111,7 +111,7 @@ services: container_name: flow_api build: context: . - dockerfile: stacks/flow.Dockerfile + dockerfile: stacks/Dockerfile target: flow-api ports: - 8112:8112 @@ -126,7 +126,7 @@ services: container_name: flow-snapshot-worker build: context: . - dockerfile: stacks/flow.Dockerfile + dockerfile: stacks/Dockerfile target: flow-snapshot-worker environment: <<: [*flow-worker-env] @@ -138,7 +138,7 @@ services: container_name: flow-worker1 build: context: . - dockerfile: stacks/flow.Dockerfile + dockerfile: stacks/Dockerfile target: flow-worker environment: <<: [*catalog-config, *flow-worker-env] @@ -150,7 +150,7 @@ services: container_name: flow-worker2 build: context: . - dockerfile: stacks/flow.Dockerfile + dockerfile: stacks/Dockerfile target: flow-worker environment: <<: [*catalog-config, *flow-worker-env] @@ -164,7 +164,7 @@ services: container_name: flow-worker3 build: context: . - dockerfile: stacks/flow.Dockerfile + dockerfile: stacks/Dockerfile target: flow-worker environment: <<: [*catalog-config, *flow-worker-env] @@ -179,7 +179,7 @@ services: stop_signal: SIGINT build: context: . - dockerfile: stacks/peerdb-server.Dockerfile + dockerfile: stacks/Dockerfile environment: <<: *catalog-config PEERDB_LOG_DIR: /var/log/peerdb @@ -197,7 +197,7 @@ services: container_name: peerdb-ui build: context: . - dockerfile: stacks/peerdb-ui.Dockerfile + dockerfile: stacks/Dockerfile ports: - 3000:3000 environment: diff --git a/docker-compose.yml b/docker-compose.yml index 440f06545d..e60ba76925 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -94,27 +94,6 @@ services: ports: - 8085:8080 - flow-api: - container_name: flow_api - image: ghcr.io/peerdb-io/flow-api:latest-dev - ports: - - 8112:8112 - - 8113:8113 - environment: - <<: [*catalog-config, *flow-worker-env] - depends_on: - temporal-admin-tools: - condition: service_healthy - - flow-snapshot-worker: - container_name: flow-snapshot-worker - image: ghcr.io/peerdb-io/flow-snapshot-worker:latest-dev - environment: - <<: [*flow-worker-env] - depends_on: - temporal-admin-tools: - condition: service_healthy - flow-worker1: container_name: flow-worker1 image: ghcr.io/peerdb-io/flow-worker:latest-dev @@ -146,33 +125,29 @@ services: temporal-admin-tools: condition: service_healthy - peerdb: - container_name: peerdb-server - stop_signal: SIGINT - image: ghcr.io/peerdb-io/peerdb-server:latest-dev + peerdb-unimage: + container_name: peerdb-unimage + build: + context: . + dockerfile: stacks/Dockerfile + target: peerdb-unimage + ports: + - 8112:8112 + - 8113:8113 + - 3000:3000 + - 9900:9900 environment: - <<: *catalog-config + <<: [*catalog-config, *flow-worker-env] + DATABASE_URL: postgres://postgres:postgres@catalog:5432/postgres + PEERDB_FLOW_SERVER_HTTP: http://localhost:8113 PEERDB_LOG_DIR: /var/log/peerdb PEERDB_PASSWORD: - PEERDB_FLOW_SERVER_ADDRESS: grpc://flow_api:8112 + PEERDB_FLOW_SERVER_ADDRESS: grpc://localhost:8112 RUST_LOG: info RUST_BACKTRACE: 1 - ports: - - 9900:9900 depends_on: - catalog: + temporal-admin-tools: condition: service_healthy - peerdb-ui: - container_name: peerdb-ui - image: ghcr.io/peerdb-io/peerdb-ui:latest-dev - ports: - - 3000:3000 - environment: - <<: *catalog-config - DATABASE_URL: postgres://postgres:postgres@catalog:5432/postgres - PEERDB_FLOW_SERVER_HTTP: http://flow_api:8113 - volumes: pgdata: - prometheusdata: diff --git a/stacks/Dockerfile b/stacks/Dockerfile new file mode 100644 index 0000000000..9d4cdfab10 --- /dev/null +++ b/stacks/Dockerfile @@ -0,0 +1,161 @@ +# syntax=docker/dockerfile:1.2 + +## flow-api, flow-worker, flow-snapshot-worker + +FROM golang:1.21.3-bookworm AS flow-builder +RUN apt-get update && apt-get install -y gcc libgeos-dev +WORKDIR /root/flow + +# first copy only go.mod and go.sum to cache dependencies +COPY flow/go.mod flow/go.sum ./ + +# download all the dependencies +RUN go mod download + +# Copy all the code +COPY flow . + +# build the binary from cmd folder +WORKDIR /root/flow/cmd +ENV CGO_ENABLED=1 +RUN go build -ldflags="-s -w" -o /root/peer-flow . + +FROM debian:bookworm-slim AS flow-base +RUN apt-get update && apt-get install -y ca-certificates gcc libgeos-dev +WORKDIR /root +COPY --from=flow-builder /root/peer-flow . + +FROM flow-base AS flow-api +EXPOSE 8112 8113 +ENTRYPOINT [\ + "./peer-flow",\ + "api",\ + "--port",\ + "8112",\ + "--gateway-port",\ + "8113"\ + ] + +FROM flow-base AS flow-worker +ENTRYPOINT [\ + "./peer-flow",\ + "worker"\ + ] + +FROM flow-base AS flow-snapshot-worker +ENTRYPOINT [\ + "./peer-flow",\ + "snapshot-worker"\ + ] + +## peerdb-server + +FROM lukemathwalker/cargo-chef:latest-rust-1.74-slim-bookworm as peerdb-chef +WORKDIR /root + +FROM peerdb-chef as peerdb-planner +COPY nexus nexus +WORKDIR /root/nexus +RUN cargo chef prepare --recipe-path recipe.json + +FROM peerdb-chef as peerdb-builder +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive \ + apt-get install --assume-yes --no-install-recommends \ + build-essential libssl-dev pkg-config curl unzip +WORKDIR /root/nexus +COPY scripts /root/scripts +RUN /root/scripts/install-protobuf.sh +COPY --from=peerdb-planner /root/nexus/recipe.json . +# Build dependencies - this is the caching Docker layer! +RUN cargo chef cook --release --recipe-path recipe.json +COPY nexus /root/nexus +COPY protos /root/protos +WORKDIR /root/nexus +RUN CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse cargo build --release --bin peerdb-server + +FROM debian:bookworm-slim AS peerdb-server +RUN apt-get update && \ + apt-get install -y ca-certificates postgresql-client curl iputils-ping && \ + mkdir -p /var/log/peerdb +WORKDIR /root +COPY --from=peerdb-builder /root/nexus/target/release/peerdb-server . +CMD ["./peerdb-server"] + +## peerdb-ui + +FROM node:20-bookworm-slim AS ui-base +ENV NPM_CONFIG_UPDATE_NOTIFIER=false +RUN apt-get update && \ + apt-get install -y openssl && \ + mkdir /app && \ + chown -R node:node /app +ENV NEXT_TELEMETRY_DISABLED 1 +USER node +WORKDIR /app + +# Dependencies stage +FROM ui-base AS ui-builder +COPY --chown=node:node ui/package.json ui/package-lock.json ./ +RUN npm ci +COPY --chown=node:node ui/ . + +RUN npx prisma generate && npm run build + +# Builder stage +FROM ui-base AS ui +ENV NODE_ENV production + +COPY --from=ui-builder /app/public ./public + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=ui-builder /app/.next/standalone . +COPY --from=ui-builder /app/.next/static ./.next/static +COPY --chown=node:node stacks/ui/ui-entrypoint.sh /app/entrypoint.sh + +EXPOSE 3000 + +ENV PORT 3000 +# set hostname to localhost +ENV HOSTNAME "0.0.0.0" + +ENTRYPOINT ["/app/entrypoint.sh"] +CMD ["node", "server.js"] + +## unified image, bundles UI, peerdb-server, flow-api and flow-snapshot-worker + +FROM ui-base AS peerdb-unimage +USER root +# install supervisor along with other dependencies +RUN apt-get update && \ + apt-get install -y ca-certificates libgeos-dev postgresql-client curl iputils-ping supervisor && \ + mkdir -p /var/log/peerdb && \ + chown -R node:node /var/log/peerdb +USER node + +# move flow binary over, for flow-api and flow-snapshot-worker +COPY --from=flow-builder /root/peer-flow /app + +# move peerdb-server binary over +COPY --from=peerdb-builder /root/nexus/target/release/peerdb-server /app + +# move UI... files over +COPY --from=ui-builder /app/public ./public +COPY --from=ui-builder /app/.next/standalone . +COPY --from=ui-builder /app/.next/static ./.next/static +COPY --chown=node:node stacks/ui/ui-entrypoint.sh /app/entrypoint.sh + +COPY stacks/supervisord.conf /app + +ENV NODE_ENV production +ENV PORT 3000 +ENV HOSTNAME "0.0.0.0" + +EXPOSE 3000 8112 8113 9900 + +ENTRYPOINT ["/app/entrypoint.sh"] +CMD ["/usr/bin/supervisord", "-c", "/app/supervisord.conf"] + + + diff --git a/stacks/flow.Dockerfile b/stacks/flow.Dockerfile deleted file mode 100644 index ca607170d1..0000000000 --- a/stacks/flow.Dockerfile +++ /dev/null @@ -1,47 +0,0 @@ -# syntax=docker/dockerfile:1.2 - -FROM golang:1.21.3-bookworm AS builder -RUN apt-get update && apt-get install -y gcc libgeos-dev -WORKDIR /root/flow - -# first copy only go.mod and go.sum to cache dependencies -COPY flow/go.mod flow/go.sum . - -# download all the dependencies -RUN go mod download - -# Copy all the code -COPY flow . - -# build the binary from cmd folder -WORKDIR /root/flow/cmd -ENV CGO_ENABLED=1 -RUN go build -ldflags="-s -w" -o /root/peer-flow . - -FROM debian:bookworm-slim AS flow-base -RUN apt-get update && apt-get install -y ca-certificates gcc libgeos-dev -WORKDIR /root -COPY --from=builder /root/peer-flow . - -FROM flow-base AS flow-api -EXPOSE 8112 8113 -ENTRYPOINT [\ - "./peer-flow",\ - "api",\ - "--port",\ - "8112",\ - "--gateway-port",\ - "8113"\ - ] - -FROM flow-base AS flow-worker -ENTRYPOINT [\ - "./peer-flow",\ - "worker"\ - ] - -FROM flow-base AS flow-snapshot-worker -ENTRYPOINT [\ - "./peer-flow",\ - "snapshot-worker"\ - ] diff --git a/stacks/peerdb-server.Dockerfile b/stacks/peerdb-server.Dockerfile deleted file mode 100644 index b63006d1ca..0000000000 --- a/stacks/peerdb-server.Dockerfile +++ /dev/null @@ -1,33 +0,0 @@ -# syntax=docker/dockerfile:1 - -FROM lukemathwalker/cargo-chef:latest-rust-1.74-slim-bookworm as chef -WORKDIR /root - -FROM chef as planner -COPY nexus nexus -WORKDIR /root/nexus -RUN cargo chef prepare --recipe-path recipe.json - -FROM chef as builder -RUN apt-get update \ - && DEBIAN_FRONTEND=noninteractive \ - apt-get install --assume-yes --no-install-recommends \ - build-essential libssl-dev pkg-config curl unzip -WORKDIR /root/nexus -COPY scripts /root/scripts -RUN /root/scripts/install-protobuf.sh -COPY --from=planner /root/nexus/recipe.json . -# Build dependencies - this is the caching Docker layer! -RUN cargo chef cook --release --recipe-path recipe.json -COPY nexus /root/nexus -COPY protos /root/protos -WORKDIR /root/nexus -RUN CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse cargo build --release --bin peerdb-server - -FROM debian:bookworm-slim -RUN apt-get update && \ - apt-get install -y ca-certificates postgresql-client curl iputils-ping && \ - mkdir -p /var/log/peerdb -WORKDIR /root -COPY --from=builder /root/nexus/target/release/peerdb-server . -CMD ["./peerdb-server"] diff --git a/stacks/peerdb-ui.Dockerfile b/stacks/peerdb-ui.Dockerfile deleted file mode 100644 index 8630beb1dd..0000000000 --- a/stacks/peerdb-ui.Dockerfile +++ /dev/null @@ -1,41 +0,0 @@ -# syntax=docker/dockerfile:1.2 - -# Base stage -FROM node:20-bookworm-slim AS base -ENV NPM_CONFIG_UPDATE_NOTIFIER=false -RUN apt-get update && \ - apt-get install -y openssl && \ - mkdir /app && \ - chown -R node:node /app -ENV NEXT_TELEMETRY_DISABLED 1 -USER node -WORKDIR /app - -# Dependencies stage -FROM base AS builder -COPY --chown=node:node ui/package.json ui/package-lock.json . -RUN npm ci -COPY --chown=node:node ui/ . - -RUN npx prisma generate && npm run build - -# Builder stage -FROM base AS runner -ENV NODE_ENV production - -COPY --from=builder /app/public ./public - -# Automatically leverage output traces to reduce image size -# https://nextjs.org/docs/advanced-features/output-file-tracing -COPY --from=builder /app/.next/standalone . -COPY --from=builder /app/.next/static ./.next/static -COPY --chown=node:node stacks/ui/ui-entrypoint.sh /app/entrypoint.sh - -EXPOSE 3000 - -ENV PORT 3000 -# set hostname to localhost -ENV HOSTNAME "0.0.0.0" - -ENTRYPOINT ["/app/entrypoint.sh"] -CMD ["node", "server.js"] diff --git a/stacks/supervisord.conf b/stacks/supervisord.conf new file mode 100644 index 0000000000..eb396eec02 --- /dev/null +++ b/stacks/supervisord.conf @@ -0,0 +1,34 @@ +[supervisord] +user=node +nodaemon=true +logfile=/dev/null +logfile_maxbytes=0 + +[program:flow-api] +user=node +command=/app/peer-flow api --port 8112 --gateway-port 8113 +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true + +[program:flow-snapshot-worker] +user=node +command=/app/peer-flow snapshot-worker +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true + +[program:peerdb-server] +user=node +command=/app/peerdb-server +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true +stopsignal=INT + +[program:peerdb-ui] +user=node +command=node server.js +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true \ No newline at end of file