Share my Dockerfile and docker-compose.yml of a actix web + sqlx + postgress project #3964
Closed
dwslo
started this conversation in
Show and tell
Replies: 3 comments
-
important: : # ---- Build stage ----
FROM rust:latest AS build
WORKDIR /app
# System deps for build (OpenSSL headers etc.)
RUN apt-get update && apt-get install -y --no-install-recommends \
pkg-config libssl-dev && rm -rf /var/lib/apt/lists/*
# Cache deps
COPY Cargo.toml Cargo.lock ./
COPY .sqlx ./.sqlx
COPY src ./src
COPY migrations ./migrations
# If you use SQLX offline, copy the data file
# COPY sqlx-data.json ./sqlx-data.json
# Only set this if you actually have sqlx-data.json
ENV DATABASE_URL=postgres://postgres:postgres@localhost:5432/aihub
ENV SQLX_OFFLINE=true
# Produce smaller binary
ENV RUSTFLAGS="-C target-cpu=native -C strip=symbols"
RUN cargo build --release
# ---- Runtime stage ----
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates tzdata \
# If using native-tls/openssl at runtime, keep libssl:
libssl3 \
&& rm -rf /var/lib/apt/lists/*
# Non-root user
RUN useradd -m -U -s /usr/sbin/nologin appuser
WORKDIR /home/appuser
COPY --from=build /app/target/release/aihub /usr/local/bin/aihub
ENV RUST_LOG=info
USER appuser
# Make sure this matches what your server actually binds to
EXPOSE 8111
# Optional: healthcheck (tweak path/port)
# HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
# CMD wget -qO- http://127.0.0.1:8111/health || exit 1
CMD ["/usr/local/bin/aihub"] |
Beta Was this translation helpful? Give feedback.
0 replies
-
if don't need network, delete all about network is ok services:
db:
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: aihub
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d aihub"]
interval: 3s
timeout: 3s
retries: 20
restart: unless-stopped
networks:
- aihub
api:
build:
context: .
dockerfile: Dockerfile
env_file: .env
depends_on:
db:
condition: service_healthy
ports:
- "8111:8111"
command: ["/usr/local/bin/aihub"]
restart: unless-stopped
networks:
- aihub
extra_hosts:
- "host.docker.internal:host-gateway"
# Optional: a simple DB UI
# adminer:
# image: adminer
# ports: ["8081:8080"]
# depends_on:
# db:
# condition: service_healthy
volumes:
pgdata:
networks:
aihub:
external: true
driver: bridge
name: aihub |
Beta Was this translation helpful? Give feedback.
0 replies
-
connect database, create data: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Start Server in development environment
Running Actix + SQLx Backend via Docker Compose
This guide describes how to set up and run a Rust (Actix Web + SQLx) backend with PostgreSQL using Docker Compose.
1. Start the PostgreSQL service
1.1 Start PostgreSQL container:
postgres:latest
5432
postgres
postgres
aihub
2. Configure database connection
2.1 Set the
DATABASE_URL
for your current shell session:export DATABASE_URL=postgres://postgres:postgres@localhost:5432/aihub
Format:
3. Install SQLx CLI
4. Create migration files
4.1 Create a new migration file with a timestamp:
Cargo.toml
../migrations
directory.4.2 Edit the generated
.sql
file to add your SQL schema.5. Create the database
6. Run migrations
7. Generate SQLx offline cache (optional but recommended)
If you use:
SQLX_OFFLINE=true
in your shell session, orENV SQLX_OFFLINE=true
in your DockerfileYou must generate the offline cache before building the Docker image:
This will create the
.sqlx/
directory containing the query metadata required for offline builds.8. Start the API service
Notes & Common Pitfalls
Local vs. Docker network:
When running inside Docker, use
db
(the Docker Compose service name) as the database host instead oflocalhost
.SQLx offline mode:
If you forget to run
cargo sqlx prepare
before building withSQLX_OFFLINE=true
, the build will fail with "no.sqlx
data found".Rebuilding after migrations:
If your SQL schema changes, regenerate the offline cache before rebuilding the Docker image.
Multiple environments:
For development, use a
.env.dev
file and load it withdotenv
orenv_file
indocker-compose.yml
.Beta Was this translation helpful? Give feedback.
All reactions