简体   繁体   中英

How to connect to a SQL Server database inside a docker container?

I've created a docker container using the following command:

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStr@ngPassw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 -d microsoft/mssql-server-linux:latest

I'd like to use SSMS to connect to this database from my host machine. Which parameters should I use in the SSMS login dialog?

My current entries:

  • Server type: Database Engine
  • Server name: [machine-name]
  • Authentication: SQL Server authentication
  • Login: sa
  • Password: MyStr@ngPassw0rd

But I'm getting this error:

TITLE: Connect to Server

Cannot connect to localhost:.

ADDITIONAL INFORMATION:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)


network path not found

Seems I'm not able to 'see' the database.

Is there something wrong with my connection string or is it not possible to connect this way?

If you are using Docker CE on Windows with Linux containers, specify double-quotes around the environment variables so that they are recognized:

docker run -e "ACCEPT_EULA=Y" -e 'SA_PASSWORD=MyStr@ngPassw0rd" -e "MSSQL_PID=Express" -p 1433:1433 -d microsoft/mssql-server-linux:latest

You will then be able to connect using SSMS. With the default 1433 port in this case, you can specify . or localhost as the server name. You can also connect to the container instance from a remote host specifying only your machine name (assuming firewall rules allow incoming port 1433 traffic).

With single-quotes, the values are not recognized and the container will immediately stop. You can use docker logs <container-name> to view messages from the SQL Server error log for troubleshooting. Below is an example using an explict container name (which I recommend to facilitate tasks like this) using your original problem command:

docker run --name sql1 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStr@ngPassw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 -d microsoft/mssql-server-linux:latest
c3de002fc22106e53c9b165c7f444905b5ef1bb1596eddf1b097cdd6d60e6c75

docker logs sql1

The SQL Server End-User License Agreement (EULA) must be accepted before SQL Server can start. The license terms for this product can be downloaded from http://go.microsoft.com/fwlink/?LinkId=746388 .

You can accept the EULA by specifying the --accept-eula command line option, setting the ACCEPT_EULA environment variable, or using the mssql-conf tool.

As you can see from the logged message, the SQL instance stopped (stopping the container) because the SQL Server did not recognize the EULA was accepted due to the single quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM