Home > Software design >  docker-compose "depends_on" doesn't work, dependency service is never built
docker-compose "depends_on" doesn't work, dependency service is never built

Time:02-01

I'm using the following docker-compose.yml file to try to build a PostgreSQL container and a dependent web app container that uses prisma to connect to a postgres server.

version: '3.9'
services:
  app: 
    build: .
    depends_on:
      - db
    ports:
      - 4000:4000

  db:
    image: postgres
    ports: 
      - '5432:5432'
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: dev

when I run the command docker-compose --verbose up --force-recreate --remove-orphans it gives the output:

DEBU[0000] using default config store "/Users/<redacted>/.docker/buildx" 
[ ] Building 1.5s (13/13) FINISHED                                                                                                                                                               
 => [internal] load build definition from Dockerfile                                                                                                                                        0.0s
 => => transferring dockerfile: 32B                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                           0.0s
 => => transferring context: 34B                                                                                                                                                            0.0s
 => [internal] load metadata for docker.io/library/node:18.9                                                                                                                                0.5s
 => [1/9] FROM docker.io/library/node:18.9@sha256:d6ed353d022f6313aa7c3f3df69f3a216f1c9f8c3374502eb5e6c45088ce68e8                                                                          0.0s
 => [internal] load build context                                                                                                                                                           0.0s
 => => transferring context: 23.92kB                                                                                                                                                        0.0s
 => CACHED [2/9] RUN mkdir -p /usr/src/app                                                                                                                                                  0.0s
 => CACHED [3/9] WORKDIR /usr/src/app                                                                                                                                                       0.0s
 => CACHED [4/9] COPY package*.json .                                                                                                                                                       0.0s
 => CACHED [5/9] RUN npm install                                                                                                                                                            0.0s
 => CACHED [6/9] COPY . .                                                                                                                                                                   0.0s
 => CACHED [7/9] RUN echo "Got here"                                                                                                                                                        0.0s
 => CACHED [8/9] RUN npx prisma generate                                                                                                                                                    0.0s
 => ERROR [9/9] RUN npx prisma migrate deploy                                                                                                                                               0.9s
------                                                                                                                                                                                           
 > [9/9] RUN npx prisma migrate deploy:                                                                                                                                                          
#0 0.800 Environment variables loaded from .env                                                                                                                                                  
#0 0.800 Prisma schema loaded from prisma/schema.prisma
#0 0.820 Datasource "db": PostgreSQL database "dev", schema "public" at "db:5432"
#0 0.869 
#0 0.869 Error: P1001: Can't reach database server at `db`:`5432`
#0 0.869 
#0 0.869 Please make sure your database server is running at `db`:`5432`.
------
DEBU[0000] serving grpc connection                      
DEBU[0000] stopping session                              span="load buildkit capabilities"
DEBU[0000] serving grpc connection                      
DEBU[0001] stopping session                             
failed to solve: executor failed running [/bin/sh -c npx prisma migrate deploy]: exit code: 1

There is no evidence of docker-compose building the "db" service first like it should be because of "depends_on". Why is this?

Here is the app dockerfile

FROM node:18.9

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

COPY package*.json .

RUN npm install

COPY . .

RUN echo "Got here"

RUN npx prisma generate
RUN npx prisma migrate deploy

EXPOSE 4000
CMD [ "npm", "start"]

The following is defined in my .env DATABASE_URL="postgresql://postgres:postgres@db:5432/dev?schema=public"

I'm aware of healthchecks and making the app service wait until the db service reports healthy, I've tried this to no avail. I'm first tying to figure out why the db service isn't being built at all

CodePudding user response:

You're trying to connect to the DB during your image build. That's not going to work as you haven't started any containers at that point.

Run your prisma commands in the container. Some pre-scripts ought to get you moving...

package.json

"scripts": {
  "prisma:generate": "prisma generate",
  "prisma:migrate": "prisma migrate deploy",
  "prestart:docker": "npm run prisma:generate && npm run prisma:migrate",
  "start:docker": "...", // can be exactly the same as "start"
  "start": "..."
}

and in your Dockerfile

# ...
COPY . .
CMD [ "npm", "run", "start:docker" ]
  • Related