Skip to content

Commit 9758177

Browse files
0xflashboyBrandon Graham
andauthored
chore(backend): dockerize backend rust app (#58)
* Dockerize backend * This PR introduces an automated workflow to build and publish the backend container to GHCR upon merging to `main`. (#59) * **Optimized Dockerfile**: Added `backend/Dockerfile_build_and_publish` using a multi-stage build. * **GitHub Action**: Added `.github/workflows/publish-backend.yml` to automatically build and push the container. * **Versioning**: Images are tagged with `latest` and `sha-<short_commit_hash>`. * **Size & Compatibility**: We use `rust:1.91-slim` (based on Debian Bookworm) for building and `debian:bookworm-slim` for the final runtime. This pairing ensures binary compatibility while stripping out the massive build toolchain (~1GB+) to leave a minimal, production-ready image. * **Separation of Concerns**: Kept the original `Dockerfile` for local development/debug builds, while the new `Dockerfile_build_and_publish` is strictly for deployment artifacts. **Action Required After First Run:** Since this repository is private, the new package will be created as **Private** by default. To make it publicly accessible: 1. Wait for the first Action run to complete successfully. 2. Go to the repository/organization's **Packages** page. 3. Select the `execution-events-example` package. 4. Go to **Package Settings** -> **Change visibility**. 5. Change visibility from **Private** to **Public**. --------- Co-authored-by: Brandon Graham <brandon@monad.foundation>
1 parent 2282403 commit 9758177

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Publish Backend Container
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
paths:
7+
- 'backend/**'
8+
- '.github/workflows/publish-backend.yml'
9+
workflow_dispatch:
10+
11+
env:
12+
REGISTRY: ghcr.io
13+
IMAGE_NAME: ${{ github.repository }}
14+
15+
jobs:
16+
build-and-push:
17+
runs-on: ubuntu-latest
18+
permissions:
19+
contents: read
20+
packages: write
21+
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
25+
26+
- name: Log in to the Container registry
27+
uses: docker/login-action@v3
28+
with:
29+
registry: ${{ env.REGISTRY }}
30+
username: ${{ github.actor }}
31+
password: ${{ secrets.GITHUB_TOKEN }}
32+
33+
- name: Extract metadata (tags, labels) for Docker
34+
id: meta
35+
uses: docker/metadata-action@v5
36+
with:
37+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
38+
tags: |
39+
type=raw,value=latest,enable={{is_default_branch}}
40+
type=sha,format=short
41+
42+
- name: Build and push Docker image
43+
uses: docker/build-push-action@v5
44+
with:
45+
context: ./backend
46+
file: ./backend/Dockerfile_build_and_publish
47+
push: true
48+
tags: ${{ steps.meta.outputs.tags }}
49+
labels: ${{ steps.meta.outputs.labels }}
50+

backend/.dockerignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Git files
2+
.git
3+
.gitignore
4+
.github
5+
6+
# Build artifacts
7+
target/
8+
9+
# Documentation
10+
*.md
11+
README*
12+
CHANGELOG*
13+
14+
# Docker files
15+
.dockerignore
16+
Dockerfile*
17+
docker-compose*
18+
19+
# IDE and editor files
20+
.vscode
21+
.idea
22+
*.iml
23+
.DS_Store
24+
*.swp
25+
*.swo
26+
*~
27+
28+
# Environment files
29+
.env
30+
.env.local
31+
.env.*.local
32+
33+
# Test and coverage
34+
*.test
35+
coverage/
36+
.nyc_output
37+
38+
# Logs
39+
*.log
40+
logs/

backend/Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM rust:1.91-slim
2+
3+
# Install build dependencies
4+
RUN apt-get update && apt-get install -y \
5+
git \
6+
curl \
7+
gcc \
8+
g++ \
9+
cmake \
10+
pkg-config \
11+
libssl-dev \
12+
libclang-dev \
13+
libzstd-dev \
14+
libhugetlbfs-dev \
15+
&& rm -rf /var/lib/apt/lists/*
16+
17+
# Set working directory
18+
WORKDIR /usr/src/app
19+
20+
# Copy all source files
21+
COPY . .
22+
23+
# Build the binary
24+
RUN cargo build --release --bin backend
25+
26+
# Expose WebSocket port
27+
EXPOSE 8443
28+
29+
# Set entrypoint with default server address for container
30+
ENTRYPOINT ["cargo", "run", "--release", "--bin", "backend", "--"]
31+
CMD ["--server-addr", "0.0.0.0:8443"]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Builder stage
2+
FROM rust:1.91-slim AS builder
3+
4+
# Install build dependencies
5+
RUN apt-get update && apt-get install -y \
6+
git \
7+
curl \
8+
gcc \
9+
g++ \
10+
cmake \
11+
pkg-config \
12+
libssl-dev \
13+
libclang-dev \
14+
libzstd-dev \
15+
libhugetlbfs-dev \
16+
&& rm -rf /var/lib/apt/lists/*
17+
18+
# Set working directory
19+
WORKDIR /usr/src/app
20+
21+
# Copy all source files
22+
COPY . .
23+
24+
# Build the binary
25+
RUN cargo build --release --bin backend
26+
27+
# Runtime stage
28+
FROM debian:bookworm-slim AS runtime
29+
30+
# Install runtime dependencies
31+
RUN apt-get update && apt-get install -y \
32+
libssl3 \
33+
libzstd1 \
34+
libhugetlbfs0 \
35+
ca-certificates \
36+
&& rm -rf /var/lib/apt/lists/*
37+
38+
# Set working directory
39+
WORKDIR /usr/src/app
40+
41+
# Copy the compiled binary from the builder stage
42+
COPY --from=builder /usr/src/app/target/release/backend /usr/local/bin/backend
43+
44+
# Expose WebSocket port
45+
EXPOSE 8443
46+
47+
# Set entrypoint
48+
ENTRYPOINT ["backend"]
49+
CMD ["--server-addr", "0.0.0.0:8443"]
50+

docker-compose.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '3.9'
2+
3+
services:
4+
backend:
5+
build:
6+
context: ./backend
7+
dockerfile: Dockerfile
8+
container_name: backend
9+
network_mode: host
10+
environment:
11+
- RUST_LOG=info
12+
volumes:
13+
# Mount the Monad event ring (read-only)
14+
# Note: Update the host path if your event ring is located elsewhere
15+
- /var/lib/hugetlbfs/user/monad/pagesize-2MB/event-rings:/var/lib/hugetlbfs/user/monad/pagesize-2MB/event-rings:ro
16+
# Optional: Persistent logs volume
17+
- logs:/var/log/eventwatch
18+
restart: unless-stopped
19+
command: ["--server-addr", "0.0.0.0:8443"]
20+
logging:
21+
driver: json-file
22+
options:
23+
max-size: "10m"
24+
max-file: "3"
25+
26+
volumes:
27+
logs:

0 commit comments

Comments
 (0)