Decorative upper image
Decorative right ellipse
Decorative right ellipse

Simple and Lightweight Rust (Actix + Diesel) Dockerfile

8/22/2025

5 minutes read
0 views
0 comments

Simple Dockerfile for Rust (Actix + Diesel)

Finding the best Dockerfile for Rust can be a challenge, especially when working with Actix and Diesel. Here’s a Dockerfile setup that works well for an Actix + Diesel app using PostgreSQL:

# Build stage
FROM rust:latest AS builder

# Install system dependencies for Diesel with PostgreSQL
RUN apt-get update && \
    apt-get install -y libpq-dev pkg-config && \
    rm -rf /var/lib/apt/lists/*

# Install Diesel CLI for migrations
RUN cargo install diesel_cli --no-default-features --features postgres

WORKDIR /app

# Copy manifest files
COPY Cargo.toml Cargo.lock ./

# Cache dependencies
RUN mkdir -p src && \
    echo "fn main() {}" > src/main.rs && \
    cargo build --release

# Copy source code and migrations
COPY src ./src
COPY migrations ./migrations
COPY .env ./

# Ensure main.rs is up to date
RUN touch src/main.rs

# Build release binary
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim

# Install runtime dependencies for PostgreSQL
RUN apt-get update && \
    apt-get install -y libpq5 && \
    rm -rf /var/lib/apt/lists/*

# Copy built binaries and environment file
COPY --from=builder /usr/local/cargo/bin/diesel /usr/local/bin/
COPY --from=builder /app/target/release/app_name /usr/local/bin/app_name
COPY --from=builder /app/.env /app/.env

WORKDIR /app

# Expose Port
EXPOSE 8000

CMD ["app_name"]

Leave a Comment

0 Comments