Very slow queries between server and database in docker

Viewed 3010

I'm trying to setup some integrations tests with docker compose. I'm using docker compose to spin up a postgres database and a nodejs server. I'm then using jest to run http requests against the server.

For some reasons that I can't explain all SQL queries (even the simplest one) are extremely slow (+ 1s).

It sounds like a communication problem between the two containers, but I can't spot it. Am I doing something wrong?

Here's my docker-compose.yml file. The server is just a simple express app

version: "3.9"
services:
  database:
    image: postgres:12
    env_file: .env
    volumes:
      - ./db-data:/var/lib/postgresql/data
    healthcheck:
      test: pg_isready -U test_user -d test_database
      interval: 1s
      timeout: 10s
      retries: 3
      start_period: 0s
  server:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      database:
        condition: service_healthy
    env_file: .env
    environment:
      POSTGRES_HOST: database
      NODE_ENV: test
    init: true
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/healthcheck"]
      interval: 1s
      timeout: 10s
      retries: 3
      start_period: 0s

EDIT I'm using

Docker version 20.10.2, build 2291f61
macOs BigSur 11.1 (20C69)
2 Answers

Try using Volume instead of Bind Mount for the data folder by changing this:

- ./db-data:/var/lib/postgresql/data

To this:

- db-data:/var/lib/postgresql/data

And adding this section to the end of the compose file:

volumes: 
    db-data:

You can read more about bind mounts vs volumes here

I found it. The issue is not in docker, but in the compiler. I'm using ncc and it breaks my postgres client.

I opened an issue in their repo with a minimal reproducible example

https://github.com/vercel/ncc/issues/646

Thanks a lot for your help

Related