Docker + mssql-server-linux: How to launch .sql file during build (from Dockerfile)

流过昼夜 提交于 2019-12-03 01:56:28

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 
VDR

From the mssql-server-linux dockerfile, looks like mssql is started on docker run, so you have to modify your last "RUN" command in your dockerfile to start sql-server in the background, run your sql file and stop the sql-server.

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 

When you build image main process is not running eat. In your case main process is SQL Server.

Process will run only when you start container with command docker run ...

And you can make database initialization in this phase. microsoft/mssql-server-linux Dockerfile contains some tips

Copy all SQL Server runtime files from build drop into image. COPY ./install /

Just copy into build phase init scripts to ./install dir and it will be executed into start phase.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!