Reputation: 21
I really dont know why but my Dockerfile image cannot access my postgres db running with docker compose. Im using Linux Ubuntu
// prisma.schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// package.json
"scripts": {
"build": "npx nest build",
"format": "prettier --write "src/*/.ts" "test/*/.ts"",
"start": "node dist/main",
"start:dev": "npm run start --watch",
"start:repl": "npm run start --entryFile repl",
"start:debug": "npm run start --watch --debug",
"lint": "eslint "{src,apps,libs,test}/\*/.ts" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"prepare": "husky install",
"prisma:dev": "npx prisma migrate dev",
"prisma:reset": "npx prisma migrate reset",
"prisma:deploy": "npx prisma migrate deploy",
"prisma:studio": "npx prisma studio",
"docker:up": "docker compose up",
"docker:down": "docker compose down",
"docker:ls": "docker container ls"
},
// Dockefile
FROM node:18
WORKDIR /usr/src/app
COPY package\*.json ./
RUN npm install
COPY . .
RUN npm run prisma:deploy
RUN npm run build
EXPOSE 8080
CMD \["npm", "run", "start:dev"\]
// docker-compose.yml
version: '3'
services:
postgres:
image: postgres:15.3-alpine3.18
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_DB: personal
POSTGRES_PASSWORD: pass123
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
app:
build: .
depends_on:
- postgres
volumes:
postgres_data:
// What im running
npm run docker:up
// Error im having
> RUN npm run prisma:deploy:
> [email protected] prisma:deploy
> npx prisma migrate deploy
> Environment variables loaded from .env
> Prisma schema loaded from prisma/schema.prisma
> Datasource "db": PostgreSQL database "personal", schema "public" at "localhost:5432"
> Error: P1001: Can't reach database server at localhost:5432
> Please make sure your database server is running at localhost:5432.
// .env
# DATABASE_URL="postgresql://postgres:pass123@postgres:5432/personal?schema=public"
DATABASE_URL="postgresql://postgres:pass123@localhost:5432/personal?schema=public"
PORT=8080
HOST="0.0.0.0"
Obs: if i run docker:up only for postgres with DATABASE_URL="postgresql://postgres:pass123@localhost:5432/personal?schema=public" (on localhost) i can access the postgress normally with DBeaver
I tried to run change DATABASE_URL to: DATABASE_URL="postgresql://postgres:pass123@postgres:5432/personal?schema=public" and DATABASE_URL="postgresql://postgres:pass123@localhost:5432/personal?schema=public"
Upvotes: 1
Views: 1116
Reputation: 21
Another solution i've found too (I'm still accepting other more practical answers to solve this problem):
REMOVE -> DATABASE_URL="xxx" from .env
SET -> DATABASE_URL variable under the docker-compose file
services:
postgres:
image: postgres:15.3-alpine3.18
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_DB: personal
POSTGRES_PASSWORD: pass123
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
app:
build: .
environment:
DATABASE_URL: postgresql://postgres:pass123@postgres:5432/personal?schema=public (NOW YOU CAN SET THE NAME OF THE POSTGRES SERVICES AND IT WORKS)
depends_on:
- postgres
ports:
- "8080:8080"
Obs: Now the Dockerfile will connect to the docker-compose service, BUT you need to run your Dockerfile image under the docker-compose to work, if you run only postgres on docker-compose and run the Dockerfile with docker run IMAGE_NAME wont work
Upvotes: 1
Reputation: 21
I've solved the problem with this steps: (I'm still accepting other more practical answers to solve this problem)
RUN -> docker container ls
COPY: name of the postgres container
RUN -> docker inspect \ -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_NAME
COPY: id of the postgres container
CHANGE:
DATABASE_URL="postgresql://postgres:pass123@localhost:5432/personal?schema=public"
(localhost:5432)
to
DATABASE_URL="postgresql://postgres:[email protected]:5432/personal?schema=public"
(CONTAINER_IP:5432)
Obs: I've tried to set DATABASE_URL="postgresql://postgres:pass123@postgres:5432/personal?schema=public" (SERVICE_NAME_ON_DOCKER_COMPOSE:5432) but doens't worked
Upvotes: 0