我不情愿地执行尝试在 docker 容器中使用 Microsoft SQL Server 的耗费灵魂的任务,并且正在使用 Microsoft 提供的 mssql-server-linux 盒子。但无论我做什么我都无法登录。
这是我的 docker-compose.yml 文件:
version: '2'
services:
db:
build:
context: ./docker/db
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "My@Super@Secret"
expose:
- 1433
这是我的 Dockerfile:
FROM microsoft/mssql-server-linux
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY startup.sh setup.sql ./
CMD /bin/bash ./startup.sh
这是我的startup.sh:
/opt/mssql/bin/sqlservr & /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "My@Super@Secret" -d master -i setup.sql
这是我的 setup.sql:
CREATE DATABASE DemoData;
每次尝试使用此入口点启动容器都会导致以下错误:
SQL Server is now ready for client connections. This is an informational message; no user action is required.
2017-09-27 17:30:00.85 spid10s Polybase feature disabled.
2017-09-27 17:30:00.85 spid10s Clearing tempdb database.
2017-09-27 17:30:01.09 Logon Error: 18456, Severity: 14, State: 7.
2017-09-27 17:30:01.09 Logon Login failed for user 'sa'. Reason: An error occurred while evaluating the password. [CLIENT: 172.19.0.2]
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'sa'..
db_1 | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error code 0x2749.
db_1 | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
显然,删除入口点,进入 /bin/bash 并运行命令或尝试手动连接命令会产生相同的结果。我尝试过多种不同的密码。
我讨厌微软...提前致谢!
您正在后台启动 SQL Server,方法是使用&
。因此,您的 sqlcmd 将在启动 SQL Server 后立即启动,而不是等到它准备好接受连接。
通常你可以通过使用双&号来解决这个问题&&
。通过使用&&
在第一个命令成功完成之前,第二个命令不会启动。但是,SQL Server 是一个长时间运行的进程,因此您的第二个命令永远不会启动。
您可以通过在启动 SQL Server 后添加睡眠命令来解决此问题。
/opt/mssql/bin/sqlservr & sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost etc.
sqlcmd 将在启动 SQL Server 后 30 秒执行。如果 SQL Server 仍然不可用,请尝试延长睡眠时间。
sqlcmd 完成后,docker 容器将停止,因为不再有前台进程。 SQL Server 正在后台运行,而 docker 需要一个活动的前台进程。
Add the wait
命令再次开始等待 SQL Server 进程。
/opt/mssql/bin/sqlservr & sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost etc. && wait
免责声明我自己刚刚开始使用 Linux 和 Docker,所以可能会有更好的解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)