title: Docker container can't connect to SQL Server on a remote server
body: <p>I have a container hosted ASP.NET Core application but it can't connect to SQL Server on a remote server.</p>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>This is what I already checked:</p>
  
  <ul>
  <li>Try to disable firewall on host machine and DB server. : still get the error</li>
  <li>Edit connection string to use IP and port : still get the error</li>
  <li>Ping from application container to DB server: I can ping to DB server normally</li>
  <li>Connect to database server via SQL Server Management Studio: I can connect normally</li>
  </ul>
  
  <p>So I think that the container can see the DB server but can't connect. What's the other thing should I check?</p>
  
  <p>Thank you very much for your help.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS base
  WORKDIR /app
  EXPOSE 80
  EXPOSE 1433
  
  #our sql server was use this port for connect
  EXPOSE 64608
  
  FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
  WORKDIR /src
  
  COPY Web.API.sln ./
  COPY MyProject.Core/*.csproj ./MyProject.Core/
  COPY MyProject.API/*.csproj ./MyProject.API/
  
  RUN dotnet restore
  COPY . .
  
  WORKDIR /src/MyProject.API
  RUN dotnet build -c Release -o /app
  
  FROM build AS publish
  RUN dotnet publish -c Release -o /app
  
  FROM base AS final
  WORKDIR /app
  COPY --from=publish /app .
  ENTRYPOINT ["dotnet", "MyProject.API.dll"]
  </code></pre>
  
  <p><strong>Run Docker Command:</strong></p>
  
  <pre><code>docker build -t myproject-api:latest .
  
  docker run -d -p 7991:80 --name myproject-api myproject-api:latest
  </code></pre>
  
  <p><strong>Connection String:</strong></p>
  
  <pre><code>"data source=172.16.0.88\\SQL_DEV,64608; initial catalog=MyProject; persist security info=True; user id=myuser; password=mypassword;"
  </code></pre>