使用Powershell Core初始化SQL Server Linux容器中的数据库



我在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失败并出现错误'Failed to connect to server'。

找到了。在Dockerfile中,我现在在SQL启动后调用Powershell脚本:

CMD ( /opt/mssql/bin/sqlservr & ) | grep -q "Service Broker manager has started" 
&& pwsh ./Initialize-Database.ps1 -sa_password $SA_PASSWORD -data_path $DATA_PATH -Verbose

https://www.kenmuse.com/blog/devops-sql-server-dacpac-docker

相关内容

  • 没有找到相关文章

最新更新