Jose
Jose

Reputation: 51

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'
    

Upvotes: 4

Views: 3171

Answers (3)

Harry Developer
Harry Developer

Reputation: 522

I had the same issue when using a gateway and a microservice:

  • Create network:

docker network create mynet

  • Add containers:
docker network connect mynet my-gateway
docker network connect mynet my_service_8080

Doing so, I wasn't able to communicate between both containers when using container name. E.g. when connecting to the gateway container and doing a curl to the service:

curl -i http://my_service_8080:8080/management/info

HTTP/1.1 400
Transfer-Encoding: chunked
Date: Tue, 23 Feb 2021 12:51:47 GMT
Connection: close

After doing a lot of research on the net which led to nothing, I finally had the idea to provide an alias name:

docker network connect --alias gateway mynet my-gateway
docker network connect --alias myservice mynet my_service_8080

Now, I can successfully communicate between the containers by using alias name as host name:

curl -i http://myservice:8080/management/info

HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 23 Feb 2021 12:54:36 GMT

{"name":"myservice-app","description":"my service","version":"20210202"}

EDIT: After some more digging, I found that communication using container names doesn't work if the name (or alias) contains an underline character. So if you experience problems, check your container names.

Upvotes: 0

Jose
Jose

Reputation: 51

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'
    

Upvotes: 1

Bjoern Rennhak
Bjoern Rennhak

Reputation: 6936

The TestDBServer part of your sqlcmd references a so-called Server Alias. These server aliases are not accessible due to the way you have set up the Docker networking currently. If you switch to Docker compose to set up the networking for you it should work.

An alternative approach would be to --link the containers in question together.

Upvotes: 0

Related Questions