1

I've tried with both ENTRYPOINT and CMD to force my ASP.NET core application to wait for my postgres container to start up the database, but I'm still met with this exception when migration runs

pgsql.NpgsqlException (0x80004005): Exception while reading from stream

Here's what I tried:

CMD ["bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; dotnet API.dll"]
ENTRYPOINT ["bash -c 'while !</dev/tcp/db/5432; do sleep 1; done;", "dotnet", "API.dll"]

What am I doing wrong?

orpheus
  • 99
  • 12
  • I might be misunderstanding your use case but are you using Docker compose? If so, have you tried using the `depends_on` option to control the startup order? If you've got a multi-container environment, I'd recommend it looking into it. Link here https://docs.docker.com/compose/startup-order/ – jdewerth Dec 07 '20 at 20:22
  • @jandrew depends_on will only wait for the container to start. My problem is that the migration attempt begins before the postgres service starts up inside the container. – orpheus Dec 07 '20 at 20:32
  • Could you use a health-check to determine when the database is up and running? You could then use the health-check to know when the required services are ready and start the app container. You'd have to define the health-check of course. https://docs.docker.com/compose/compose-file/#healthcheck and the additional ref https://docs.docker.com/engine/reference/builder/#healthcheck – jdewerth Dec 07 '20 at 20:40
  • Provide your `docker-compose.yml` file if you have one. I can provide an example that might help you out, depends on what version you're running though. – jdewerth Dec 07 '20 at 21:28
  • One of the examples shown at https://docs.docker.com/compose/startup-order/ (linked by @jandrew) should do what you want; use `psql` on the app container to ensure the db is fully up before starting your .NET app. Your example bash while loop above is simply going to attempt a TCP connection, which is likely to hang while the PostgreSQL server waits for authentication. Use the psql method in the linked page instead to make sure that the server is fully up and can be authenticated to (you may need to install the `psql` command-line client in your app container). – Timshel Dec 08 '20 at 01:46

0 Answers0