I built a simple RestAPI using node.js and Prisma as an ORM and created a Dockerfile
for that application, then i used docker-compose
to make these services:-
web
-> this is the application service, built using the Dockerfile
mentioned above.pgdb
-> a postgresql container built using the PostgreSQL official docker image.when i try to run docker-compose up
it fails to build the web
service because Prisma can't run it's migrations due to it's inability to connect to the database that's running in the other service, namely pgdb
.
Error: P1001: Can't reach database server at
pgdb:5432
.
Please make sure your database server is running atpgdb:5432
.
note: i used pgdb
(the name of the PostgreSQL service) here to reference the host.
it should be able to use pgdb
as a host reference and successfully connects to the database server located at pgdb:5432
The Dockerfile
referenced inside the docker-compose
to create the web
service
FROM node:16
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
# Run Prisma migrations.
RUN npx prisma migrate dev --name init
# Compile typescript code to javascript.
RUN npx tsc
# Run the compiled code (compiled files are outputed to `dist` folder).
CMD [ "node", "dist/index.js" ]
The docker-compose.yml
version: "3.9"
services:
webapp:
build: .
ports:
- "5000:5000"
depends_on:
- pgdb
pgdb:
image: postgres:14
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=mydb
The .env
(containing Prisma DB connection string)
DATABASE_URL="postgresql://postgres:postgres@pgdb:5432/mydb?schema=public"
v16.17.0
^4.4.0
# Run Prisma migrations. RUN npx prisma migrate dev --name init
it doesn't make sense to run migrations at docker image build time. The migrations should be run within the entrypoint of the image, not when its being built.
Then when the container is deployed, the migrations run before the application is started in each environment in which it's deployed.