1

I setup a prisma project recently and here is my docker-compose.yml file

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.31
    restart: always
    ports:
      - '4030:4466'
    environment:
      TZ: ${PRISMA_DB_TIME_ZONE}
      PRISMA_CONFIG: |
        port: 4466
        # managementApiSecret: my-secret
        databases:
          default:
            connector: postgres
            host: postgres
            port: 5432
            user: prisma
            password: ${PRISMA_DB_PASSWORD}
            migrations: true
            rawAccess: true
  postgres:
    image: postgres:10.3
    restart: always
    ports:
    - "3306:3306"
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
      TZ: ${PRISMA_DB_TIME_ZONE}
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
   prisma:
   postgres:

I can open my prisma playground and it functions without any issue. but I can't create a direct connection to the postgre container with dbeaver.

dbeaver Error message

Connection reset

Why my connection to the database fails?

This photo will be helpful. enter image description here

Dulara Malindu
  • 1,477
  • 4
  • 18
  • 37

1 Answers1

1

postgres by default listens on port 5432.

In your postgres container specification, you should expose the port 5432 not 3306.

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.31
    restart: always
    ports:
      - '4030:4466'
    environment:
      TZ: ${PRISMA_DB_TIME_ZONE}
      PRISMA_CONFIG: |
        port: 4466
        # managementApiSecret: my-secret
        databases:
          default:
            connector: postgres
            host: postgres
            port: 5432
            user: prisma
            password: ${PRISMA_DB_PASSWORD}
            migrations: true
            rawAccess: true
  postgres:
    image: postgres:10.3
    restart: always
    ports:
    - "5432:5432"
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
      TZ: ${PRISMA_DB_TIME_ZONE}
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
   prisma:
   postgres:

If 5432 port in your host is already in use and if you want to use 3306 instead , then you can do port forwarding like below:

  postgres:
    image: postgres:10.3
    restart: always
    ports:
    - "3306:5432"
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
      TZ: ${PRISMA_DB_TIME_ZONE}
    volumes:
      - postgres:/var/lib/postgresql/data

UPDATE - 1

Reason why prisma can access postgres

ports section is meant only to make our services accessible in host level. But in the container level, If a port is open in a container, any other running container can access that port with the help of networks or links section.

By default, docker-compose will create a network per docker-compose.yml file and joins all the services in that file into that network.

That's the reason we could use the <service name> as the hostname and compose will resolve that name to the ip-address of the respective (in your case postgres) container.

Thilak
  • 935
  • 8
  • 12
  • Thank you.! database connection is working now. but how did the prisma connected to the postgre container with my docker-compose.yml settings? I was using 3306:3306 I could create new nodes and all the data manipulations through the prisma playground successfully saved to the postgres. – Dulara Malindu May 22 '19 at 10:33
  • **ports** section is meant only to make our services accessible in **host** level. But in the container level, If a port is open in a container, any other running container can access that port with the help of **networks or links** section. By default, **docker-compose** will create a network per `docker-compose.yml` file and joins all the services in that file into that network. That's the reason we could use the `` as the hostname and compose will resolve that name to the ip-address of the respective (in your case **postgres**) container. – Thilak May 23 '19 at 04:26