microsoft / mssql-docker

Official Microsoft repository for SQL Server in Docker resources
MIT License
1.72k stars 759 forks source link

Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired. #347

Open logcorner opened 6 years ago

logcorner commented 6 years ago

Hi, I try this tutorial to run a sqlserverlinux image and insert some data and i have always this error ; Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.

I use docker for windows 18.06.0-ce

SqlCmdScript.sql

create database juliedb;
GO
use juliedb;
create table people (PersonId int Primary Key, Name nvarchar(50));
insert into people values (1,'julie');
insert into people values (2,'giantpuppy');
select * from people

SqlCmdStartup.sh

#wait for the SQL Server to come up
sleep 20s
#run the setup script to create the DB and the schema in the DB
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Passw0rd -d master -i SqlCmdScript.sql

entrypoint.sh

#start the script to create the DB and data then start the sqlserver
./SqlCmdStartup.sh & /opt/mssql/bin/sqlservr

Dockerfile

FROM microsoft/mssql-server-linux
ENV SA_PASSWORD=Passw0rd
ENV ACCEPT_EULA=Y
COPY entrypoint.sh entrypoint.sh
COPY SqlCmdStartup.sh SqlCmdStartup.sh
COPY SqlCmdScript.sql SqlCmdScript.sql
RUN chmod +x ./SqlCmdStartup.sh
CMD /bin/bash ./entrypoint.sh

I try the tutorial : Data Points - On-the-Fly SQL Servers with Docker) Do you have any idea ?

twright-msft commented 6 years ago

docker exec into your running container and see if if sqlservr is even running: by using top

docker exec -it /bin/bash root@> top also try starting your container interactively by not passing -d to docker run so you can see the output as SQL Server starts up.

logcorner commented 6 years ago

thank you, @twright-msft for your answer

docker exec -it sql1 "bash" /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Passw0rd

CREATE DATABASE TestDB SELECT Name from sys.Databases works fine .
Everything works mannualy but if i use the dockerfile, it throws an exception Sqlcmd: Error

twright-msft commented 6 years ago

Maybe try without the -d parameter in your .sh file? Also try using 127.0.0.1 instead of localhost.

vu-le commented 6 years ago

You can try this

RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" \ && /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Passw0rd -d master -i SqlCmdScript.sql

logcorner commented 6 years ago

I have the following error @vu-le

./setup-database.sh: line 7: syntax error near unexpected token `/opt/mssql/bin/sqlservr'
./setup-database.sh: line 7: `RUN(/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" '

Here is the content of setup-database.sh

echo 'Please wait while SQL Server 2017 warms up'
sleep 10s

echo 'Initializing database after 30 seconds of wait'
#/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'MyC0m9l&xP@ssw0rd' -d master -i initialize-database.sql

RUN(/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" 
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'MyC0m9l&xP@ssw0rd' -d master -i initialize-database.sql

echo 'Finished initializing the database'

NB : Im using docker for windows and linux container

vu-le commented 6 years ago

Dockerfile

FROM microsoft/mssql-server-linux ENV SA_PASSWORD=Passw0rd ENV ACCEPT_EULA=Y COPY SqlCmdScript.sql SqlCmdScript.sql RUN(/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" && /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'Passw0rd' -d master -i SqlCmdScript.sql

logcorner commented 6 years ago

Hi, @vu-le thank you, Running the command on your docker file : docker build –t testsql/sqldb . I have the following Sending build context to Docker daemon 6.144kB Error response from daemon: Dockerfile parse error line 5: unknown instruction: RUN(/OPT/MSSQL/BIN/SQLSERVR

error version

vu-le commented 6 years ago

can you run it in a bash shell ?

logcorner commented 6 years ago

bash

using bash @vu-le

mvaneijk commented 5 years ago

FWIW: the response of vu-le had some syntax errors, if you paste the following code, it should work

FROM microsoft/mssql-server-linux
ENV SA_PASSWORD=Passw0rd
ENV ACCEPT_EULA=Y
COPY SqlCmdScript.sql SqlCmdScript.sql
RUN (/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" && /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'Passw0rd' -d master -i SqlCmdScript.sql
vitalybibikov commented 5 years ago

This is my script to create logins and attach dbs on docker run:

docker run -it -p 1433:1433 --name mssql -v C:\docker-db:/var/opt/mssql mssql

FROM mcr.microsoft.com/mssql/server:2017-latest
ENV SA_PASSWORD=Passw0rd
ENV ACCEPT_EULA=Y

COPY ./CreateLogins.sql .
COPY ./AttachDb.sql .

COPY ./EntryPoint.sh .
COPY ./SqlCmdStartup.sh .

RUN chmod +x ./SqlCmdStartup.sh
CMD /bin/bash ./EntryPoint.sh

EntryPoint.sh

#!/bin/bash
./SqlCmdStartup.sh & /opt/mssql/bin/sqlservr

SqlCmdStartup.sh

#!/bin/bash
set -e

wait_time=90s 
password=Passw0rd

# wait for SQL Server to come up
echo importing data will start in $wait_time...
sleep $wait_time

echo running CreateLogins...
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P $password -i CreateLogins.sql

echo running AttachDb...
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P $password -i AttachDb.sql

exec "$@"
mokhos commented 3 years ago

Hi. I have same problem with a little difference. I'm using GitLab CI to do integration for my mssql using the docker image. When I run a mssql-container myself and try to execute a command using "sqlcmd", it's fine. But as GitLab CI runs a container and tries to connect to the DB or something else, it fails with same login error.

Can anyone help me?

nchandra-seg commented 3 years ago

I am using @vitalybibikov code. it works fine. when I use CMD it hangs. when I use entrypoint, it works fine. But finishes script and hangs. Not exiting the container. what is the problem?