From e0966fa4510684d0e1d582203d0e8676f0608d33 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Thu, 19 Oct 2023 17:22:20 -0400 Subject: [PATCH] refactor docker --- .github/workflows/dev-docker.yml | 62 ++---------------- .github/workflows/stable-docker.yml | 62 ++---------------- docker-bake.hcl | 89 ++++++++++++++++++++++++++ docker-compose.yml | 9 ++- stacks/flow-api.Dockerfile | 45 ------------- stacks/flow-snapshot-worker.Dockerfile | 37 ----------- stacks/flow-worker.Dockerfile | 37 ----------- stacks/flow.Dockerfile | 49 ++++++++++++++ 8 files changed, 156 insertions(+), 234 deletions(-) create mode 100644 docker-bake.hcl delete mode 100644 stacks/flow-api.Dockerfile delete mode 100644 stacks/flow-snapshot-worker.Dockerfile delete mode 100644 stacks/flow-worker.Dockerfile create mode 100644 stacks/flow.Dockerfile diff --git a/.github/workflows/dev-docker.yml b/.github/workflows/dev-docker.yml index def66e032..51bdf82b8 100644 --- a/.github/workflows/dev-docker.yml +++ b/.github/workflows/dev-docker.yml @@ -34,62 +34,12 @@ jobs: id: vars run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)" - - name: Build (optionally publish) PeerDB Dev Image - uses: depot/build-push-action@v1 + - name: Build (optionally publish) PeerDB Images + uses: depot/bake-action@v1 with: token: ${{ secrets.DEPOT_TOKEN }} - platforms: linux/amd64,linux/arm64 - context: . - file: stacks/peerdb-server.Dockerfile + files: ./docker-bake.hcl push: ${{ github.ref == 'refs/heads/main' }} - tags: | - ghcr.io/peerdb-io/peerdb-server:dev-${{ steps.vars.outputs.sha_short }} - ghcr.io/peerdb-io/peerdb-server:latest-dev - - - name: Build (optionally publish) Flow API Dev Image - uses: depot/build-push-action@v1 - with: - token: ${{ secrets.DEPOT_TOKEN }} - platforms: linux/amd64,linux/arm64 - context: . - file: stacks/flow-api.Dockerfile - push: ${{ github.ref == 'refs/heads/main' }} - tags: | - ghcr.io/peerdb-io/flow-api:dev-${{ steps.vars.outputs.sha_short }} - ghcr.io/peerdb-io/flow-api:latest-dev - - - name: Build (optionally publish) Flow Worker Dev Image - uses: depot/build-push-action@v1 - with: - token: ${{ secrets.DEPOT_TOKEN }} - platforms: linux/amd64,linux/arm64 - context: . - file: stacks/flow-worker.Dockerfile - push: ${{ github.ref == 'refs/heads/main' }} - tags: | - ghcr.io/peerdb-io/flow-worker:dev-${{ steps.vars.outputs.sha_short }} - ghcr.io/peerdb-io/flow-worker:latest-dev - - - name: Build (optionally publish) Flow Snapshot Worker Dev Image - uses: depot/build-push-action@v1 - with: - token: ${{ secrets.DEPOT_TOKEN }} - platforms: linux/amd64,linux/arm64 - context: . - file: stacks/flow-snapshot-worker.Dockerfile - push: ${{ github.ref == 'refs/heads/main' }} - tags: | - ghcr.io/peerdb-io/flow-snapshot-worker:dev-${{ steps.vars.outputs.sha_short }} - ghcr.io/peerdb-io/flow-snapshot-worker:latest-dev - - - name: Build (optionally publish) PeerDB UI Dev Image - uses: depot/build-push-action@v1 - with: - token: ${{ secrets.DEPOT_TOKEN }} - platforms: linux/amd64,linux/arm64 - context: . - file: stacks/peerdb-ui.Dockerfile - push: ${{ github.ref == 'refs/heads/main' }} - tags: | - ghcr.io/peerdb-io/peerdb-ui:dev-${{ steps.vars.outputs.sha_short }} - ghcr.io/peerdb-io/peerdb-ui:latest-dev + env: + SHA_SHORT: ${{ steps.vars.outputs.sha_short }} + TAG: latest-dev diff --git a/.github/workflows/stable-docker.yml b/.github/workflows/stable-docker.yml index 1b0eef263..86e378d0c 100644 --- a/.github/workflows/stable-docker.yml +++ b/.github/workflows/stable-docker.yml @@ -29,62 +29,12 @@ jobs: username: ${{github.actor}} password: ${{secrets.GITHUB_TOKEN}} - - name: Publish PeerDB Stable Image - uses: depot/build-push-action@v1 + - name: Build (optionally publish) PeerDB Images + uses: depot/bake-action@v1 with: - platforms: linux/amd64,linux/arm64 token: ${{ secrets.DEPOT_TOKEN }} - context: . - file: stacks/peerdb-server.Dockerfile - push: true - tags: | - ghcr.io/peerdb-io/peerdb-server:${{ github.ref_name }} - ghcr.io/peerdb-io/peerdb-server:latest-stable - - - name: Publish Flow API Image - uses: depot/build-push-action@v1 - with: - platforms: linux/amd64,linux/arm64 - token: ${{ secrets.DEPOT_TOKEN }} - context: . - file: stacks/flow-api.Dockerfile - push: true - tags: | - ghcr.io/peerdb-io/flow-api:${{ github.ref_name }} - ghcr.io/peerdb-io/flow-api:latest-stable - - - name: Publish Flow Worker Stable Image - uses: depot/build-push-action@v1 - with: - platforms: linux/amd64,linux/arm64 - token: ${{ secrets.DEPOT_TOKEN }} - context: . - file: stacks/flow-worker.Dockerfile - push: true - tags: | - ghcr.io/peerdb-io/flow-worker:${{ github.ref_name }} - ghcr.io/peerdb-io/flow-worker:latest-stable - - - name: Publish Flow Snapshot Worker Stable Image - uses: depot/build-push-action@v1 - with: - platforms: linux/amd64,linux/arm64 - token: ${{ secrets.DEPOT_TOKEN }} - context: . - file: stacks/flow-snapshot-worker.Dockerfile - push: true - tags: | - ghcr.io/peerdb-io/flow-snapshot-worker:${{ github.ref_name }} - ghcr.io/peerdb-io/flow-snapshot-worker:latest-stable - - - name: Build (optionally publish) PeerDB UI Stable Image - uses: depot/build-push-action@v1 - with: - token: ${{ secrets.DEPOT_TOKEN }} - platforms: linux/amd64,linux/arm64 - context: . - file: stacks/peerdb-ui.Dockerfile + files: ./docker-bake.hcl push: ${{ github.ref == 'refs/heads/main' }} - tags: | - ghcr.io/peerdb-io/peerdb-ui:${{ github.ref_name }} - ghcr.io/peerdb-io/peerdb-ui:latest-stable + env: + SHA_SHORT: ${{ github.ref_name }} + TAG: latest-stable diff --git a/docker-bake.hcl b/docker-bake.hcl new file mode 100644 index 000000000..ec3b7cd46 --- /dev/null +++ b/docker-bake.hcl @@ -0,0 +1,89 @@ +variable SHA_SHORT { + default = "123456" +} + +variable TAG { + default = "latest-dev" +} + +variable REGISTRY { + default = "ghcr.io/peerdb-io" +} + +group "default" { + targets = [ + "peerdb", + "flow-worker", + "flow-api", + "flow-snapshot-worker", + "peerdb-ui" + ] +} + +target "flow-api" { + context = "." + dockerfile = "stacks/flow.Dockerfile" + target = "flow-api" + platforms = [ + "linux/amd64", + "linux/arm64", + ] + tags = [ + "${REGISTRY}/flow-api:${TAG}", + "${REGISTRY}/flow-api:${SHA_SHORT}", + ] +} + +target "flow-snapshot-worker" { + context = "." + dockerfile = "stacks/flow.Dockerfile" + target = "flow-snapshot-worker" + platforms = [ + "linux/amd64", + "linux/arm64", + ] + tags = [ + "${REGISTRY}/flow-snapshot-worker:${TAG}", + "${REGISTRY}/flow-snapshot-worker:${SHA_SHORT}", + ] +} + +target "flow-worker" { + context = "." + dockerfile = "stacks/flow.Dockerfile" + target = "flow-worker" + platforms = [ + "linux/amd64", + "linux/arm64", + ] + tags = [ + "${REGISTRY}/flow-worker:${TAG}", + "${REGISTRY}/flow-worker:${SHA_SHORT}", + ] +} + +target "peerdb" { + context = "." + dockerfile = "stacks/peerdb-server.Dockerfile" + platforms = [ + "linux/amd64", + "linux/arm64", + ] + tags = [ + "${REGISTRY}/peerdb-server:${TAG}", + "${REGISTRY}/peerdb-server:${SHA_SHORT}", + ] +} + +target "peerdb-ui" { + context = "." + dockerfile = "stacks/peerdb-ui.Dockerfile" + platforms = [ + "linux/amd64", + "linux/arm64", + ] + tags = [ + "${REGISTRY}/peerdb-ui:${TAG}", + "${REGISTRY}/peerdb-ui:${SHA_SHORT}", + ] +} diff --git a/docker-compose.yml b/docker-compose.yml index 9085191e3..1bad765a5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -106,7 +106,8 @@ services: container_name: flow_api build: context: . - dockerfile: stacks/flow-api.Dockerfile + dockerfile: stacks/flow.Dockerfile + target: flow-api ports: - 8112:8112 - 8113:8113 @@ -121,7 +122,8 @@ services: container_name: flow-snapshot-worker build: context: . - dockerfile: stacks/flow-snapshot-worker.Dockerfile + dockerfile: stacks/flow.Dockerfile + target: flow-snapshot-worker environment: <<: [*peerdb-temporal-namespace] TEMPORAL_HOST_PORT: temporal:7233 @@ -133,7 +135,8 @@ services: container_name: flow-worker1 build: context: . - dockerfile: stacks/flow-worker.Dockerfile + dockerfile: stacks/flow.Dockerfile + target: flow-worker environment: <<: [*catalog-config, *flow-worker-env, *peerdb-temporal-namespace] METRICS_SERVER: 0.0.0.0:6061 diff --git a/stacks/flow-api.Dockerfile b/stacks/flow-api.Dockerfile deleted file mode 100644 index 22af70755..000000000 --- a/stacks/flow-api.Dockerfile +++ /dev/null @@ -1,45 +0,0 @@ -# syntax=docker/dockerfile:1.2 - -# Start from the latest Golang base image -FROM golang:1.21.3-bookworm AS builder - -RUN apt-get update && apt-get install -y gcc - -RUN apt-get update && apt-get install -y libgeos-dev -WORKDIR /root/flow - -# first copy only go.mod and go.sum to cache dependencies -COPY flow/go.mod . -COPY flow/go.sum . - -# download all the dependencies -RUN --mount=type=cache,target=/go/pkg/mod \ - go mod download - -# Copy all the code -COPY flow . - -# build the binary from cmd folder -WORKDIR /root/flow/cmd -RUN --mount=type=cache,target=/root/.cache/go-build \ - CGO_ENABLED=1 go build -ldflags="-s -w" -o /root/peer-flow . - -FROM golang:1.21.3-bookworm -RUN apt-get update && apt-get install -y ca-certificates - -RUN apt-get update && apt-get install -y gcc - -# install lib geos dev -RUN apt-get update && apt-get install -y libgeos-dev -WORKDIR /root -COPY --from=builder /root/peer-flow . -EXPOSE 8112 -EXPOSE 8113 -ENTRYPOINT [\ - "./peer-flow",\ - "api",\ - "--port",\ - "8112",\ - "--gateway-port",\ - "8113"\ - ] diff --git a/stacks/flow-snapshot-worker.Dockerfile b/stacks/flow-snapshot-worker.Dockerfile deleted file mode 100644 index 0383a0374..000000000 --- a/stacks/flow-snapshot-worker.Dockerfile +++ /dev/null @@ -1,37 +0,0 @@ -# syntax=docker/dockerfile:1.2 - -# Start from the latest Golang base image -FROM golang:1.21.3-bookworm AS builder - -RUN apt-get update && apt-get install -y gcc - -RUN apt-get update && apt-get install -y libgeos-dev - - -WORKDIR /root/ - -# first copy only go.mod and go.sum to cache dependencies -COPY flow/go.mod . -COPY flow/go.sum . - -# download all the dependencies -RUN --mount=type=cache,target=/go/pkg/mod \ - go mod download - -COPY flow . - -# build the binary from cmd folder -WORKDIR /root/cmd -RUN --mount=type=cache,target=/root/.cache/go-build \ - CGO_ENABLED=1 go build -ldflags="-s -w" -o /root/peer-flow . - -FROM golang:1.21.3-bookworm -RUN apt-get update && apt-get install -y ca-certificates - -RUN apt-get update && apt-get install -y gcc -# install lib geos dev -RUN apt-get update && apt-get install -y libgeos-dev -WORKDIR /root -COPY --from=builder /root/peer-flow . -EXPOSE 8112 -ENTRYPOINT ["./peer-flow", "snapshot-worker"] diff --git a/stacks/flow-worker.Dockerfile b/stacks/flow-worker.Dockerfile deleted file mode 100644 index 526fb59d4..000000000 --- a/stacks/flow-worker.Dockerfile +++ /dev/null @@ -1,37 +0,0 @@ -# syntax=docker/dockerfile:1.2 - -# Start from the latest Golang base image -FROM golang:1.21.3-bookworm AS builder - -RUN apt-get update && apt-get install -y gcc - -RUN apt-get update && apt-get install -y libgeos-dev - -WORKDIR /root/ - -# first copy only go.mod and go.sum to cache dependencies -COPY flow/go.mod . -COPY flow/go.sum . - -# download all the dependencies -RUN --mount=type=cache,target=/go/pkg/mod \ - go mod download - -COPY flow . - -# build the binary from cmd folder -WORKDIR /root/cmd -RUN --mount=type=cache,target=/root/.cache/go-build \ - CGO_ENABLED=1 go build -ldflags="-s -w"\ - -o /root/peer-flow . - -FROM golang:1.21.3-bookworm -RUN apt-get update && apt-get install -y ca-certificates - -RUN apt-get update && apt-get install -y gcc -# install lib geos dev -RUN apt-get update && apt-get install -y libgeos-dev -WORKDIR /root -COPY --from=builder /root/peer-flow . -EXPOSE 8112 -ENTRYPOINT ["./peer-flow", "worker"] diff --git a/stacks/flow.Dockerfile b/stacks/flow.Dockerfile new file mode 100644 index 000000000..d04b30739 --- /dev/null +++ b/stacks/flow.Dockerfile @@ -0,0 +1,49 @@ +# 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 . +COPY 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 +EXPOSE 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"\ + ]