docker.recipes

Phoenix + PostgreSQL

intermediate

Elixir Phoenix framework with PostgreSQL and LiveView.

Overview

Phoenix is a modern web framework built on the Elixir programming language and the Erlang Virtual Machine (BEAM), created by Chris McCord in 2014. Phoenix leverages the Actor Model and fault-tolerance principles of Erlang to deliver exceptional performance and reliability for web applications. The framework is particularly renowned for LiveView, a revolutionary feature that enables real-time, interactive user interfaces with server-rendered HTML, eliminating the need for complex JavaScript frameworks while maintaining rich user experiences. Phoenix applications can handle millions of concurrent connections with minimal resource usage, making it ideal for real-time applications like chat systems, collaboration tools, and live dashboards. This stack combines Phoenix with PostgreSQL to create a robust foundation for data-intensive web applications that demand both high concurrency and strong data integrity. PostgreSQL's ACID compliance and advanced querying capabilities complement Phoenix's real-time features, enabling applications to handle complex business logic while maintaining data consistency across thousands of simultaneous users. The combination is particularly powerful for applications requiring real-time updates backed by reliable data persistence, such as financial dashboards, inventory management systems, or collaborative platforms where data accuracy is critical. This configuration is ideal for development teams building modern web applications that need to scale efficiently while maintaining code simplicity. Elixir developers will appreciate the productive development experience with hot code reloading and pattern matching, while database administrators benefit from PostgreSQL's mature ecosystem and extensive monitoring capabilities. The stack is particularly valuable for companies transitioning from monolithic architectures to more resilient systems, as Phoenix's supervised processes and 'let it crash' philosophy provide exceptional fault tolerance without the complexity of microservices.

Key Features

  • LiveView real-time UI updates with server-rendered HTML and minimal JavaScript
  • Phoenix Channels for WebSocket-based real-time communication and pub/sub messaging
  • Ecto ORM with PostgreSQL providing compile-time query validation and database migrations
  • PostgreSQL JSONB support for flexible schema design alongside relational data
  • Phoenix PubSub for distributed real-time messaging across multiple server nodes
  • Hot code reloading in development for instant feedback during Elixir development
  • Supervision trees ensuring automatic recovery from process failures
  • PostgreSQL's advanced indexing including GIN indexes for JSONB and full-text search

Common Use Cases

  • 1Real-time collaboration platforms like document editors or project management tools
  • 2Financial dashboards requiring live data updates with strict data consistency
  • 3E-commerce platforms with real-time inventory tracking and order processing
  • 4Chat applications and messaging systems leveraging Phoenix Channels
  • 5IoT data collection and monitoring dashboards with live sensor updates
  • 6Gaming leaderboards and live tournament tracking with instant score updates
  • 7Customer support systems with real-time ticket updates and live chat functionality

Prerequisites

  • Docker and Docker Compose installed on the host system
  • Minimum 1GB RAM recommended for PostgreSQL optimal performance
  • Port 4000 available for Phoenix web server access
  • Generated SECRET_KEY_BASE environment variable using mix phx.gen.secret
  • Basic understanding of Elixir syntax and functional programming concepts
  • Familiarity with PostgreSQL database concepts and SQL querying

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 phoenix:
3 build: .
4 container_name: phoenix
5 environment:
6 DATABASE_URL: ecto://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
7 SECRET_KEY_BASE: ${SECRET_KEY_BASE}
8 ports:
9 - "4000:4000"
10 depends_on:
11 - postgres
12 networks:
13 - phoenix-network
14
15 postgres:
16 image: postgres:16-alpine
17 environment:
18 POSTGRES_DB: ${DB_NAME}
19 POSTGRES_USER: ${DB_USER}
20 POSTGRES_PASSWORD: ${DB_PASSWORD}
21 volumes:
22 - postgres_data:/var/lib/postgresql/data
23 networks:
24 - phoenix-network
25
26volumes:
27 postgres_data:
28
29networks:
30 phoenix-network:
31 driver: bridge

.env Template

.env
1DB_NAME=phoenix
2DB_USER=phoenix
3DB_PASSWORD=changeme
4SECRET_KEY_BASE=your-secret-key-here

Usage Notes

  1. 1Docs: https://hexdocs.pm/phoenix/
  2. 2Access at http://localhost:4000
  3. 3LiveView: real-time server-rendered UIs without JavaScript
  4. 4Channels for WebSocket-based features
  5. 5Run migrations: docker compose exec phoenix mix ecto.migrate
  6. 6Generate SECRET_KEY_BASE: mix phx.gen.secret

Individual Services(2 services)

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

phoenix
phoenix:
  build: .
  container_name: phoenix
  environment:
    DATABASE_URL: ecto://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
    SECRET_KEY_BASE: ${SECRET_KEY_BASE}
  ports:
    - "4000:4000"
  depends_on:
    - postgres
  networks:
    - phoenix-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:
    - phoenix-network

Quick Start

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

Troubleshooting

  • Connection refused to PostgreSQL: Ensure postgres service starts before phoenix using depends_on and verify DATABASE_URL format
  • LiveView not updating in real-time: Check that Phoenix.PubSub is properly configured and WebSocket connections aren't blocked by firewalls
  • Ecto migration failures: Run docker compose exec phoenix mix ecto.create before running migrations if database doesn't exist
  • SECRET_KEY_BASE missing error: Generate key with mix phx.gen.secret and add to environment variables in docker-compose.yml
  • Phoenix server crash on startup: Verify Elixir application dependencies are properly compiled by rebuilding the Docker image
  • Database encoding issues: Ensure PostgreSQL locale is set to UTF-8 compatible encoding in the postgres service environment

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