SQL Server Linux的Docker容器不断退出
我想通过在容器启动时添加一个带有一个表的简单数据库来修改SQL Server Linux容器的行为。
我看到这个问题的docker图像版本是截至2017年5月20日的最新版本,即ctp2-1
。
我正在使用17.05.0-ce
的最新版本的Docker for Windows
。 我将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成功创buildDemoData
数据库后的日志底部。
[![enter image description here][2]][2]
国际海事组织,日志中的问题是这一个
Parallel redo is shutdown for database 'DemoData' with worker pool size [1].
我已经尝试了各种其他的SQL语句(甚至添加自定义的MDF和LDF文件并附加到它们)来添加行为到OOB图像。 我甚至能够在容器退出之前使用SSMS连接到新的数据库几秒钟!
有没有人看过这个问题? 有人可以试试这个吗?
有效的解决scheme
根据我收到的反馈,很明显Dockerfile的CMD正在返回。 以下是解决问题的完整步骤。
[请让我知道,如果你已经find了一个更可靠的解决scheme,不依赖于某个超时值,我已经在这个解决scheme中编码,允许SQL Server在容器启动时自行引导]
准备文件
(1)创build一个本地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)添加一个子文件夹“自定义”
C:\temp\docker\custom\
(4)添加2个文件到“自定义”子文件夹,如下所示。
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)build立图像后,获得图像的前3个字符,在我的情况下086
docker images
(D)使用正确的图像ID和正确的密码创build一个容器
docker run -d -e 'SA_PASSWORD=HdkPKD57%@6876^' -e 'ACCEPT_EULA=Y' -p 1433:1433 086
(五)检查你的容器正在运行
docker ps -a
这个容器不会退出! 预期的数据库“DemoData”被创build。 问题解决了!
docker logs 2aa
命令( 2aa
是我的容器ID,你的将是不同的)显示干净的构build,没有错误或警告。 日志开始如下
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
是包含容器的docker主机的IP地址)
重要笔记
-
sqlcmd SA密码
sqlcmd
是用于运行dbcreate.SQL
并创build数据库DemoData
的实用工具。 该实用程序在Linux上使用ODBC驱动程序,并且对如何指定交换机的值(尤其是密码-P
很敏感。为了避免在这里find和解释login相关的问题,请仔细select您的强密码,并在
-P
下的entrypoint.sh
指定它,而不用double quotes
或single quotes
或[]
包装。 参见上面的(4)
。此外,这个密码必须与你传递给容器的
docker run
环境variables相匹配。 参见上面的(D)
以避免密码不匹配。关于sqlcmd的详细文档在这里 。
-
sqlcmdlogin超时
请注意,我如何使用
-l
开关指定entrypoint.sh
文件中sqlcmd的login超时时间为30 seconds
。 这是我的解决scheme的关键,以避免CMD返回(这使容器退出)。 此超时足够长,以便SQL Server启动。