簡體   English   中英

無法從Docker容器連接到SQL Server

[英]Cannot connect to SQL Server from a Docker container

我們已經使用現有的SQL Server創建了ASP NET Core App。

const string connectionString = @"Server=hostname\sqlexpress;Database=DEVDB;User Id=sa;Password=password;";
services.AddDbContext<RecruitmentToolsDatabaseContext>(options => options.UseSqlServer(connectionString));

它適用於Windows。 創建具有相同設置的Docker映像后,我們無法連接到數據庫服務器。

錯誤:

fail: Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[1]
    An exception occurred in the database while iterating the results of a query.
    System.Data.SqlClient.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) ---> System.AggregateException: One or more errors occurred. (No such device or address) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
        at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
        at System.Net.Dns.EndGetHostAddresses(IAsyncResult asyncResult)
        at System.Net.Dns.<>c.<GetHostAddressesAsync>b__14_1(IAsyncResult asyncResult)
        at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
    --- End of stack trace from previous location where exception was thrown ---

我們已經創建了freetds配置/etc/freetds/freetds.conf

[dev01]
host = hostname 
instance = sqlexpress
tds version = 8.0" 

並且我們能夠使用sqsh連接到該數據庫。

有任何想法嗎?

編輯

我的Dockerfile:

FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app

# Install apt packages
RUN apt-get update && apt-get install -y \
    freetds-bin \
    freetds-common \
    freetds-dev \
    sqsh

RUN touch /etc/freetds/freetds.conf
RUN echo "[dev01]" > /etc/freetds/freetds.conf
RUN echo "host = hostname" >> /etc/freetds/freetds.conf
RUN echo "instance = sqlexpress" >> /etc/freetds/freetds.conf
RUN echo "tds version = 8.0" >> /etc/freetds/freetds.conf

RUN touch /home/.sqshrc
RUN echo "\set username=sa" >  /home/.sqshrc
RUN echo "\set password=password" >>  /home/.sqshrc
RUN echo "\set style=vert" >>  /home/.sqshrc

RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]

EXPOSE 5000/tcp
ENV ASPNETCORE_URLS http://*:5000

ENTRYPOINT ["dotnet", "run"]
#CMD ["bash"] 

除了普通的ASP .NET Core之外,此圖像還包含freetds和sqsh,用於我們的測試目的。 我們的應用程序正常啟動,正在運行。 當我們嘗試連接到sql時,會發生問題,並拋出.net異常。

根據答案,您必須等待.net core的新版本或安裝sql server默認實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM