Reputation: 1600
Technologies I use:
I'm trying to run these technologies using Docker but I'm running into the following issue:
prisma schema loaded from prisma/schema.prism
Datasource "db": PostgreSQL database "nestjs", schema "public" at "localhost:5200"
Error: P1001: Can't reach database server at `localhost`:`5200`
Please make sure your database server is running at `localhost`:`5200`
docker-compose.dev.yml
version: '3.7'
services:
db:
image: postgres:12.9
ports:
- 5200:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123
POSTGRES_DB: nestjs
volumes:
- database-data:/var/lib/postgresql/data
networks:
- sai
restart: always
test:
container_name: test
image: test
build:
context: .
target: development
dockerfile: Dockerfile
command: npm run start:prod
ports:
- 3000:3000
- 9229:9229
networks:
- sai
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
links:
- db
depends_on:
- db
restart: always
networks:
sai:
driver: bridge
volumes:
database-data:
Nestjs does not see my locahost database on port 5200.
Dockerfile file:
FROM node:latest as development
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
COPY . .
RUN npm run build
FROM node:latest as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=development /usr/src/app/prisma ./prisma
COPY --from=development /usr/src/app/dist ./dist
EXPOSE 3000
CMD npm run start:prod
The npm run start:prod command also corresponds to the following in the package.json file:
...
"generate:prisma": "npx prisma migrate dev --name init",
"start:prod": "npm run generate:prisma && npm run dist/main",
...
Upvotes: 10
Views: 35500
Reputation: 9
In my case:
docker-compose
file for Windows you need to use host: host.docker.internal
and exposed port in my case 5433
:
(DATABASE_URL=postgresql://postgres:[email protected]:5433/user-accounts
)version: '3.1'
services:
postgres:
image: postgres:15
restart: always
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=user-accounts
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- '5433:5432'
volumes:
- ./docker-data/user-accounts:/var/lib/postgresql/data
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 5s
timeout: 5s
server:
build:
context: .
dockerfile: Dockerfile
command: sh -c "npx prisma db push && npm start"
environment:
- PORT=3000
- JWT_SECRET=super_secret
- DATABASE_URL=postgresql://postgres:[email protected]:5433/user-accounts
ports:
- '3000:3000'
depends_on:
postgres:
condition: service_healthy
docker-compose file
for Linux (only database url changed!) you need to use host: postgres
which is the name of my service and port: 5432
(internal port)
(DATABASE_URL=postgresql://postgres:postgres@postgres:5432/user-accounts
)version: '3.1'
services:
postgres:
image: postgres:15
restart: always
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=user-accounts
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- '5433:5432'
volumes:
- ./docker-data/user-accounts:/var/lib/postgresql/data
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 5s
timeout: 5s
server:
build:
context: .
dockerfile: Dockerfile
command: sh -c "npx prisma db push && npm start"
environment:
- PORT=3000
- JWT_SECRET=super_secret
- DATABASE_URL=postgresql://postgres:postgres@postgres:5432/user-accounts
ports:
- '3000:3000'
depends_on:
postgres:
condition: service_healthy
GitHub: https://github.com/KhitrovMaksim/user-accounts
apollo-server-express graphql prisma postgres docker
Upvotes: -1
Reputation: 629
In the docker compose I found:
services:
db-test:
image: postgres:15.2
ports:
- "5433:5432"
and i connected to the db using: localhost:5433
Upvotes: 0
Reputation: 7
Instead of using localhost:5200 as the address of the database, you need to use db:5432.
remomiendo la salucion de Hans Kilian
Upvotes: -1
Reputation: 25154
Instead of using localhost:5200
as the address of the database, you need to use db:5432
.
Localhost and the mapped port are used when connecting from the host machine. You're connecting from another container on the bridge network and there you need to use the service name and the port the container is listening on.
Upvotes: 16