我正在尝试使用 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
的内容在我看来并不重要。
真正的问题是当我尝试使用命令 docker build -t demo 构建此
我总是收到以下错误:Dockerfile
时。
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
运行脚本?
最佳答案
我最终使用了 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
关于sql-server - Docker + mssql-server-linux : How to launch . 构建期间的 sql 文件(来自 Dockerfile),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888045/