1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[SQL] Connecting to local MS SQL Server from a docker container on Ubuntu 18.04

Discussão em 'Outras Linguagens' iniciado por Stack, Setembro 13, 2021.

  1. Stack

    Stack Membro Participativo

    I have a VPS running Ubuntu 18.04. The server is running an ASP.NET Core application in a docker container and SQL Server Express 2019 is NOT in a container. I am trying to access a database from an application. My connection string:

    Server=host.docker.internal,1433;User ID=SA;Password=mypass;Database=mydb


    In application

    services.AddDbContext<UniversityContext>(options =>
    options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("ConnectionString")));


    I get an error

    SqlException: 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: 35 - An internal exception was caught)


    Running a container:

    docker run -d -p 228:80/tcp myrepos/myproject


    ASP.NET Core application successfully connecting to SQL Server from my PC

    Also, I tried

    docker run -d -p 228:80/tcp myrepos/myproject --network="host"


    and 127.0.0.1 or localhost instead of host.docker.internal and get similar error but with another code

    SqlException: 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: 40 - Could not open a connection to SQL Server)


    UPDATED

    If I'm not mistaken SQL Server is already listening 1433 [​IMG]

    Continue reading...

Compartilhe esta Página