[英]Docker container for SQL Server Linux keeps exiting
我想通過在容器啟動時添加一個包含一個表的簡單數據庫來修改SQL Server Linux容器的行為。
我看到此問題的docker圖像版本是截至2017年5月20日的最新版本,即ctp2-1
。
我正在使用Docker for Windows
,最新版本為17.05.0-ce
。 我將MobyLinuxVM的RAM增加到6144MB
因為建議使用4GB以上。
重現問題的步驟
准備文件
(1) Create a local Windows folder, in my case,
C:\temp\docker\
(2) Add "Dockerfile" (note no file extension) with the following content.
FROM microsoft/mssql-server-linux:latest
COPY ./custom /tmp
RUN chmod +x /tmp/entrypoint.sh \
&& chmod +x /tmp/createdb.sh
CMD /bin/bash /tmp/entrypoint.sh
(3) Add a subfolder "custom"
C:\temp\docker\custom\
(4) Add 3 files to "custom" subfolder with following content.
(A) entrypoint.sh
/opt/mssql/bin/sqlservr & /tmp/createdb.sh
(B) createdb.sh
sleep 30s
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P '!StrongPass45' -d master -i /tmp/createdb.sql
(C) createdb.sql
CREATE DATABASE DemoData;
GO
USE DemoData;
GO
CREATE TABLE Products (ID int, ProductName nvarchar(max));
GO
運行DOCKER命令(你將在這里看到最后一步的問題)
(1) Open PowerShell and cd to folder in step (1) above, in my case
cd C:\temp\docker
(2) Run docker build command
docker build .
(3) After image is built, run the following command and remember the first 3 letters of your image id, in my case "e65"
docker images
[![enter image description here][2]][2]
(4) Create the container with the following command (note the last 3 characters should be replaced by yours, also use the same password you used above)
docker run -d -e SA_PASSWORD="!StrongPass45" -e ACCEPT_EULA="Y" -p 1433:1433 e65
(5) Check your container is running
docker ps -a
[![enter image description here][2]][2]
(6) Wait about 2 minutes and check your container status again. AT THIS POINT, THE CONTAINER WOULD HAVE EXITED.
docker ps -a
[![enter image description here][2]][2]
我按如下方式檢查了日志。
docker logs e65
在SQL Server成功創建DemoData
數據庫之后,下面是日志的底部。
[![enter image description here][2]][2]
IMO,日志中的問題就是這一行
Parallel redo is shutdown for database 'DemoData' with worker pool size [1].
我已經嘗試了各種其他SQL語句(甚至添加自定義MDF和LDF文件並附加到它們)來向OOB映像添加行為。 在容器退出之前,我甚至可以使用SSMS連接到新數據庫幾秒鍾!
有沒有人見過這個問題? 有人可以嘗試一下嗎?
有效的解決方案
根據我收到的反饋,很明顯Dockerfile中的CMD正在返回。 以下是解決問題的完整步驟。
[請告訴我,如果您找到了一個更可靠的解決方案,它不依賴於我在此解決方案中編碼的某個超時值,以允許SQL Server在容器啟動時自行引導]
准備文件
(1)在我的情況下,創建一個本地Windows文件夾
C:\temp\docker\
(2)添加“Dockerfile”(注意沒有文件擴展名),內容如下。
FROM microsoft/mssql-server-linux:latest
COPY ./custom /tmp
RUN chmod +x /tmp/entrypoint.sh
CMD /bin/bash /tmp/entrypoint.sh
(3)添加子文件夾“custom”
C:\temp\docker\custom\
(4)將2個文件添加到“custom”子文件夾,如下所示。
entrypoint.sh
#!/bin/bash
set -e
run_cmd="/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P HdkPKD57%@6876^ -l 30 -d master -i /tmp/createdb.sql"
>&2 echo "Allowing 30 seconds for SQL Server to bootstrap, then creating database.."
until $run_cmd & /opt/mssql/bin/sqlservr; do
>&2 echo "This should not be executing!"
done
createdb.sql
CREATE DATABASE DemoData;
GO
USE DemoData;
GO
CREATE TABLE Products (ID int, ProductName nvarchar(max));
GO
運行DOCKER命令
(A)在我的情況下,在上面的步驟(1)中打開PowerShell和cd到文件夾
cd C:\temp\docker
(B)運行docker build命令
docker build .
(C)構建圖像后,獲取圖像的前3個字符,在我的情況下為086
docker images
(D)使用正確的圖像ID和正確的密碼創建容器
docker run -d -e 'SA_PASSWORD=HdkPKD57%@6876^' -e 'ACCEPT_EULA=Y' -p 1433:1433 086
(E)檢查您的容器是否正在運行
docker ps -a
這個容器沒有退出! 創建了預期的數據庫“DemoData”。 問題解決了!
docker logs 2aa
命令( 2aa
是我的容器ID,你的將是不同的)顯示干凈的構建,沒有錯誤或警告。 日志開始如下
Allowing 30 seconds for SQL Server to bootstrap, then creating database..
This is an evaluation version. There are [173] days left in the evaluation period.
2017-05-21 17:39:50.69 Server Setup step is copying system data file 'C:\templatedata\master.mdf' to '/var/opt/mssql/data/master.mdf'.
....
並結束如下。
2017-05-21 17:39:54.20 spid51 Starting up database 'DemoData'.
2017-05-21 17:39:54.43 spid51 Parallel redo is started for database 'DemoData' with worker pool size [1].
2017-05-21 17:39:54.44 spid51 Parallel redo is shutdown for database 'DemoData' with worker pool size [1].
2017-05-21 17:39:54.51 spid7s Recovery is complete. This is an informational message only. No user action is required.
Changed database context to 'DemoData'.
與SSMS連接
我能夠使用SSMS成功連接到此數據庫,如下所示(IP地址10.0.75.1
是包含容器的10.0.75.1
主機的地址)
重要筆記
sqlcmd SA密碼
sqlcmd
是用於運行dbcreate.SQL
並創建數據庫DemoData
的實用程序。 該實用程序在Linux上使用ODBC驅動程序,並且對如何為交換機指定值敏感,尤其是密碼-P
。
為了避免在此處找到並在 此處 解釋的登錄相關問題,請仔細選擇您的強密碼,並在-P
下的entrypoint.sh
指定它,而不用double quotes
或single quotes
或[]
包裝。 見上文(4)
。
此外,此密碼必須與要傳遞給容器的docker run
環境變量相匹配。 請參閱上面的(D)
以避免密碼不匹配。
sqlcmd登錄超時
請注意我如何使用-l
開關為entrypoint.sh
文件中的sqlcmd指定30 seconds
登錄超時。 這是我的解決方案的關鍵,以避免CMD返回(這使得容器退出)。 此超時足夠長,SQL Server可以啟動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.