1
我们已经使用现有的sql server创建了ASP NET Core App。无法从Docker容器连接到SQL Server
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核心这个图像包含了我们的测试目的和freetds的SQSH。我们的应用程序正常启动,它正在工作。当我们尝试连接到SQL它会抛出.net异常时发生问题。
请不要强迫标签进入问题标题 – Tseng
FreeTDS与ASP.NET无关。该消息明确指出,由于服务器名称或防火墙规则错误,您甚至无法访问服务器。你甚至可以ping服务器? Docker默认不允许出站连接,您需要指定允许的内容 –
是的,我可以ping该服务器。我使用docker run命令添加主机,并且可以使用FreeTDS检查的Docker镜像中的实例名称,用户名和密码到达服务器。 – bizon