Home > Back-end >  docker-compose for mssql not using .env file?
docker-compose for mssql not using .env file?

Time:01-17

I have a .env file named .mssql filled with the basic MSSQL options:

ACCEPT_EULA=Y
MSSQL_SA_PASSWORD=12Password34

My docker-compose.yml looks like this:

version: '3'
services:
  db:
    image: "mcr.microsoft.com/mssql/server:2017-latest"
    volumes:
      - ./db-data:/var/opt/mssql
      - ./sql_scripts:/sql_scripts
    env_file:
      - .envs/.local/.mssql
    healthcheck:
      test: [ "CMD", "/opt/mssql-tools/bin/sqlcmd", "-S", "localhost", "-U", "sa", "-P", "$SA_PASSWORD", "-Q", "SELECT 1" ]
      interval: 30s
      timeout: 30s
      retries: 3
    # This command runs the sqlservr process and then runs the sql scripts in the sql_scripts folder - it uses init_done.flag to determine if it has already run
    command: /bin/bash -c '/opt/mssql/bin/sqlservr; if [ ! -f /sql_scripts/init_done.flag ]; then apt-get update && apt-get install -y mssql-tools unixodbc-dev; /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -i /sql_scripts/db_setup.sql; /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -i /sql_scripts/second_db_setup.sql; /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -i /sql_scripts/third_db_setup.sql; touch /sql_scripts/init_done.flag; fi; tail -f /dev/null'
    ports:
      - 1433:1433
volumes:
  db-data:

When I run the command docker-compose up -d --build

It gives me a warning in the terminal where I ran the docker-compose command: WARN[0000] The "SA_PASSWORD" variable is not set. Defaulting to a blank string.

Then when the container boots I see it begin the process without issue. Then I see the following in the container logs:

Logon       Error: 18456, Severity: 14, State: 8.
Logon       Login failed for user 'sa'. Reason: Password did not match that for the login provided.

CodePudding user response:

You set MSSQL_SA_PASSWORD in your env file. But as far as I see, in your Dockerfile you try to log in with $SA_PASSWORD.

So, either you change MSSQL_SA_PASSWORD to SA_PASSWORD in your env file or you change the Dockerfile (in the "test" and "command" section) from $SA_PASSWORD to $MSSQL_SA_PASSWORD. The second option could be preferred, since the image might additionally require MSSQL_SA_PASSWORD to be correctly set internally. I can't tell you about that specifically, since I don't know the MSSQL image enough.

  • Related