Cannot log in as sa to SQL Server running in a Docker container
Sqlcmd: 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.
Fix it fast
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>2Match what you see, then jump to its fix
- 1
docker 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
You changed MSSQL_SA_PASSWORD, but the old password is the one that worksA mounted data volume already has a different sa passwordFix - 3
docker 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
The error mentions ODBC Driver 17 while the image carries mssql-tools18The tools path points at the old ODBC 17 folderFix
- 1
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 202Fix
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-latest3Verify
Connect from inside the container.
docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd -S localhost -U saConfirmed by learn.microsoft.com
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 saConfirmed by learn.microsoft.com
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 sa3Verify
Run a trivial query through the mapped port.
sqlcmd -S localhost,1400 -U sa -Q "SELECT @@VERSION"Confirmed by learn.microsoft.com
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/bin2Fix
Call sqlcmd from the tools18 folder inside the container.
docker exec -it sql1 /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa3Verify
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
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
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
mongodb-memory-server will not start under Jest or in Docker
NoSystemBinaryFoundError
When mongodb-memory-server fails to start, the real reason is hidden behind a generic message.
4 causes, each with a fixDatabases
The MySQL container stops with: the designated data directory is unusable
[ERROR] --initialize specified but the data directory exists. Aborting.
This message closes a failed first start.
3 causes, each with a fixDatabases
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 fixDatabases
A Postgres query in Docker fails with: could not resize shared memory segment
ERROR: could not resize shared memory segment: No space left on device
Postgres in a container hands query memory to parallel workers through the container's /dev/shm.
2 causes, each with a fixDatabases
The Postgres container refuses to start: data directory has invalid permissions
mkdir: cannot create directory ‘/home/postgres/pgdata/data’: permission denied
Postgres starts only when its data directory is owned by the server user and closed to everyone else: mode 0700, or 0750 when group read is enabled.
3 causes, each with a fix