Env file flag not working

I have a CI app running on Docker but when I run the container locally it redirects me to the live webpage. This is because in ./app/Config/App.php the baseURL is based on an environment variable:

public $baseURL = 'http://localhost:8080/';


    public function __construct() {
        parent::__construct();

        $this->setBaseUrl(); // Set the Base URL
    }

    protected function setBaseUrl() {
        switch ($_ENV['CI_ENVIRONMENT']) {
            case 'development':
                $this->baseURL = "http://localhost:8080/";
                break;
            default:
                $this->baseURL = "[live-hostname]";
                break;
        }
    }

in my root directory I have an .env file which contains the variable and has it set to ‘production’.
Now when I try to run docker compose --env-file ./config/.env.dev up --build it doesn’t change it, I don’t even get an error message or anything. I still get redirected to the live page and running docker compose config --environment still show the variable being set to production…

I have read the docs and googled several years old posts and I just can’t figure out what’s going on.
Please help.

The --env-file is not for setting variables in container,s but for setting variables that you can refer to in a compose file. It is basically an alternative .env file.

We discussed it couple of times like here

You can search for --env-file on the forum for more posts.

You can read this too

so there’s no real easy way to tell docker what environment you’re in as you impose docker compose as an argument? I’d have to replace the one .env every time?

I’m not sure what you mean. Please, describe how you interpret how the parameter works, so we know what we need to explain. The linked topic and the documentation should also explain how you can use --env-file with multiple env files. Note that the --env-file option and env_files in a compose file are for two different purposes, but you can have references in a compose file to variables defined in an env file. The file defined in --env-file is just not loaded directly into containers.

Since you haven’t shared the relevant parts of your compose file, we can’t tell if you use it correctly.

You can also check this page:

and also the other subpages of “Use environment variables” in the navigation bar.

Well I realize now that it just doesn’t work how I interpreted how it works. my composer currently looks like this:

services:
  web:
    image: apache-with-modules
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www/custom
      - ./docker-data/apache2/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf
    depends_on:
      - db
    develop:
      watch:
        - path: ./
          target: /var/www/custom
          action: sync+restart

  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: ***
      MYSQL_DATABASE: ***
      MYSQL_USER: ***
      MYSQL_PASSWORD: ***
    ports:
      - "3306:3306"
    volumes:
      - ./docker-data/mysql-data:/var/lib/mysql


  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "8081:80"
    environment:
      PMA_HOST: db
      UPLOAD_LIMIT: 300M
      MYSQL_ROOT_PASSWORD: ***
    depends_on:
      - db

and I have two .env files:

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

 CI_ENVIRONMENT = production

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------


database.default.hostname = 127.0.0.1
database.default.database = ***
database.default.username = ***
database.default.password = ***
database.default.DBDriver = MySQLi
#database.default.DBPrefix =
database.default.port = 3306
#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

 CI_ENVIRONMENT = development

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = db
database.default.database = ***
database.default.username = ***
database.default.password = ***
database.default.DBDriver = MySQLi
#database.default.DBPrefix =
database.default.port = 3306

And what I basically expected to happen is that .env gets replaced by .env.dev but that’s not the case.

After your comment I tried playing around with the environment attribute in the composer which allowed me to give App.php the variable it needs to dedicate a baseURL, but I couldn’t do the same for the database variables. It spits out an error page saying it can’t connect to the database and the $_Server variables show the production variables from .env,

After extensive research I just couldn’t find an answer so I’ll just resort to copying and pasting the contents of the env files back and forth when I push and pull.

Since your compose file has no varable reference at all, it is not surprising that the .env.dev is not used. Again, the .env file or anything you set as a parameter of docker compose to override the default is only for using variables in the compose file. Not in containers. You have to refer those variables in your compose file. There are examples in the documentation I linked to demonstrate it. It is in fact the first thing on the page I linked in my previous post.

If you PHP app read any of the variables in your .env file, it is possible the PHP app reads it directly since you mounted the project folder.

So if you want your variables to be available in containers, you have two options.

Option 1: Use the env_file option in the compose file combined with a variable passed to the compose file:

services:
  servicename:
    env_file: .env.${MODE}
MODE=dev docker compose up -d

Option 2: refer to individual variables in the compose file under the “environment” section the same way I did in the above example to refer to MODE.

.env.dev

VARNAME=value
services:
  servicename:
    environment:
      VARNAME: ${VARNAME}
docker compose --env-file .env.dev up -d