0

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"]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Islam El-Khayat
  • 406
  • 5
  • 14

2 Answers2

0

First off, you generally DO want to make your connection string easily configurable (like perhaps an environment variable), as it would make sense that your application running on different machines might connect to a different database server as well.

To the point of your docker question, you might want to look into using docker compose instead of just raw docker. Compose sets up hostname aliases for all of the containers within your cluster, so you would just be able to use a hostname like db.

MrName
  • 2,363
  • 17
  • 31
  • I already set my connectionstring inside the docker-compose.yml environment. I think you can set the container hostname with `docker run --hostname=alias` right? I tried it but it didn't work, I will give docker-compose a try – Islam El-Khayat Jan 17 '18 at 16:09
  • I added my docker-compose file – Islam El-Khayat Jan 18 '18 at 13:57
0

Yes, you can use localhost to connect the database, which is better than IP address.

You need to link container to your database container.

If you are using docker-compose then follow example

 container1:
     build: image_name
     links:
      - mysql:mysql

container2:
     build: image_name
     links:
      - mysql:mysql

mysql:  
 build: mysql

In above example you will see, I have link mysql container to every other container, by this you don't need to specify IP address.

if you are not using docker-compose then in your docker run command please use

--link sql_container_name:sql_container_name 

Hope this will help you, let me know in case any issue with this?

Rohan J Mohite
  • 2,283
  • 10
  • 19
  • The problem with this approach that the database is not available during `docker-compose build` and since I use EF core code first, I need to run the migration scripts during build process – Islam El-Khayat Jan 17 '18 at 16:22
  • why database won't available? docker-compose will handle this. or try with "--link" option. – Rohan J Mohite Jan 17 '18 at 16:32