feat: Add Dockerfile and Docker Compose configuration for containerized deployment and update README with instructions.

This commit is contained in:
2026-03-03 19:19:47 +00:00
parent bfcf45343f
commit b93d4548d4
3 changed files with 91 additions and 0 deletions

46
Dockerfile Normal file
View File

@@ -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"]

View File

@@ -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
```

13
docker-compose.yml Normal file
View File

@@ -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