I built a ASP.NET Core 2.1 Web API using Entity Framework. Now I need to deploy that WebAPI to one of our servers. The api works perfectly in Visual Studio, with access to the database hosted on another server. The database server has a static IP that is only accessible to the machines inside the network.
The server where I have to deploy has the following configuration:
This is my docker file
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app
EXPOSE 1433
EXPOSE 8080
# 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.1-sdk
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "myApp.dll"]
When building the Image I use the following command:
Docker build -t myApp .
An when I run the container
docker run -d -p 8080:80 --name api myapp
I also tried the following command, to no avail:
docker run -d -p --net:host --name api myapp
Now then, the connection to the database is made via an environment variable named API_DBCON. I add this environment variable from Kitematic.
Whenever I start the container I get the following error message:
Microsoft.EntityFrameworkCore.Database.Connection[20004]
An error occurred using the connection to database 'myDatabase' on server '172.17.16.119,1433'.
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: 40 - Could not open a connection to SQL Server)at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
I'm new to Docker and ASP.NET, so could anyone point to me what I might be doing wrong or give me a lead so I could investigate by myself.
User contributions licensed under CC BY-SA 3.0