6

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)
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
  • 1
    1. Can you make a test for different versions of postgresql? E.g. try postgres:13 or postgres:11 just for test. 2. You mention node.js: which version it is? We had problems with 14 + postgresql. Can you try with 12? 3. Queries are slow only for queries from node.js or from everywhere? – Alex Yu Jan 25 '21 at 07:19
  • @AlexYu I found the issue and it was not in docker but in the postgres client. Thanks for the help – ThomasThiebaud Jan 25 '21 at 08:01

2 Answers2

8

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

eitann
  • 1,203
  • 10
  • 23
  • 1
    (If this is in fact the issue – and it likely may be – see also [Docker in MacOs is very slow](https://stackoverflow.com/questions/55951014/docker-in-macos-is-very-slow) for other workarounds.) – David Maze Jan 24 '21 at 22:54
  • Thanks for the answer, but that does not solve my issue. I also tried to add `:delegated` as pointed in the link but it does not work either – ThomasThiebaud Jan 25 '21 at 06:10
  • 1
    This made a lot of difference for me, thanks a lot! – Raibaz Feb 16 '22 at 16:08
1

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

ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
  • Hm. Interesting. We had problems with combination of plain `pg` + node 12 (but we don't use ncc). Actually as `slonik` is built upon `pg` so it's possible that even combination `ncc`+`pg` will display the same problem – Alex Yu Jan 25 '21 at 09:28