Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Cannot log in as sa to SQL Server running in a Docker container

ErrorSqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login failed for user 'SA'

Short answer

The sa login does not accept the password you type. In a container that happens when the password broke the policy, when a mounted data volume already holds an older sa password, when you reach a different SQL Server on the same port, or when the old variable name was used. Check docker logs first.

  • Applies to SQL Server 2022 and 2025 Linux containers, sqlcmd (mssql-tools18), Docker 27
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the container log and the port mapping. The log shows whether SQL Server started with your password, and the port shows which server you are really talking to.

    docker ps -a
    docker logs <container-id>
  2. 2Match what you see, then jump to its fix

    1. 1docker ps -a shows the container as Exited, or the login fails with a short or simple passwordThe password breaks the SQL Server password policyFix
    2. 2You changed MSSQL_SA_PASSWORD, but the old password is the one that worksA mounted data volume already has a different sa passwordFix
    3. 3docker ps shows the container mapped to another host port, or a local SQL Server also uses 1433You are connecting to a different SQL Server on the same portFix
    4. 4The error mentions ODBC Driver 17 while the image carries mssql-tools18The tools path points at the old ODBC 17 folderFix

What does the error look like?

  • Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login failed for user 'SA'

Cause 1 of 4

The password breaks the SQL Server password policy

1Check

By default the password needs at least eight characters from three of four sets: uppercase, lowercase, digits and symbols.

docker logs <container-id> | tail -n 20

2Fix

Start a new container with a password that meets the policy. Use MSSQL_SA_PASSWORD, because SA_PASSWORD is deprecated.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<StrongPassword1!>" \
  -p 1433:1433 --name sql1 -d mcr.microsoft.com/mssql/server:2022-latest

3Verify

Connect from inside the container.

docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 4

A mounted data volume already has a different sa password

1Check

When the container uses an existing data volume, SQL Server ignores MSSQL_SA_PASSWORD and keeps the sa password stored in the data.

docker inspect sql1 --format '{{json .Mounts}}'

2Fix

Use the sa password that belongs to that data. Or change it from inside the container with the old password.

docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa \
  -Q "ALTER LOGIN sa WITH PASSWORD='<NewStrongPassword1!>'"

3Verify

Connect again with the new password.

docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 4

You are connecting to a different SQL Server on the same port

1Check

If the host already listens on 1433, or you mapped the container to another port, sqlcmd from the host reaches a different server. The PORTS column of docker ps shows the mapping.

docker ps -a --format 'table {{.Names}}	{{.Ports}}	{{.Status}}'

2Fix

Map the container to a free host port and give that port to sqlcmd after a comma.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<StrongPassword1!>" \
  -p 1400:1433 -d mcr.microsoft.com/mssql/server:2022-latest
sqlcmd -S localhost,1400 -U sa

3Verify

Run a trivial query through the mapped port.

sqlcmd -S localhost,1400 -U sa -Q "SELECT @@VERSION"

Confirmed by learn.microsoft.com

Did this fix it?

Cause 4 of 4

The tools path points at the old ODBC 17 folder

1Check

Newer container images (SQL Server 2022 CU14 and later, 2019 CU28 and later) ship mssql-tools18, which lives in /opt/mssql-tools18/bin instead of /opt/mssql-tools/bin.

docker exec sql1 ls /opt/mssql-tools18/bin

2Fix

Call sqlcmd from the tools18 folder inside the container.

docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa

3Verify

The prompt 1> appears after you enter the password.

docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -Q "SELECT 1"

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Store the sa password in a secrets manager or env file, never in an image or a repository.
  • Change the sa password for any real environment, because MSSQL_SA_PASSWORD is visible in the process list.
  • Map each container to its own host port and write the port into every connection string.
  • Prefer a named login with the least rights over sa for applications.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

PHP throws "could not find driver" when it opens a PDO connection

Fatal error: Uncaught PDOException: could not find driver in /var/www/app/db.php:12

PDO is only a layer; each database needs its own driver extension, such as pdo_mysql or pdo_pgsql, and the DSN prefix must name a driver that is loaded.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.