I have a docker container with a .NET app with the following code:
var pgsql = OpenDbConnection("Server=localhost;Database=postgres;Username=postgres;Password=MyPass;Port=5432");
private static NpgsqlConnection OpenDbConnection(string connectionString)
{
NpgsqlConnection connection;
Console.Error.WriteLine("Connecting to DB");
while (true)
{
try
{
connection = new NpgsqlConnection(connectionString);
connection.Open();
break;
}
catch (SocketException e)
{
Console.WriteLine("{0} Exception caught.", e);
Thread.Sleep(1000);
}
catch (DbException)
{
Console.Error.WriteLine("Waiting for db - db error");
Thread.Sleep(1000);
}
}
return connection;
}
I have a different container with the official Postgres container, that I started with command
docker run --name localpg -e POSTGRES_PASSWORD=MyPass -d postgres -p 5432:5432
Now I can connect from pgAdmin with host localhost, user&password postgres and password MyPass.
However, if I try to run the .NET code above from the same computer with command docker run worker, I get this exception:
System.Net.Sockets.SocketException: Unknown error -1
at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout)
at Npgsql.ConnectorPool.Allocate(NpgsqlConnection conn, NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnection.OpenInternal()
at Worker.Program.OpenDbConnection(String connectionString) Exception caught.
Any thoughts?
EDIT
Following Dan's recommendation I changed the connection string to
var pgsql = OpenDbConnection("Server=localpg;Database=postgres;Username=postgres;Password=MyPass;Port=5432");
but still doesn't work (InternalSocketException: No such device or address).
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
ad8fa68b5dab bridge bridge local
984027aadc2a host host local
5604001734fa none null local
Starting app container with docker run worker (no compose used).