C
C#3mo ago
Dante

SQLServer Express in docker - can update DB, but cant connect

I have a SQLServer Express in a docker container and my small (still learning) app in VS 2022. Ive used Update-Database in PMC and it works fine. When I try to hit my simple GET route (built-in swagger) Im getting errors that "the server was not found" - implying it cant connect to SQL. Connection string seems fine since I can update/migrate DB. Any idea?
16 Replies
jcotton42
jcotton423mo ago
What’s the full error message?
Dante
DanteOP3mo ago
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: 40 - Could not open a connection to SQL Server)
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: 40 - Could not open a connection to SQL Server)
jcotton42
jcotton423mo ago
Ah, hm. Is your app also running in docker?
Dante
DanteOP3mo ago
yeah, Im using the web api template with docker
jcotton42
jcotton423mo ago
So, when your app is running in docker it needs to be wired into the same container network as sql server. Because localhost inside a container points to the container itself. Something like Docker compose can help here.
Dante
DanteOP3mo ago
from my understanding Docker Desktop should configure the networking between containers automatically, but I'll check - google time, brb
jcotton42
jcotton423mo ago
If you use compose, then yes. Otherwise, containers are, by design, isolated from each other. (You can also create a network manually, compose is just convenient)
Dante
DanteOP3mo ago
I did a
docker network inspect bridge
docker network inspect bridge
and Im seeing both containers on it, Im assuming thats what you meant right ? (first time using docker desktop, used portainer a bit more)
jcotton42
jcotton423mo ago
It’s been a bit, but I believe bridge just lets the host talk to the containers. It’s not inter-container.
Dante
DanteOP3mo ago
nice catch, that was the issue 😄 - created a new network - connected both containers to the test-network - changed conn string to use container IP instead of localhost - magic thanks a lot, I got very confused by the fact that PMC was not telling me anything
jcotton42
jcotton423mo ago
My personal recommendation would be to learn docker compose. It will do all this for you, in a nice declarative style.
Dante
DanteOP3mo ago
Im planning on doing that and setting it up on Portainer, but I was thinking of first developing the API and then moving it (it seemed easier)
Keswiik
Keswiik3mo ago
very late, but additionally you can resolve containers within the same network by name. For example, I have my own compose file with rabbitmq, postgres, and some of my own microservices. Something like:
services:
rabbitmq:
<stuff>
postgres-15:
<stuff>
some-microservice:
<more stuff>
services:
rabbitmq:
<stuff>
postgres-15:
<stuff>
some-microservice:
<more stuff>
Compose will place all of these containers on the same network, so I can connect to rabbitmq and postgres by using rabbitmq and postgres-15 as hostnames. You don't have to manually pull up container IPs and other stuff.
Dante
DanteOP3mo ago
Hmm ok so this would be like a starting point for both services, but Im not sure if it works with VS2022 build. Ill definitely give this a try when the app is finished, thanks !
Keswiik
Keswiik3mo ago
I'd personally only build the image in VS, then use compose files to actually run it (outside of debugging in VS).
Dante
DanteOP3mo ago
Yep, thats the plan for me as well (I'll learn something new since I havent done that before, but your reply just confirmed its doable)
Want results from more Discord servers?
Add your server