我正在Linux容器中运行SQL Server。当容器启动时,我希望进行一些初始化,并使用Powershell Core创建一个数据库。
我的Dockerfile如下所示:
FROM mcr.microsoft.com/mssql/server:2017-latest
ENV ACCEPT_EULA="Y" `
DATA_PATH="./data" `
sa_password="MyP@ssw0rd"
VOLUME ${DATA_PATH}
WORKDIR ./init
RUN apt-get update
RUN apt-get install -y powershell
COPY SomeFolder/Initialize-Database.ps1 .
CMD pwsh ./Initialize-Database.ps1 -sa_password $env:sa_password -data_path $env:DATA_PATH -Verbose
在Powershell脚本中,我将安装SQLServer模块以执行SQL查询。
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module SQLServer
if ($sa_password -ne "_") {
Write-Verbose 'Changing SA login credentials'
$sqlcmd = "CREATE DATABASE my-db"
Invoke-SqlCmd -Query $sqlcmd -ServerInstance "."
}
这个
Invoke-SqlCmd
命令失败,出现连接错误:
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: TCP Provider, error: 40 - Could not
| open a connection to SQL Server)
如何检查SQL实例是否已启动,如果未启动,如何启动?
当我使用此映像启动容器并执行到容器中时,
Get-SqlInstance -ServerInstance "." -Credential Get-Credential
失败,错误为“无法连接到服务器”。