2016-08-19 31 views
1

既存のSQL ServerでASP NETコアアプリケーションを作成しました。DockerコンテナからSQL Serverに接続できません

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

これはウィンドウで機能します。同じ設定のドッカーイメージを作成した後、dbサーバーに接続できません。

エラー:

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 --- 

我々は

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

/etc/freetds/freetds.conf FreeTDSのコンフィギュレーションを作成していると我々はそれがsqshを使用してDBに接続することができました。

アイデア?

編集

マイ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の例外がスローされます。

+0

質問タイトルにタグをつけないでください。 – Tseng

+0

FreeTDSはASP.NETとは関係ありません。メッセージには、間違ったサーバー名やファイアウォールルールのためにサーバーにアクセスすることさえできないことが明確に示されています。サーバーにpingすることもできますか? Dockerはデフォルトでアウトバウンド接続を許可していません。許可するものを指定する必要があります –

+0

はいそのサーバーにpingできます。私はdocker runコマンドでホストを追加しています。私はFreeTDSを使ってチェックしたこのdockerイメージから、インスタンス名、ユーザー、パスワードでサーバーにアクセスできます。 – bizon

答えて

2

thisによれば、新しいバージョンの.netコアを待つか、SQL Serverのデフォルトインスタンスをインストールする必要があります。

関連する問題