作者:溪边莎草 | 来源:互联网 | 2023-01-11 20:32
我正在尝试使用MSSQL DB创建自己的Docker镜像以进行开发.它基于microsoft/mssql-server-linux
图像.在构建期间,我想将一些.sql
文件复制到容器中,然后运行这些脚本(创建数据库模式,表,插入一些数据等).我Dockerfile
看起来像这样:
# use MSSQL 2017 image on Ubuntu 16.04
FROM microsoft/mssql-server-linux:2017-latest
# create directory within SQL container for database files
RUN mkdir -p /opt/mssql-scripts
# copy the database files from host to container
COPY sql/000_create_db.sql /opt/mssql-scripts
# set environment variables
ENV MSSQL_SA_PASSWORD=P@ssw0rd
ENV ACCEPT_EULA=Y
# run initial scripts
RUN /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/mssql-scripts/000_create_db.sql
000_create_db.sql
在我看来,内容并不重要.
真正的问题是当我尝试Dockerfile
使用命令构建它时,docker build -t demo .
我总是会遇到这些错误:
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error code 0x2749.
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..
但是当我删除最后一个命令(运行初始脚本)时,构建并运行图像,并调用相同的命令:
docker build -t demo .
docker run -p 1433:1433 --name mssql -d demo
docker exec -it mssql "bash"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/mssql-scripts/000_create_db.sql
一切都进展顺利.为什么我不能运行脚本Dockefile
?
1> robd..:
我最终使用了VDR解决方案的稍微修改的版本,该版本通过检查日志而不是休眠10秒来等待sqlservr启动:
RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/mssql-scripts/000_create_db.sql \
&& pkill sqlservr
2> VDR..:
从MSSQL服务器Linux的dockerfile,看起来就像是MSSQL泊坞窗上开始运行,所以你必须在你的dockerfile修改最后的"运行"命令,在后台启动SQL服务器,运行SQL文件并停止SQL-服务器.
RUN /opt/mssql/bin/sqlservr --accept-eula & sleep 10 \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/mssql-scripts/000_create_db.sql \
&& pkill sqlservr