I have a problem with creating a Docker Image for a NestJS application talking via Prisma to a Postgress database already running in another Container. The problem is that is can't reach the database during the 'prisma generate' phase in the Docker Build.
That's the short version. :-)
docker-compose Database
First, the docker-compose of the database which is running fine after a 'docker-compose up -d':
version: '3.9'
services:
db:
image: postgres:latest
restart: "no"
container_name: hwpostgres
volumes:
- ./database:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: root
POSTGRES_USER: root
POSTGRES_DB: taskDb
networks:
default:
external:
name: my-network
docker-compose of the API
There is another docker-compose file building the NestJS API application:
ersion: '3.9'
services:
api:
build:
context: ./build
dockerfile: Dockerfile_api
image: hwapi
restart: "no"
container_name: hwapi
environment:
DATABASE_URL: postgresql://root:root@hwpostgres:5432/tasksDb?schema=public
ports:
- "8080:3001"
command: ["node", "dist/main.js"]
networks:
default:
external:
name: cops-net
Dockerfile_api
The Dockerfile_api looks like this:
FROM node:latest As development
ARG DATABASE_URL=postgresql://root:root@hwpostgres:5432/tasksDb?schema=public
ENV DATABASE_URL $DATABASE_URL
WORKDIR /usr/src/app
COPY . .
RUN npm install -g npm@7.6.3
RUN npm install
RUN npx prisma migrate dev --name init --preview-feature
RUN npm run build
This Dockerfile_api shown here is obviously the first stage of a multi-part Dockerfile, but the second part is not interesting for this problem description.
The problem is that the 'npx prisma migrate' command fails because it can't find the database. Output of that section of the build process:
Step 8/9 : RUN npx prisma migrate dev --name init --preview-feature
---> Running in 938d6538806a
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "tasksDb", schema "public" at "hwpostgres:5432"
Error: P1001: Can't reach database server at `hwpostgres`:`5432`
Please make sure your database server is running at `hwpostgres`:`5432`.
So, it says there's something wrong with the database
Same thing interactively
When I change the Dockerfile_api to the following:
FROM node:latest As development
ARG DATABASE_URL=postgresql://root:root@hwpostgres:5432/tasksDb?schema=public
ENV DATABASE_URL $DATABASE_URL
WORKDIR /usr/src/app
COPY . .
RUN npm install -g npm@7.6.3
RUN npm install
# RUN npx prisma migrate dev --name init --preview-feature
# RUN npm run build
And change the command in the docker-compose.yml for this Dockerfile to
command: ["sleep", "3650d"]
Then the container keeps on running after finishing the Build.
And then I go into the created Docker Container (docker exec -it hwapi /bin/bash), and then do the 'npm prisma migrate' command, it all works !!
Output of that:
/usr/src/app# echo $DATABASE_URL
postgresql://root:root@hwpostgres:5432/tasksDb?schema=public
/usr/src/app# npx prisma migrate dev --name init --preview-feature
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "tasksDb", schema "public" at "hwpostgres:5432"
Already in sync, no schema change or pending migration was found.
/usr/src/app#
So, here it looks like it is able to find the Container running the database.
Why is it that it can't find the database during the Build phase and it can find the Database when I do the Prisma Migrate in the interactive version of the same Container?