sql-serverdockervisual-studio-code

Use VS Code (mssql extension) to connect to SQL Server running in Docker on Windows


I'm using Docker on Windows, and I have a Docker container with SQL Server running. To start my container, I used the information shown here:

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=yourStrong(!)Password" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-CU8-ubuntu

(Note, I'm using double qutes (") instead of single quotes (') as I'm running on Windows.)

My container is running and I can connect to it and query using sqlcmd:

docker exec -it <container_id|container_name> /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P <your_password>

(Note, I'm specifying the container ID found with docker container ls, and the password used when running the container, yourStrong(!)Password.)

1> USE master
2> GO
Changed database context to 'master'.
1> SELECT name, database_id, create_date FROM sys.databases;
2> GO
name                                                                                                                             database_id create_date
----------------------------------------
master         1 2003-04-08 09:13:36.390
tempdb         2 2020-04-06 12:00:33.493
model          3 2003-04-08 09:13:36.390
msdb           4 2018-06-13 18:27:29.220

However, I want to use VS Code to connect to my SQL server instance, so after installing the SQL SQL Server (mssql), I have tried to set up a connection, but it fails to connect.

Server Name

enter image description here

For the server name, I am using the IP address for the container, which is 172.17.0.2. This is obtained using the answer shown here.

Database Name

enter image description here

For the database name, I'm using master.

Username

enter image description here

For the username, I'm using SA (note that I've tried with both upper and lower case).

Password

enter image description here

I'm using the same password specified when running the container, yourStrong(!)Password

The last two steps are to "Save Password", for which I select "Yes", and to name the connection, for which I've tried specifying a name, and tried omitting it.

The connection fails every time, and I'm not sure what the problem is. The credentials are definitely correct (unless they are somehow transformed when added into the prompts in VS Code, which is unlikely), the server name (IP) is correct, and the database exists.

Does anyone know what the problem might be, or how I can troubleshoot this further?

I know this should work, as I have successfully done the exact same thing on Linux. I am guessing this has something to do with Docker running on Windows, but cant be sure.

Any help appreciated!

Edit

The error information when trying to connect: enter image description here


Solution

  • Try using the syntax <ip>,<port> when vscode asks for host.

    Eg. If the server is listening on port 1433 on your local machine, you should use localhost,1433.