Skip to content

Commit e6bebc1

Browse files
committed
Add Docker support with multi-stage build
- Add Dockerfile with two-stage build (Rust 1.92 + Debian trixie-slim) - Add docker-compose.yml for easy container orchestration - Add .dockerignore to optimize build context - Final image size ~176MB - Runs as non-root user for security - Persistent data volume at /data - Default server mode with port 8080 exposed - Update README with Docker usage instructions - Update CHANGELOG for v1.1.0
1 parent 2c143d7 commit e6bebc1

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

.dockerignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Build artifacts
2+
target/
3+
debug/
4+
release/
5+
6+
# Git
7+
.git/
8+
.gitignore
9+
.github/
10+
11+
# IDE and editor files
12+
.idea/
13+
.vscode/
14+
*.swp
15+
*.swo
16+
*~
17+
.DS_Store
18+
19+
# Documentation (not needed for build, but keep README.md for Cargo.toml)
20+
CHANGELOG.md
21+
22+
# Claude/AI assistant files
23+
.claude/
24+
25+
# Test files
26+
tests/
27+
**/*_test.rs
28+
29+
# Development files
30+
DEVELOPMENT.md
31+
ARCHITECTURE.md
32+
33+
# Docker files (prevent recursive issues)
34+
Dockerfile
35+
docker-compose.yml
36+
.dockerignore
37+
38+
# Misc
39+
*.log
40+
*.bak
41+
*.tmp
42+
.env
43+
.env.*

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ All notable changes to this project will be documented in this file.
7373
* Tests for schema version verification
7474
* Tests for RPKI and Pfx2as mock data storage/retrieval
7575

76+
* Added Docker support with multi-stage build
77+
* `Dockerfile` with two-stage build process for minimal image size (~176MB final image)
78+
* Uses Rust 1.92 and Debian trixie-slim as runtime base
79+
* `docker-compose.yml` for easy container orchestration
80+
* `.dockerignore` to optimize build context
81+
* Runs as non-root user for security
82+
* Persistent data volume at `/data`
83+
* Default server mode with port 8080 exposed
84+
7685
## v1.0.1 - 2025-12-17
7786

7887
### Bug Fixes

Dockerfile

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# =============================================================================
2+
# Stage 1: Build
3+
# =============================================================================
4+
FROM rust:1.92-trixie AS builder
5+
6+
WORKDIR /usr/src/monocle
7+
8+
# Install build dependencies
9+
RUN apt-get update && apt-get install -y --no-install-recommends \
10+
pkg-config \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
# Copy manifests first for better layer caching
14+
COPY Cargo.toml Cargo.lock ./
15+
16+
# Create a dummy main and lib to build dependencies
17+
RUN mkdir -p src/bin && \
18+
echo 'fn main() { println!("dummy"); }' > src/bin/monocle.rs && \
19+
echo '#![allow(dead_code)]' > src/lib.rs
20+
21+
# Build dependencies only (this layer will be cached)
22+
# Must use same features as final build
23+
RUN cargo build --release --features cli || true
24+
RUN rm -rf src
25+
26+
# Copy the actual source code
27+
COPY src ./src
28+
COPY examples ./examples
29+
COPY README.md ./
30+
31+
# Touch the source files to ensure they're rebuilt
32+
RUN touch src/lib.rs src/bin/monocle.rs
33+
34+
# Build the actual binary
35+
RUN cargo build --release --features cli
36+
37+
# =============================================================================
38+
# Stage 2: Runtime
39+
# =============================================================================
40+
FROM debian:trixie-slim AS runtime
41+
42+
# Install runtime dependencies
43+
RUN apt-get update && apt-get install -y --no-install-recommends \
44+
ca-certificates \
45+
&& rm -rf /var/lib/apt/lists/*
46+
47+
# Create a non-root user for security
48+
RUN useradd --create-home --shell /bin/bash monocle
49+
50+
# Create data directory
51+
RUN mkdir -p /data && \
52+
chown -R monocle:monocle /data
53+
54+
# Copy the binary from builder
55+
COPY --from=builder /usr/src/monocle/target/release/monocle /usr/local/bin/monocle
56+
57+
# Switch to non-root user
58+
USER monocle
59+
WORKDIR /home/monocle
60+
61+
# Set environment variables
62+
ENV MONOCLE_DATA_DIR=/data
63+
64+
# Define volume for persistent data
65+
VOLUME ["/data"]
66+
67+
# Expose the default server port
68+
EXPOSE 8080
69+
70+
# Default command shows help; override with your desired command
71+
ENTRYPOINT ["monocle"]
72+
CMD ["--help"]

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,37 @@ Then install `monocle` using `cargo binstall`
6161
cargo binstall monocle
6262
```
6363

64+
### Using Docker
65+
66+
Pull the pre-built image or build locally:
67+
68+
```bash
69+
# Build the image locally
70+
docker build -t bgpkit/monocle:latest .
71+
72+
# Or use docker compose
73+
docker compose build
74+
```
75+
76+
Run monocle commands:
77+
78+
```bash
79+
# Show help
80+
docker run --rm bgpkit/monocle:latest
81+
82+
# Run a command (e.g., inspect an ASN)
83+
docker run --rm bgpkit/monocle:latest inspect 13335
84+
85+
# Run with persistent data directory
86+
docker run --rm -v monocle-data:/data bgpkit/monocle:latest inspect 13335
87+
88+
# Start the WebSocket server
89+
docker run --rm -p 8080:8080 -v monocle-data:/data bgpkit/monocle:latest server --address 0.0.0.0 --port 8080
90+
91+
# Using docker compose for server mode
92+
docker compose up -d
93+
```
94+
6495
## Library Usage
6596

6697
Monocle can also be used as a library in your Rust projects. Add it to your `Cargo.toml`:

docker-compose.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Docker Compose configuration for monocle
2+
#
3+
# Usage:
4+
# docker compose up -d # Start server in background
5+
# docker compose run monocle <cmd> # Run a one-off command
6+
# docker compose logs -f # View logs
7+
# docker compose down # Stop and remove containers
8+
#
9+
# Examples:
10+
# docker compose run monocle inspect 13335
11+
# docker compose run monocle search --start-ts "1 hour ago" --prefix 1.1.1.0/24
12+
# docker compose run monocle rpki validate 13335 1.1.1.0/24
13+
14+
services:
15+
monocle:
16+
build:
17+
context: .
18+
dockerfile: Dockerfile
19+
image: bgpkit/monocle:latest
20+
container_name: monocle
21+
22+
# Persist monocle data (database, config) across container restarts
23+
volumes:
24+
- monocle-data:/data
25+
26+
# Server mode configuration
27+
ports:
28+
- "8080:8080"
29+
30+
# Override default command to run the WebSocket server
31+
# Comment this out if you want to use monocle as a CLI tool only
32+
command: ["server", "--address", "0.0.0.0", "--port", "8080"]
33+
34+
# Environment variables
35+
environment:
36+
- MONOCLE_DATA_DIR=/data
37+
# Uncomment to accept invalid TLS certificates (useful for corporate proxies)
38+
# - ONEIO_ACCEPT_INVALID_CERTS=true
39+
40+
# Health check for server mode
41+
healthcheck:
42+
test: ["CMD", "monocle", "--help"]
43+
interval: 30s
44+
timeout: 10s
45+
retries: 3
46+
start_period: 10s
47+
48+
# Resource limits (optional, adjust as needed)
49+
deploy:
50+
resources:
51+
limits:
52+
memory: 2G
53+
reservations:
54+
memory: 256M
55+
56+
# Restart policy
57+
restart: unless-stopped
58+
59+
volumes:
60+
monocle-data:
61+
driver: local

0 commit comments

Comments
 (0)