Py学习  »  docker

SQL Server Docker Compose SQLCMD不执行

Felix Stumvoll • 4 年前 • 994 次点击  

我正在docker compose中设置一个SQL服务器 RUN 指示。这不起作用,但是当我用sh在运行中的容器上执行相同的命令时,它起作用

我的撰写文件如下:

version: "3.7"

services:
  mssql:
    build: ./mssql
    environment: 
      SA_PASSWORD: "Password12345!"
      ACCEPT_EULA: "Y"
    container_name: mssqlDB
    ports:
      - "1433:1433"
    restart: always

这里是我的档案:

FROM mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
COPY ./prod.sql /
RUN ./opt/mssql-bin/sqlcmd -S localhost -U SA -P "Password12345!" -Q "Create Database HelloWorld"
CMD ["/opt/mssql/bin/sqlservr"]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52971
 
994 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Max
Reply   •   1 楼
Max    4 年前

这是因为SQL Server实例未启动,您必须等待它。

Docker Hub official page of SQL Server run a sql script on Docker container .

下面我为您重新修改了GitHub代码

# Typically SQL Server takes about 5-10 seconds to start up 
# Wait for the SQL Server to come up (90 sec) You can reduce to 20sec and see
sleep 90s

#run the setup script to create the DB and the schema in the DB
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P password -d master -i prod.sql

入口点.sh

#start SQL Server, start the script to create the DB and import the data
/opt/mssql/bin/sqlservr & initialize.sh 

停靠文件

FROM mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04
COPY ./prod.sql /

# Grant permissions for the import-data script to be executable
RUN chmod +x ./initialize.sh

CMD /bin/bash ./entrypoint.sh

我个人提出的另一个解决方案是运行SQL Server服务并等待该服务出现。

创建.sh

/opt/mssql-tools/bin/sqlcmd -U sa -P $1 -Q 'CREATE DATABASE [MyNewDatabase]'
/opt/mssql-tools/bin/sqlcmd -U sa -P $1 -d 'MyNewDatabase' -i /src/script.sql

脚本.sql

CREATE TABLE MyTable (..)


FROM mcr.microsoft.com/mssql/server:2017-latest-ubuntu
EXPOSE 1433

WORKDIR /
COPY ./create.sh /src/
COPY ./script.sql /src/

ENV ACCEPT_EULA Y
ENV SA_PASSWORD P@ssw0rd

RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc

RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" \
    && /src/create.sh P@ssw0rd \
    && pkill sqlservr