1

I have .NET Core console app running in docker and I want to connect to local MSSQL database (not dockerized!), but I'm unable to do so. Connecting to DB on remote server by IP works fine, but using connection string like Data Source=DESKTOP-9DRU731;Initial Catalog=DB;User id=user;password=password; gives me an error

System.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: 35 - An internal exception was caught) ---> System.AggregateException: One or more errors occurred. (No such device or address) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address

Is there any way to do this?

I have already tried adding expose in dockerfile and port mapping in docker compose for port 1433, as well as setting the network as host. My DB has remote access enabled and I am able to connect to it when I include host IP and port in connection string, but it is a requirement that connection string remains unchanged. I also tried using host.docker.internal, but again no luck. I think that some of it may fail due to environment which I'm using, which is:

  • Win 10 Home (which means using Docker Toolbox, not Docker for Windows)
  • Linux containers

I also tried using bridged network, but in that case I cannot even reach the part when I'm trying to connect to DB, as before that I'm trying to create Service Bus queue and it fails with socket exception.

My dockerfile:

FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:2.2-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "app.dll"]

And docker compose:

version: '3.4'

services:
  app:
    image: ${DOCKER_REGISTRY-}app
    build:
      context: .
      dockerfile: app/Dockerfile

EDIT

Thanks to @Mihai there's a possible solution. Adding

extra_hosts:
      - "DESKTOP-9DRU731:<your_host_ip>"

to Dockerfile works. Unfortunately I'm looking for more generic solution.

I'll add more context - I have nearly 100 connection strings stored in remote DB. They are pointing to databases specific for instances of an external app. This DBs are located on host or in host's private network and I need to connect to them using server name or IP from this private network.

2
  • have you tried with your host IP address as well instead of the name? Commented May 16, 2019 at 8:06
  • Yes, it works, but unfortunately I can't change the connection string. Well, technically I could, but I really want to avoid that. Commented May 16, 2019 at 8:11

1 Answer 1

2

Your docker-compose.yml:

version: '3.4'

services:
  app:
    image: ${DOCKER_REGISTRY-}app
    build:
      context: .
      dockerfile: app/Dockerfile
    extra_hosts:
      - "DESKTOP-9DRU731:<your_host_ip>"

Can you try this if it works?

If you move hosts then obviously this will top working. Also if the host changes IP this will stop working.

I recommend pulling these values from and .env file like this:

HOSTNAME=...
HOSTIP=....

And then your docker-compose.yml would become:

version: '3.4'

services:
  app:
    image: ${DOCKER_REGISTRY-}app
    build:
      context: .
      dockerfile: app/Dockerfile
    extra_hosts:
      - "$HOSTNAME:$HOSTIP"

This way also if you have more services and something changes you only have to update the .env file. Also the .env can be used by multiple docker-compose files.

Sign up to request clarification or add additional context in comments.

8 Comments

Yes, it works, thank you, but maybe there's more generic solution? I have whole bunch of connection strings like that, so including them all in dockerfile could be troublesome.
I'll add it to the solution
Unfortunately I don't think it'll work in my case. Connection strings are generated for every instance of an app and are stored in remote DB. There are lots of them (nearly 100), so manually managing IPs in file could be really troublesome.
And you can't write those values in a .env as well? Keep in mind that containers are really not meant to be dependent on the host so this here is a bit of a dirty trick... It will be difficult to make it a lot nicer than this in my opinion.
I've added more context to my question to fully describe the situation I'm trying to solve. I won't argue with you, maybe it's the only solution. :) But it's also quite more difficult than I expected, so I'll wait a bit, maybe something more simple will come up. Nethertheless thank you very much, I'm sure that even if I won't use it someone will find it useful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.