Cannot log into SQL Server in mssql-server-linux c

2019-08-29 08:50发布

问题:

I am begrudgingly performing the soul-draining task of trying to use Microsoft SQL Server in a docker container, and am using the mssql-server-linux box provided by Microsoft. But no matter what I do I cannot login.

Here is my docker-compose.yml file:

version: '2'

services:
  db:
    build:
      context: ./docker/db
    environment:
      ACCEPT_EULA: "Y"
      SA_PASSWORD: "My@Super@Secret"
    expose:
      - 1433

Here is my Dockerfile:

FROM microsoft/mssql-server-linux

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY startup.sh setup.sql ./

CMD /bin/bash ./startup.sh

Here is my startup.sh:

/opt/mssql/bin/sqlservr & /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "My@Super@Secret" -d master -i setup.sql

Here is my setup.sql:

CREATE DATABASE DemoData;

Every attempt to bring up the container with this entrypoint results in this error:

SQL Server is now ready for client connections. This is an informational message; no user action is required.
2017-09-27 17:30:00.85 spid10s     Polybase feature disabled.
2017-09-27 17:30:00.85 spid10s     Clearing tempdb database.
2017-09-27 17:30:01.09 Logon       Error: 18456, Severity: 14, State: 7.
2017-09-27 17:30:01.09 Logon       Login failed for user 'sa'. Reason: An error occurred while evaluating the password. [CLIENT: 172.19.0.2]
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'sa'..
db_1     | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error code 0x2749.
db_1     | Sqlcmd: Error: Microsoft ODBC Driver 13 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..

Obviously removing the entrypoint, going in with /bin/bash and running the command or an attempt to connect command manually produces the same result. I have tried a variety of different password.

I hate Microsoft... Thanks in advance!

回答1:

You're starting SQL Server in the background by using the &. Because of this your sqlcmd will start immediately after starting SQL Server and not wait until it's ready to accept connections.

Usually you could solve this by using the double ampersand &&. By using && the second command won't start before the first command completed succesfully. However, SQL Server is a long running proces so your second command will never start.

You can solve this by adding a sleep command after starting SQL Server.

/opt/mssql/bin/sqlservr & sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost etc.

The sqlcmd will execute 30 seconds after starting SQL Server. If SQL Server is still not available try to up the sleep time.

After the sqlcmd is finished the docker container will stop because there is no more foreground process. SQL Server is running the background and docker requires an active foreground process.

Add the wait command to start waiting for the SQL Server process again.

/opt/mssql/bin/sqlservr & sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost etc. && wait

disclaimer I just started working with Linux and Docker myself so there might be better solutions.