Clyde Barrow
Clyde Barrow

Reputation: 2102

Docker compose and microsoft/mssql-server-linux: How to run init sql script ?

I want run locally SQL Server and RabbitMQ on docker. However I cannot init SQL Server Database with my sql script. My Database Dockerfile

FROM microsoft/mssql-server-linux:latest

COPY ./mock-database/db-dump.sql /etc/mock-database/

And here is my docker-compose.yml

version: '3.1'
services:
  rabbitMq:
    image: rabbitmq
        hostname: localhost
        ports:
        - "15672:15672"
        - "5672:5672"
        - "5671:5671"

      db:
        image: listings_db
        ports:
        - "1433:1433"
        environment:
          SA_PASSWORD: "microsoftSucks1!"
          ACCEPT_EULA: "Y"
        command: /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P microsoftSucks1! -i /etc/mock-database/db-dump.sql

And after I run my app with myscript

    docker build -t listings_db -f mock-database/databaseDockerfile . 
    docker build -t rabbitmq -f rabbitmq/rabbitmqDockerfile . 
    docker-compose -f stack.yml up

I got this exception

db_1        | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
db_1        | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
db_1        | Sqlcmd: Error: Microsoft ODBC Driver 17 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..

How to solve this ?

Upvotes: 0

Views: 1755

Answers (1)

Peter Kneale
Peter Kneale

Reputation: 1936

You need to wait for a few seconds before the SQL Server is both up and able to accept connections. ie:

#wait for the SQL Server to come up
sleep 10s

#run the setup script
/opt/mssql-tools/bin/sqlcmd  .....

#import the data 
/opt/mssql-tools/bin/bcp .......

per https://github.com/twright-msft/mssql-node-docker-demo-app/blob/master/import-data.sh

Upvotes: 1

Related Questions