Home > OS >  Docker compose refuses to apply environment variables
Docker compose refuses to apply environment variables

Time:08-11

UPDATE
It appears the problem is specifically related to the RUN command in the Dockerfile. If I remove it, the build works fine and the environment variables are clearly being picked up since the password gets applied and I can connect using it. Not sure why the login fails in the RUN command, I've seen many examples using similar code.

I'm working on a very basic docker compose file to setup a dev environment for an app and I started with the database server, which is MS SQL. Here's what the docker-compose.yml file looks like:

version: '3.8'

services:
  mssql:
    build:
      context: .
      dockerfile: docker/mssql/Dockerfile
    ports:
      - '1434:1433'
    environment:
      ACCEPT_EULA: "Y"
      SA_PASSWORD: "YourStrong!Passw0rd"
    volumes:
      - mssql-data:/var/opt/mssql

As you can see from my dockerfile path, that's in a sub-path and looks like this:

FROM mcr.microsoft.com/mssql/server:2019-latest

COPY ./docker/mssql/TESTDB.bak /var/opt/mssql/backup/TESTDB.bak

RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" && /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P "YourStrong!Passw0rd" -Q 'RESTORE DATABASE TESTDB FROM DISK = "/var/opt/mssql/backup/TESTDB.bak" WITH MOVE "TESTDB_Data" to "/var/opt/mssql/data/TESTDB.mdf", MOVE "TESTDB_Log" to "/var/opt/mssql/data/TESTDB_log.ldf"'

(Yes, I realize that the password in the RUN command is redundant, I had tried to use a variable there earlier and since it wasn't working I hard coded it.)

When I run docker-compose up -d, I always get this error: Login failed for user 'SA'

I wasted way too much time thinking there was actually something wrong with the password until I realized that if I add the environment variables directly in the Dockerfile, it works. So in my Dockerfile, above the RUN command, I can just do this:

ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=YourStrong!Passw0rd

So I concluded that my environment variables simply aren't being read. I tried with quotes, without quotes, using env_file instead, nothing seems to work. I also tried the following format, no luck:

environment
  - ACCEPT_EULA=Y
  - SA_PASSWORD=YourStrong!Passw0rd

I also tried using MSSQL_SA_PASSWORD instead of SA_PASSWORD, as well as having both in there. I assumed that was unlikely to be the problem though given SA_PASSWORD works fine. Lastly, I tried using a 2017 image in case it was image specific, that didn't work either.

I'm assuming it must be something silly I'm missing. I saw a lot of talk of .env in the root being different, but if I understood correctly people go wrong with that when they try to use environment values in their docker-compose.yml file, which is not what I'm doing here. So I'm about ready to lose my mind on this as it seems like such a simple, basic thing.

CodePudding user response:

This is confusing well . . . because yaml.

When you write:

environment:
  ACCEPT_EULA: "Y"
  SA_PASSWORD: "YourStrong!Passw0rd"

You are creating keys named ACCEPT_EULA with the value "Y" and "SA_PASSWORD" with the corresponding value.

docker-compose will ignore these, what it will apply, is a list of strings as if they were passed as arguments to -e.

So:

environment:
  - ACCEPT_EULA=Y
  - SA_PASSWORD=YourStrong!Passw0rd

It's sort of subtle, but look over this section in the docs:

https://docs.docker.com/compose/environment-variables/#set-environment-variables-in-containers

CodePudding user response:

In your docker-compose.yml, have you tried:

  - ACCEPT_EULA=Y
  - SA_PASSWORD=YourStrong!Passw0rd

CodePudding user response:

Both responses above are fine, just a few more things:

SA_PASSWORD is deprecated instead use MSSQL_SA_PASSWORD

It is always nice to define .env files with the variables for instance:

sapassword.env

MSSQL_SA_PASSWORD=YourStrong!Passw0rd

sqlserver.env

ACCEPT_EULA=Y
MSSQL_DATA_DIR=/var/opt/sqlserver/data
MSSQL_LOG_DIR=/var/opt/sqlserver/log
MSSQL_BACKUP_DIR=/var/opt/sqlserver/backup

And in docker-compose.yml instance the env files the following way:

environment:
      - sqlserver.env
      - sapassword.env
  • Related