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
What’s the full error message?
Ah, hm.
Is your app also running in docker?
yeah, Im using the web api template with docker
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.
from my understanding Docker Desktop should configure the networking between containers automatically, but I'll check - google time, brb
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)
I did a and Im seeing both containers on it, Im assuming thats what you meant right ? (first time using docker desktop, used portainer a bit more)
It’s been a bit, but I believe bridge just lets the host talk to the containers.
It’s not inter-container.
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
My personal recommendation would be to learn docker compose.
It will do all this for you, in a nice declarative style.
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)
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:
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.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 !
I'd personally only build the image in VS, then use compose files to actually run it (outside of debugging in VS).
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)