I am running a SQL server container on Ubuntu using the following command
sudo docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=MyPassword' \
-p 1433:1433 --name db \
-d microsoft/mssql-server-linux:2017-latest`
I have another container on the same machine running WebAPI Core application, everything work find if i specified the server ip in the connection string but if I replaced it with "localhost" or "." it fail to connect.
Anyone faced the same issue? I don't want to modify the connection string every time I run my application on a new machine.
Edit 1: I need the my database to be up and running during the build process to apply EntityFramework code first migrations, so I cannot just add the SQL Server as a dependency in docker-compose.yml
Edit 2 Here is my docker-compose.yml
version: '3'
services:
webapi:
image: webapi
build:
context: ./
dockerfile: ./WebAPI/Dockerfile
args:
- connString=Server=db;Database...;
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:80
- conneString="Server=db;Database..."
ports:
- 50695:80
depends_on:
- db
db:
image: "microsoft/mssql-server-linux:2017-latest"
container_name: db
networks:
default:
external:
name: nat
And my docker file
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
ARG connString
ENV connString "$connString"
WORKDIR /src
COPY *.sln ./
COPY WebAPI/WebAPI.csproj WebAPI/
RUN dotnet restore
COPY . .
WORKDIR /src/Repository
RUN dotnet restore
RUN dotnet ef database update
WORKDIR /src/WebAPI
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", "WebAPI.dll"]