Docker + mssql-server-linux:如何在构建期间启动.sql文件(来自Dockerfile) [英] Docker + mssql-server-linux: How to launch .sql file during build (from Dockerfile)

查看:130
本文介绍了Docker + mssql-server-linux:如何在构建期间启动.sql文件(来自Dockerfile)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MSSQL DB创建自己的Docker映像进行开发。它基于 microsoft / mssql-server-linux 映像。在构建期间,我想将一些 .sql 文件复制到容器中,然后运行这些脚本(以创建数据库模式,表,插入一些数据等)。我的 Dockerfile 看起来像这样:

I am trying to create my own Docker image with MSSQL DB for development. It's based on microsoft/mssql-server-linux image. During the build I want to copy some .sql files into the container and then run these scripts (to create DB schemas, tables, insert some data etc.). My Dockerfile looks like this:

# 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。我总是会遇到以下错误:

The real problem is when I am trying to build this Dockerfile with command docker build -t demo . I always get these errors:

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..

但是当我删除最后一条命令时(运行初始脚本) ),生成并运行映像,然后调用相同的命令,如下所示:

But when I delete the last command (running initial scripts), build and run image, and call the same command like this:

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 运行脚本?

Then everything is going well. Why cannot I run script from Dockefile?

推荐答案

我最终使用了VDR解决方案的略微修改版本,它通过检查日志而不是休眠10秒来等待sqlservr启动:

I ended up using a slightly modified version of VDR's solution which waits for the sqlservr to start by checking the logs instead of sleeping 10 seconds:

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 

这篇关于Docker + mssql-server-linux:如何在构建期间启动.sql文件(来自Dockerfile)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆