我正在尝试使用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运行脚本?
从mssql-server-linux dockerfile看起来mssql是在docker run上启动的,所以你必须修改你的dockerfile中的最后一个“RUN”命令,在后台启动sql-server,运行你的sql文件并停止sql-服务器.
原文链接:https://www.f2er.com/docker/436667.htmlRUN /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