1

I am trying to build a SQL Server container and then run some SQL scripts to add a user and make the schema by using docker build . and later docker-compose build -> docker-compose up

FROM mcr.microsoft.com/mssql/server
COPY all.sql all.sql
COPY auto-setup-db.sql auto-setup-db.sql

RUN /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -i auto-setup-db.sql

this line^ fails with:

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

The command '/bin/sh -c /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -i auto-setup-db.sql' returned a non-zero code: 1

And I'm not sure what I'm doing wrong, does anyone have an idea?

EDIT: this is the relevant part of my docker-compose file:

version: '3'
services:
  mssql:
    image: mssql
    build: ./e2e/mssql
    container_name: mssql
    environment:
      SA_PASSWORD: "myPassword"
      ACCEPT_EULA: "Y"

EDIT 2:

Running docker-compose up gives a lot of output from SQL Server ex.

2019-08-29 17:05:13.02 spid8s      Starting up database 'tempdb'.

but I need to find a way to run the schema script in there so I thought docker-build would be the easiest place. I'm very new to docker

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Marc Sloth Eastman
  • 693
  • 1
  • 10
  • 19
  • 1
    Where in your code are you creating the instance? The Microsoft docs have the script where you need to accept the EULA, set the SA password, etc. – dfundako Aug 29 '19 at 16:41
  • @dfundako I try to pass it in like this (see edit) and get the same error – Marc Sloth Eastman Aug 29 '19 at 16:52
  • Should the Y be quoted in the environment variable? – dfundako Aug 29 '19 at 16:55
  • @dfundako yes (https://docs.docker.com/compose/aspnet-mssql-compose/) but my password should be too, good catch, will report back. EDIT: same error – Marc Sloth Eastman Aug 29 '19 at 16:56
  • I think the problem is that sql server is not started at the time when your sqlcmd command runs. I typically inspect logs for "SQL Server is now ready for client connections" before running sqlcmd, but haven't tried to do it with docker-compose – Daniel N Aug 29 '19 at 18:31
  • 1
    Similar question [Docker + mssql-server-linux: How to launch .sql file during build (from Dockerfile)](https://stackoverflow.com/q/46888045/8482479). As @Daniel N suggested, the server might not be running when the sqlcmd is executed – b0gusb Aug 30 '19 at 06:13

1 Answers1

0

Trying adding double quotes around the file path and use the full path. Please see the example.

. . . EXPOSE 1433

RUN mkdir -p /var/opt/mssql/database

WORKDIR /var/opt/mssql/database

COPY auto-setup-db.sql .

CMD /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P myPassword -i "/var/opt/mssql/database/auto-setup-db.sql" -o "/var/opt/mssql/database/auto-setup-db.log"

P Tomer
  • 1
  • 5