Search code examples
sql-serverlinuxdockersqlcmd

Docker Container: Unable to connect to SQL Server by name


I created two container on the same network and one of them as a Sql Server instance running. In the other container (with SQL Tools) i'm able to connect to the SQL using the IP Address, but if i swith to machine name it fails.

I already tried do ping the machine and the dns is solving the right IP, i also tried dnslookup and it's also working. Does anyone as a clue on how to fix this?

Full test scenario:

  1. Created new network

    docker network create --driver=bridge specsnet
    
  2. Run SQL Container

    docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Password!123456' -p 1401:1433 -d --name=TestDBServer --net=specsnet --rm microsoft/mssql-server-linux:latest
    
  3. Run New Container with SQL Tools (to test connection)

    docker run -it --net=specsnet --rm --entrypoint "bash" mcr.microsoft.com/mssql-tools:latest
    
  4. Loaded some tools for troubleshooting (into SQL Tools container)

    apt-get update | apt-get install telnet -y | apt-get install iputils-ping -y | apt-get install dnsutils -y | apt-get install nmap -y | apt-get install nano -y
    
  5. Test Connection with IP (Success - IP was 172.18.0.2)

    sqlcmd -S tcp:172.18.0.2,1433 -U sa -P 'Password!123456'
    
  6. Test Connection with Name (Fails)

    sqlcmd -S tcp:TestDBServer,1433 -U sa -P 'Password!123456'
    

Solution

  • So as Bjoern suggested i created a docker compose file and after doing some test i realized the issue was not fixed.

    Then i started to manipulate the file, tweaking the properties, and discovered the problem was on the SQL container name (the container name had upper case letters). I set the SQL container name to "testdbserver" and everything worked fine.

    1. Docker Compose File

      version: '2'
      services:
        testdbserver:
          image: microsoft/mssql-server-linux:latest
          ports:
            ["1401:1433"]
          environment:
            SA_PASSWORD: Password!123456
            ACCEPT_EULA: Y
          networks:
            - specsnet
      
        sqltools:
          image: mcr.microsoft.com/mssql-tools:latest
          depends_on:
            - testdbserver
          networks:
            - specsnet
      
      networks:
        specsnet:
          driver: bridge
          ipam:
           config:
             - subnet: 10.5.0.0/16
               gateway: 10.5.0.1
      
    2. Start SQL Tools Container on Bash Mode

      docker-compose run sqltools bash
      
    3. Execute SQL Test Connection (works now)

      sqlcmd -S tcp:testdbserver,1433 -U sa -P 'Password!123456'