1

I'm creating a docker-compose file to prepare an e2e testing environment, but I'm experiencing some issues when starting up my PostgreSQL server for testing.

docker-compose.my-app.e2e.yml:

version: "3.3"

services:

  my-app-e2e-db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: my-app-e2e-user
      POSTGRES_PASSWORD: ${MY_APP_PG_PASS}
      POSTGRES_DB: my-app
      
  my-app-e2e-seeder:
    build:
      context: ..
      dockerfile: .deploy/my-app-seeder.Dockerfile
    depends_on:
      - my-app-e2e-db

The problem is that my seeder script fails to connect to the PostgreSQL server, and when I look into it the PostgreSQL server is not running. The odd thing is that if remove my seeder script from the docker-compose and run it, the PostgreSQL server seems to run smoothly.

version: "3.3"

services:

  my-app-e2e-db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: my-app-e2e-user
      POSTGRES_PASSWORD: ${MY_APP_PG_PASS}
      POSTGRES_DB: my-app
      

I'm running the compose file with docker-compose --env-file=.deploy/my-app.e2e.env -f .deploy/docker-compose.my-app.e2e.yml up -d --build.

Thanks!

UPDATE!

It turned out I was attempting to access the database in a RUN step inside my Dockerfile. I moved that action to the CMD step instead.

iMe
  • 444
  • 6
  • 15
  • The database container often needs 30-60 seconds to initialize. `depends_on:` only ensures the container exists, not that it's fully functional. Do you need something like the techniques described in [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y)? If you run `docker-compose up` without the `-d` option, is the database still running at the end of it; do you see database startup messages after the seeder script fails? – David Maze Feb 16 '22 at 11:52
  • https://stackoverflow.com/a/71060305/1184717 – Mr. Feb 16 '22 at 12:33
  • @DavidMaze thanks for your input! I followed your link, and ended up trying to add `healthcheck` and `condition` as this guy is doing here: https://github.com/peter-evans/docker-compose-healthcheck/blob/master/docker-compose.yml, but no luck. If I run without the `-d` flag, I still see nothing running after the fail :/. – iMe Feb 16 '22 at 12:51
  • @Mr. I'd have to guess, but if your point is that `depends_on` is deprecated, I don't think that it's true, as I get no warnings when I try to use it, and it's still mentioned under the v3 spec as far as I can see (https://docs.docker.com/compose/compose-file/compose-file-v3/). The only note I can see about it is if you run in swarm mode. – iMe Feb 16 '22 at 12:54
  • it id not deprecated, but does not give you the "wait" that you are after. see the list of tools in the answer – Mr. Feb 16 '22 at 13:00
  • @Mr. Thanks for your input ! I can't come up with at working solution based on that unfortunately. It seems like the (mis)information goes in all directions--some sources says to use `wait-for-it` scripts (which BTW feels "clunky" IMO), some sources says wait-for-it is "the old way" and to use `pg_isready` in the `healthcheck`, some claim that `depends_on` is even deprecated, while the spec says it's not, and the list goes on . – iMe Feb 16 '22 at 13:52

0 Answers0