我尝试使用环境变量来配置我的苗条应用程序。
码头工人-compose.yml
services:
web:
build:
context: .
dockerfile: Dockerfile
container_name: web_container
ports:
- 8001:80
volumes:
- ./:/src/app
depends_on:
- mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: pwd
mysql:
image: mysql:5.7
container_name: mysql_container
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: pwd
ports:
- 8002:3306
volumes:
- ./database/:/docker-entrypoint-initdb.d
在我的 php-di 配置中,我有:
'db.root-password' => \DI\env("MYSQL_ROOT_PASSWORD","root"),
'db.username' => \DI\env("MYSQL_USER","root"),
'db.password' => \DI\env("MYSQL_PASSWORD","root"),
'db.name' => \DI\env("MYSQL_DATABASE
PDO::class => function () {
var_dump(\DI\env("MYSQL_PASSWORD","root"));
var_dump(\DI\get('db.password'));
var_dump($_ENV['MYSQL_PASSWORD']);
return new PDO(...);
},
我尝试获取在 docker-compose 中声明的环境变量值来设置我的 PDO 对象。问题是 var_dumps 打印:
/srv/app/app/bootstrap.php:56:
object(DI\Definition\EnvironmentVariableDefinition)[84]
private 'name' => string '' (length=0)
private 'variableName' => string 'MYSQL_PASSWORD' (length=14)
private 'isOptional' => boolean true
private 'defaultValue' => string 'root' (length=4)
/srv/app/app/bootstrap.php:57:
object(DI\Definition\Reference)[84]
private 'name' => string '' (length=0)
private 'targetEntryName' => string 'db.password' (length=11)
/srv/app/app/bootstrap.php:58:string 'pwd' (length=9)
我无法使用 PHP-DI 功能获取环境变量值。有人可以帮助我吗?
谢谢