sqlcmd command and PHP can't connect to SQL Server instance running in a Docker container on a Mac

167 views Asked by At

I'm trying to connect to a SQL Server instance running in a Docker container on my Mac using sqlcmd, but I'm getting an "I/O timeout" error. Here's what I've tried so far:

I ran the following command to start the container:

docker run --name sql -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Password123' -p 1433:1433 -d mcr.microsoft.com/azure-sql-edge

I ran the following command to get the container's IP address:

docker inspect sql | grep IPAddress,

which returned: "IPAddress": "172.17.0.2"

I tried to connect to the SQL Server instance using the following command: sqlcmd -S tcp:172.17.0.2,1433 -U sa, but I got the following error message: unable to open tcp connection with host '172.17.0.2:1433': dial tcp 172.17.0.2:1433: i/o timeout

Here's my PHP code:

<?php
$serverName = "172.17.0.2,1433";
$connectionOptions = array(
    "Database" => "inart",
    "Uid" => "SA",
    "PWD" => "Password123"
);

// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);

// Checks the connection
if (!$conn) {
    echo "Error: Unable to connect to SQL Server." . PHP_EOL;
    echo "Debugging error: " . print_r(sqlsrv_errors(), true) . PHP_EOL;
    exit;
}

echo "Success: A proper connection to SQL Server was made!" . PHP_EOL;

// Closes the connection
sqlsrv_close($conn);
?>

When I run this code, it says "Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in /a.php:10 Stack trace: #0 {main} thrown in /a.php on line 10"

0

There are 0 answers