5

I have 2 Dockers: my ASP.NET Core Web server -p 5001:80 postgresql -p 5451:5432 When I configure my Web Server to work with postgresql running on my host it works. But when I run configure myWeb App to work with postgresql in Docker , run http://localhost:5001 it

starts but then an error appears:

    warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
          Failed to determine the https port for redirect.
    fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
          An unhandled exception has occurred while executing the request.
    System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
     ---> Npgsql.NpgsqlException (0x80004005): Exception while connecting
     ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (99): Cannot assign requested address [::1]:5451

If I connect the app to an external non-dockerized PostgreSQL - it works fine.

What is incorrect and how to fix it?

There is my docker-compose file

https://pastebin.com/b8FbHSLL

3
  • How are you running the containers? docker-compose or docker run? Please add the commands you use to run the containers Commented Jul 28, 2020 at 11:44
  • you should use container port 5432 instead of 5451 publish port using docker-compose network Commented Jul 28, 2020 at 11:45
  • @ChristianFosli I run postgres docker in the docker-compse: it contains postgresl, elastic, kibana. My web app I run : >docker run -it --rm -p5901:5901 pman . Also, I have included it to the same compose. But the app has a PG connection string : localhost. I have replace it with 0.0.0.0 0 both give the error. Commented Jul 28, 2020 at 12:17

1 Answer 1

13

So, localhost here refers to the locahost of the container which runs the webserver, not your localhost. Therefore you can't use localhost to refer to another container, without doing some networking-related things first.

There are several ways to proceed. Since you mention in the comment you're using docker-compose, I would advise the following:

With docker-compose, networking is relatively simple, if all the services that need to communicate with each other are included in the docker-compose.yml file, you run all of them with docker-compose up. If you haven't specified any specific network in the docker-compose file, docker-compose sets up a single network for all the included services, which makes it possible for each container to reach the other ones, by using a hostname identical to the container name.

Basically, you can then replace localhost with the service-name of the service you want, i.e. if postgres is called "db" in your docker-compose file, you replace localhost:5451 with db:5432.

If you specify custom networks in your docker-compose file, then you have to make sure the web-server and postgres are using the same network.

If you need to run the webapp with docker run instead of docker-compose up, then you need to include a --network argument so that they use the same network.

More info here

Edit: Corrected port number. We now need to use the container port, not the host port, as mentioned by @Adiii in above comment.

Sign up to request clarification or add additional context in comments.

8 Comments

Thnx, I see the way, ok, will test and give a feedback
I did all what you recommended but missed something. Now I cannot call my dockerized app as before localhost:5001 . Christian could you please look a my docker-compose file? If yes I can upload it
It connects but error during the connection to PG pm_1 | ---> System.Net.Sockets.SocketException (111): Connection refused at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout) at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout) Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken) pm_1 | at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
Could you edit your question to include the docker-compose file?
Also, check the logs for the postgres container to verify it starts successfully, and maybe double-check that ports and connection strings/credentials are correct.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.