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

2ic8powd  于 2023-05-28  发布在  SQL Server
关注(0)|答案(2)|浏览(138)

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?

wwwo4jvm

wwwo4jvm1#

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.

lrl1mhuk

lrl1mhuk2#

Using docker composer:

version: "3"

services:
  mssql:
    image: mcr.microsoft.com/azure-sql-edge
    environment:
      - ACCEPT_EULA=1
      - MSSQL_SA_PASSWORD=changeMe1!
    networks:
      - mynet
    cap_add:
      - NET_ADMIN
    ports:
      - "1433:1433" 

networks:
  mynet:

You can now access the database from localhost:1433 .

相关问题