microsoft / mssql-docker

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

Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user 'sa'.` #581

Open lmagalhaes1989 opened 4 years ago

lmagalhaes1989 commented 4 years ago

Hi guys, I'm developing a small containerized app in .Net Core 3.1.1 with connection to an mssql database in docker and I'm facing an odd behaviour... After I stop the containers and then issue the commands docker-compose build; docker-compose up sometimes (not always) I'm facing this error:

web_1 | crit: Microsoft.AspNetCore.Hosting.Diagnostics[6] web_1 | Application startup exception web_1 | Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user 'sa'.

This is my docker-compose.yml file `version: "3" services: web: build: . ports:

This is my connection string code in my file startup.cs var connection = @"Server=db;Database=master;User=sa;Password=Your_password123;"; services.AddDbContext<AppDbContext>(opt => opt.UseSqlServer(connection));

But if I restart docker the database container starts smoothly again... Any ideas what the issue might be? I'm using Docker for windows.

Thanks in advance

croblesm commented 4 years ago

Hello there,

I did my best to re-format your docker-compose.yml, this is what you current have:

version: "3"
services:
    web:
      build: . 
      ports:
          - "8000:80"
      depends_on:
          - db
      command: 
          - "sleep 5;" 
    db: 
      image: "mcr.microsoft.com/mssql/server" 
      environment:
          SA_PASSWORD: "Your_password123"
          ACCEPT_EULA: "Y"

And here is how I would do it, please note I have added some comments to explain the changes"

version: "3"
services:
    web: 
      build: . 
      ports: 
          - "8000:80" 
      depends_on: 
          - db 
      command: 
          - "sleep 5;" 
    db:
      # Latest version of SQL Server 2019 for Ubuntu 18.04
      image: "mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04"
      environment:
          SA_PASSWORD: "Your_password123"
          ACCEPT_EULA: "Y"
      # Adding container name to make sure connection string doesn't fails
      container_name: db
      # Adding port mapping, using 1433 as default
      ports:
          - "1433:1433"

Just keep in mind, your the connection string is pointing to the "master" database instead of your application database.

Regards,

lmagalhaes1989 commented 4 years ago

Thanks for your answer @dbamaster , although I have more clues for this issue. I'm a beginner at Docker, and I was doing a step wrong. Every time that I wanted to start the container I issue two commands docker-compose build and docker-compose up and that's unnecessary because the docker-compose file already has an instruction to build the container. Now only issuing the docker-compose up I only have the error the first time a started it. If I stop the container and start again the issue doesn't reproduce. But I will force the container_name to make sure the connection string doesn't fail.

Many thanks

croblesm commented 4 years ago

Thanks for your answer @dbamaster , although I have more clues for this issue. I'm a beginner at Docker, and I was doing a step wrong. Every time that I wanted to start the container I issue two commands docker-compose build and docker-compose up and that's unnecessary because the docker-compose file already has an instruction to build the container. Now only issuing the docker-compose up I only have the error the first time a started it. If I stop the container and start again the issue doesn't reproduce. But I will force the container_name to make sure the connection string doesn't fail.

Many thanks

Great news!! And yes, you are correct about the docker-compose command. Hopefully, you will be out of errors next time you try it 😉

Regards,

bigbizze commented 4 years ago

For what it's worth, I got the same error as OP did with a wildly different cause.

It's easy to miss this line in the log files & it's a rather silly reason to have this problem, but make sure the password you set for mssql in your Dockerfile is complex enough:

21:40:54.14 spid23s ERROR: Unable to set system administrator password: Password validation failed. The password does not meet SQL Server password policy requirements because it is not complex enough. The password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols..

It was definitely my own fault however. The folks at Microsoft did make it clear that it needed to be a strong password in hindsight when looking through the documentation. It slipped my mind I guess, as I was just screwing around with a few things for fun ^_^.

NemesLaszlo commented 3 years ago

Hello,

https://github.com/microsoft/mssql-docker/issues/506#issuecomment-868446561