0

I am trying to build a docker image from my project and run it in a container, The project is keystone6 project connecting to a postgres database, everything worked well when I normally run the project and it connects successfully to the database. Here is my dockerfile:

FROM node:18.13.0-alpine3.16
ENV NODE_VERSION 18.13.0

ENV NODE_ENV=development
LABEL Name="di-wrapp" Version="1"

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY . .
RUN npm install

COPY .env .
EXPOSE 9999

CMD ["npm", "run", "dev"]

I am building an image using the command docker build -t di-wrapp:1.0 .

after that I run docker-compose file which contains the following code:

version: "3.8"
services:
  postgres:
    image: postgres:15-alpine
    container_name: localhost
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=di_wrapp
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
  dashboard:
    image: di-wrapp:1.0
    container_name: di-wrapp-container
    restart: always
    environment:
      - DB_CONNECTION=postgres
      - DB_PORT=5432
      - DB_HOST=localhost
      - DB_USER=postgres
      - DB_PASSWORD=postgres
      - DB_NAME=di_wrapp
    tty: true
    depends_on:
      - postgres
    ports:
      - 8273:9999
    links:
      - postgres
    command: "npm run dev"
    volumes:
      - /usr/src/app

volumes:
  postgres-data:

And this is the connection URI used to connect my project to postgres:

DATABASE_URL=postgresql://postgres:postgres@localhost:5432/di_wrapp

which I am using to configure my db setting in keystone config file like this:

export const db: DatabaseConfig<BaseKeystoneTypeInfo> = {
  provider: "postgresql",
  url: String(DATABASE_URL!),
};

when I run the command docker-compose -f docker-compose.yaml up This is what I receive:

localhost           | 2023-02-03 13:43:35.034 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
localhost           | 2023-02-03 13:43:35.034 UTC [1] LOG:  listening on IPv6 address "::", port 5432
localhost           | 2023-02-03 13:43:35.067 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
localhost           | 2023-02-03 13:43:35.121 UTC [24] LOG:  database system was shut down at 2023-02-03 13:43:08 UTC
localhost           | 2023-02-03 13:43:35.155 UTC [1] LOG:  database system is ready to accept connections

di-wrapp-container  | > keystone-app@1.0.2 dev
di-wrapp-container  | > keystone dev
di-wrapp-container  | 
di-wrapp-container  | ✨ Starting Keystone
di-wrapp-container  | ⭐️ Server listening on :8273 (http://localhost:8273/)
di-wrapp-container  | ⭐️ GraphQL API available at /api/graphql
di-wrapp-container  | ✨ Generating GraphQL and Prisma schemas
di-wrapp-container  | Error: P1001: Can't reach database server at `localhost`:`5432`
di-wrapp-container  | 
di-wrapp-container  | Please make sure your database server is running at `localhost`:`5432`.
di-wrapp-container  |     at Object.createDatabase (/usr/src/app/node_modules/@prisma/internals/dist/migrateEngineCommands.js:115:15)
di-wrapp-container  |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
di-wrapp-container  |     at async ensureDatabaseExists (/usr/src/app/node_modules/@keystone-6/core/dist/migrations-e3b5740b.cjs.dev.js:262:19)
di-wrapp-container  |     at async Object.pushPrismaSchemaToDatabase (/usr/src/app/node_modules/@keystone-6/core/dist/migrations-e3b5740b.cjs.dev.js:68:3) 
di-wrapp-container  |     at async Promise.all (index 1)
di-wrapp-container  |     at async setupInitialKeystone (/usr/src/app/node_modules/@keystone-6/core/scripts/cli/dist/keystone-6-core-scripts-cli.cjs.dev.js:984:3)
di-wrapp-container  |     at async initKeystone (/usr/src/app/node_modules/@keystone-6/core/scripts/cli/dist/keystone-6-core-scripts-cli.cjs.dev.js:762:35)di-wrapp-container exited with code 1

even though I receive that the database server is up on port 5432, my app container can't connect to it.

any help is appreciated.

Mustafa
  • 323
  • 2
  • 11
  • 1
    The host should be `postgres`, not `localhost`. – tkausl Feb 03 '23 at 14:04
  • @tkausl I tried that before and it didn't work – Mustafa Feb 03 '23 at 14:09
  • Is the anonymous volume mount keeping you from seeing any changes to your source code; does deleting the `volumes:` block under `dashboard` help? (You should also be able to get rid of `container_name:`, `links:`, and `command:`.) – David Maze Feb 03 '23 at 14:23
  • You cannot override the meaning of the hostname localhost just by assigning the name localhost as container_name. – Mark Rotteveel Feb 05 '23 at 10:01

0 Answers0