Francisco Aguilera
Francisco Aguilera

Reputation: 147

Postgres SSL connection error in Docker/Portainer despite SSL explicitly disabled

I'm trying to deploy a FastAPI application with PostgreSQL using Docker Compose in Portainer, but I'm getting SSL-related connection errors despite explicitly disabling SSL. It works on local, but when deploying to Portainer, the next error araises:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "db" (172.27.0.2), port 5432 failed: server does not support SSL, but SSL was required

docker-compose.yml

services:
  backend:
    container_name: backend
    build:
      context: ./balancer
      dockerfile: Dockerfile.prod
    ports:
      - "8000:8000"
    env_file:
      - stack.env
    environment:
      PORT: 8000
      PYTHONPATH: /app
      ENVIRONMENT: production
      DISABLE_SSL: "true"
    depends_on:
      - db

  db:
    container_name: db
    image: postgres:15-alpine
    ports:
      - "5432:5432"
    env_file:
      - stack.env
    environment:
      POSTGRES_DB: ${DATABASE}
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      POSTGRES_HOST_AUTH_METHOD: trust
    command: 
      - "postgres"
      - "-c"
      - "ssl=off"
    volumes:
      - postgres_data:/var/lib/postgresql/data

database.py

def get_database_url():
    if settings.USE_SQLITE:
        return settings.DATABASE_URL
    return f"postgresql://{settings.DATABASE_USER}:{settings.DATABASE_PASSWORD}@{settings.DATABASE_HOST}:{settings.DATABASE_PORT}/{settings.DATABASE}?sslmode=disable"

try:
    connect_args = {"check_same_thread": False} if settings.USE_SQLITE else {
        "connect_timeout": 5
    }
    engine = create_engine(
        get_database_url(),
        connect_args=connect_args,
        pool_pre_ping=True
    )

And these are the variables that appear in the Portainer info for the backend container:

0 DATABASE_HOST=db
1 DATABASE_PASSWORD=balancer
5 DATABASE_USER=balancer
12 DATABASE_PORT=5432
22 DATABASE=balancer
13 DISABLE_SSL=true

I've tried almost everything but I do not know how to continue with this. I don't get why it works locally but not on the Portainer.

Thanks in advance.

Upvotes: 0

Views: 62

Answers (0)

Related Questions