10

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

4
  • You wrote all this stuff but did not write the error message. Please edit your question and add the error message. Commented Apr 6, 2020 at 13:08
  • If I recall correctly, you need to use the host IP, not the internal IP. learn.microsoft.com/en-us/sql/linux/… Commented Apr 6, 2020 at 13:15
  • sqlcmd is being run from the context of the container, where as you're running VS Code (or Azure Data Studio) from the host machine. Commented Apr 6, 2020 at 13:18
  • @Nick.McDermaid I included the error info, but you're absolutely right - I need top specify localhost (or the local IP) rather than the IP of the container. Thanks! Commented Apr 6, 2020 at 13:31

4 Answers 4

13

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.

Sign up to request clarification or add additional context in comments.

Comments

3

BlockquoteFor 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. Blockquote

Don't do that, ping host.docker.internal and use the IP it displays or use host.docker.internal instead of the IP. Include the port with it so it's

host.docker.internal,1433

Comments

1

You can try to check the configuration of mssql.connections in setting.json to see if it contains "azureAccountToken": "" under profileName. If it does, please delete it and try to connect. This is effective for me, I hope it helps you.

Comments

0

For me the solution was to use the IP of localhost rather than the name. I.e. using 127.0.0.1 as the server name, rather than localhost

I presume this could be fixed with a host file entry on my host machine, but really - this works for now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.