# Multi-stage Dockerfile for OpenGrep
FROM rust:1.75-alpine AS builder
# Install system dependencies
RUN apk add --no-cache \
musl-dev \
gcc \
g++ \
make \
curl \
git
# Set working directory
WORKDIR /app
# Copy Cargo files first to leverage Docker layer caching
COPY Cargo.toml Cargo.lock ./
# Create a dummy main.rs to build dependencies
RUN mkdir src && echo "fn main() {}" > src/main.rs
# Build dependencies (this layer will be cached unless Cargo.toml changes)
RUN cargo build --release && rm -rf src
# Copy source code
COPY src ./src
# Build the application
RUN cargo build --release
# Runtime stage
FROM alpine:3.18
# Install runtime dependencies
RUN apk add --no-cache \
ca-certificates \
libgcc
# Create non-root user
RUN addgroup -g 1001 -S opengrep && \
adduser -u 1001 -S opengrep -G opengrep
# Copy binary from builder
COPY --from=builder /app/target/release/opengrep /usr/local/bin/opengrep
# Set permissions
RUN chmod +x /usr/local/bin/opengrep
# Switch to non-root user
USER opengrep
# Set working directory
WORKDIR /workspace
# Default command
ENTRYPOINT ["opengrep"]
CMD ["--help"]
# Labels
LABEL org.opencontainers.image.title="OpenGrep"
LABEL org.opencontainers.image.description="Advanced AST-aware code search with AI-powered insights"
LABEL org.opencontainers.image.version="1.0.0"
LABEL org.opencontainers.image.source="https://github.com/opengrep-org/opengrep"
LABEL org.opencontainers.image.licenses="MIT OR Apache-2.0"