diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..95ff3c1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,46 @@ +# Build Stage +FROM rust:1.85-bookworm AS builder + +WORKDIR /app + +# Cache dependencies +# We create a dummy project to build only dependencies +RUN cargo new --bin blog_cms +COPY Cargo.toml Cargo.lock ./ +RUN cargo build --release +RUN rm src/*.rs + +# Copy real source +COPY src ./src +COPY templates ./templates + +# The templates are needed during compilation by askama +# Rebuild the real source +RUN touch src/main.rs && cargo build --release + +# Final Stage +FROM debian:bookworm-slim + +WORKDIR /app + +# Install runtime dependencies (like SSL certificates and sqlite) +RUN apt-get update && apt-get install -y \ + ca-certificates \ + libsqlite3-0 \ + && rm -rf /var/lib/apt/lists/* + +# Copy the binary from the builder +COPY --from=builder /app/target/release/blog_cms . + +# Create directory for attachments and database +RUN mkdir -p attachments + +# Default environment variables +ENV PORT=3000 +ENV DATABASE_URL=sqlite:///app/data.db + +# Expose the application port +EXPOSE 3000 + +# Start the application +CMD ["./blog_cms"] diff --git a/README.md b/README.md index 90d0e04..cfa38de 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ sqlx database create ``` *(Optionally run migrations if any exist: `sqlx migrate run`)* + ## Running the app To start the server, simply run: @@ -36,3 +37,34 @@ cargo run ``` Then, open your web browser and navigate to `http://localhost:3000` (or whichever port you configured). + +## Run with Docker + +You can also run this project using Docker. + +### Using Docker Compose (Recommended) + +To start the application and its dependencies (like the database and attachment storage): + +```bash +docker-compose up -d --build +``` + +The application will be accessible at `http://localhost:3000`. Persistent data (the database and attachments) will be stored in the local directory. + +### Using Docker Directly + +1. Build the image: + ```bash + docker build -t blog-cms . + ``` + +2. Run the container: + ```bash + docker run -d \ + -p 3000:3000 \ + -v $(pwd)/data.db:/app/data.db \ + -v $(pwd)/attachments:/app/attachments \ + --name blog-cms \ + blog-cms + ``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c1a94ed --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +version: '3.8' +services: + blog: + build: . + ports: + - "3000:3000" + environment: + - PORT=3000 + - DATABASE_URL=sqlite:///app/data.db + volumes: + - ./data.db:/app/data.db + - ./attachments:/app/attachments + restart: always