Network issue while starting Docker container for dotnet core console application

0

I have created a simple console application in .NET Core with EF Core executing a SQL Server stored procedure from the server that's accessible only through a VPN connection. I was able to run the application on its own, but the container always exists throwing network error when I start it.

URL referred to implement container: https://docs.microsoft.com/en-us/dotnet/core/docker/build-container?tabs=windows

Context: the password has special characters as well

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlServer(
@"Data Source=###.##.#.##\sql_dev,-#####;Initial Catalog=DBNAME;Enlist=True;User ID=UID;Password=PWD;");
}

I identified the IP and port using the following:

SELECT
   CONNECTIONPROPERTY('local_net_address') AS local_net_address,
   CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port

Command executing the procedure:

context.Database.ExecuteSqlCommand("EXEC dbo.spToBExecuted @Param='Value'");

I was able to successfully create the container but was not able to start the container, it exits with the following error message:

Please help me solve the issue. (provider: TCP Provider, error: 25 - Connection string is not valid), also need confirmation if the VPN has anything to do with this.

> Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 25 - Connection string is not valid)
>
> at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool, SqlAuthenticationProviderManager sqlAuthProviderManager)  
> at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)  
> at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)  
> at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)  
> at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)  
> at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)  
> at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)  
> at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
    at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
    at Microsoft.Data.SqlClient.SqlConnection.Open()
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
    at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommand(DatabaseFacade databaseFacade, RawSqlString sql, IEnumerable`1 parameters)
    at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommand(DatabaseFacade databaseFacade, RawSqlString sql, Object[] parameters)
    at NetCore.Docker.Program.Main(String[] args) in C:\Users\UName\Documents\projects\App\Program.cs:line 16
    
> ClientConnectionId:00000000-0000-0000-0000-000000000000

Please help me solve the issue: (provider: TCP Provider, error: 25 - Connection string is not valid), Also need confirmation if the VPN has anything to do with this.

sql-server
docker
.net-core
connection-string
docker-container
asked on Stack Overflow Jul 18, 2020 by RandomUser • edited Jul 27, 2020 by Ziaullah Khan

2 Answers

0

You're using a verbatim string literal for your connection string so you don't need to escape your backslash characters.

Try using the following instead (note the single backslash):

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlServer(
@"Data Source=###.##.#.##\sql_dev,-#####;Initial Catalog=DBNAME;Enlist=True;User ID=UID;Password=PWD;");
}
answered on Stack Overflow Jul 18, 2020 by AlwaysLearning
0

I managed to successfully start the container after switching to Windows container.

URL Referred: https://docs.microsoft.com/en-us/virtualization/windowscontainers/quick-start/set-up-environment?tabs=Windows-10-Client

answered on Stack Overflow Jul 19, 2020 by RandomUser

User contributions licensed under CC BY-SA 3.0