I wrote a simple ASP.NET Core 8 Web API. It must connect to SQL Server Express. If I run it with HTTPS it works fine and connects to the database. If I run it with Docker or Docker compose, it works but does not connect to the database.
Here it is my Dockerfile:
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Euro_WeatherForecast.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Euro_WeatherForecast.dll"]
And my docker-compose.yaml:
version: '3.4'
services:
euro_weatherforecast:
image: ${DOCKER_REGISTRY-}euroweatherforecast
build:
context: .
dockerfile: Euro_WeatherForecast/Dockerfile
This is my connection string:
"ConnectionStrings": {
"AppDbContext": "server=sqlserver;port=8080;Data Source=TUF1\\SQLEXPRESS;Initial Catalog=Euronext_WeatherForecast;Integrated Security=True;Encrypt=False"
The container runs in docker, the service SQL Server and SQL Server browser are running. Firewall is deactivated.
Your question is not very explicit about it but if the SQL server is running on the same machine.
One solution might be to use host.docker.internal as the hostname to refer to the host machine from within a Docker container (https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host)
Giving something like :
Some other points to consider :
Integrated Securityproperty use Windows Authentication, but in a docker container it might not work as expected. In this case specifying a username and password directly in the connection string could make the deal.