docker.recipes

Actix Web + PostgreSQL

advanced

High-performance Rust web framework with PostgreSQL.

Overview

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust built on the Actor model. Originally created by Nikolay Kim in 2018, Actix Web has consistently ranked as one of the fastest web frameworks in any programming language, leveraging Rust's memory safety and zero-cost abstractions to deliver exceptional performance. The framework uses an actor-based architecture where each component operates as an independent actor, communicating through message passing, which naturally provides excellent concurrency and fault tolerance. This stack combines Actix Web's blazing-fast HTTP handling capabilities with PostgreSQL's enterprise-grade database features, creating a high-performance backend solution optimized for demanding applications. Actix Web's async-first design pairs exceptionally well with PostgreSQL's robust connection pooling and transaction management, while libraries like SQLx provide compile-time checked queries that eliminate runtime SQL errors. The combination delivers both raw speed and data reliability, making it ideal for applications that cannot compromise on either performance or data integrity. This configuration is perfect for Rust developers building production-grade APIs, financial technology platforms, IoT data collection systems, or any high-throughput application requiring guaranteed data consistency. Companies migrating from slower interpreted languages often choose this stack to achieve 10x+ performance improvements while maintaining PostgreSQL's proven reliability. The learning curve is steep due to Rust's ownership model, but the payoff includes memory safety, predictable performance, and significantly reduced infrastructure costs due to lower resource consumption.

Key Features

  • Actor-based concurrency model enabling millions of concurrent connections with minimal resource overhead
  • Compile-time SQL query validation through SQLx preventing runtime database errors
  • Zero-copy request parsing and response generation for maximum throughput
  • Built-in middleware ecosystem including authentication, CORS, logging, and request compression
  • PostgreSQL's JSONB support for hybrid relational-document data models with full indexing
  • Advanced connection pooling with configurable pool sizes and connection lifecycle management
  • Hot-reloading development server with automatic restart on code changes
  • Native support for HTTP/2, WebSockets, and Server-Sent Events

Common Use Cases

  • 1High-frequency trading platforms requiring microsecond response times with ACID transaction guarantees
  • 2IoT data ingestion APIs handling millions of sensor readings per minute
  • 3Financial services backends processing payment transactions with strict data consistency requirements
  • 4Real-time multiplayer game servers with persistent player data and leaderboards
  • 5E-commerce platforms managing inventory, orders, and user sessions under heavy load
  • 6Analytics APIs serving complex queries over large datasets with PostgreSQL's window functions
  • 7Cryptocurrency exchanges requiring ultra-low latency order matching and trade execution

Prerequisites

  • Rust development environment with Cargo package manager installed
  • Minimum 2GB RAM (4GB+ recommended for PostgreSQL under load)
  • Understanding of Rust ownership concepts and async/await programming model
  • Familiarity with PostgreSQL schema design and query optimization
  • Docker port 8080 available for Actix Web HTTP server
  • Basic knowledge of Actor model patterns and message-driven architecture

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 actix:
3 build: .
4 container_name: actix
5 environment:
6 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
7 ports:
8 - "8080:8080"
9 depends_on:
10 - postgres
11 networks:
12 - actix-network
13
14 postgres:
15 image: postgres:16-alpine
16 environment:
17 POSTGRES_DB: ${DB_NAME}
18 POSTGRES_USER: ${DB_USER}
19 POSTGRES_PASSWORD: ${DB_PASSWORD}
20 volumes:
21 - postgres_data:/var/lib/postgresql/data
22 networks:
23 - actix-network
24
25volumes:
26 postgres_data:
27
28networks:
29 actix-network:
30 driver: bridge

.env Template

.env
1DB_NAME=actix
2DB_USER=actix
3DB_PASSWORD=changeme

Usage Notes

  1. 1Docs: https://actix.rs/docs/
  2. 2Access at http://localhost:8080
  3. 3One of the fastest web frameworks (any language)
  4. 4Actor model for concurrency
  5. 5Use SQLx for async database access
  6. 6Multi-stage Dockerfile: build with rust, run with minimal image

Individual Services(2 services)

Copy individual services to mix and match with your existing compose files.

actix
actix:
  build: .
  container_name: actix
  environment:
    DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
  ports:
    - "8080:8080"
  depends_on:
    - postgres
  networks:
    - actix-network
postgres
postgres:
  image: postgres:16-alpine
  environment:
    POSTGRES_DB: ${DB_NAME}
    POSTGRES_USER: ${DB_USER}
    POSTGRES_PASSWORD: ${DB_PASSWORD}
  volumes:
    - postgres_data:/var/lib/postgresql/data
  networks:
    - actix-network

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 actix:
5 build: .
6 container_name: actix
7 environment:
8 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
9 ports:
10 - "8080:8080"
11 depends_on:
12 - postgres
13 networks:
14 - actix-network
15
16 postgres:
17 image: postgres:16-alpine
18 environment:
19 POSTGRES_DB: ${DB_NAME}
20 POSTGRES_USER: ${DB_USER}
21 POSTGRES_PASSWORD: ${DB_PASSWORD}
22 volumes:
23 - postgres_data:/var/lib/postgresql/data
24 networks:
25 - actix-network
26
27volumes:
28 postgres_data:
29
30networks:
31 actix-network:
32 driver: bridge
33EOF
34
35# 2. Create the .env file
36cat > .env << 'EOF'
37DB_NAME=actix
38DB_USER=actix
39DB_PASSWORD=changeme
40EOF
41
42# 3. Start the services
43docker compose up -d
44
45# 4. View logs
46docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/actix-postgres/run | bash

Troubleshooting

  • Connection pool exhausted errors: Increase max_connections in PostgreSQL config and tune SQLx pool settings
  • Cargo build failures with linking errors: Ensure PostgreSQL development headers are available in build container
  • High memory usage during compilation: Use multi-stage Dockerfile with separate build and runtime images
  • Database connection timeouts: Verify DATABASE_URL format and network connectivity between containers
  • Actix runtime panics on heavy load: Tune worker thread counts and async task spawning limits
  • SQLx migration failures: Check PostgreSQL user permissions and ensure database exists before running migrations

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Ad Space