简体   繁体   English

从 .env 或 dockerFile/docker-compose.yml 获取环境变量,到 config,php 以连接到 mysql 数据库

[英]Get env variables from .env or dockerFile/docker-compose.yml, to config,php to connect to mysql database

Instead of hardcoding the db_username and password, I want to define a env variable in docker.我想在 docker 中定义一个 env 变量,而不是硬编码 db_username 和密码。 I am new to Docker, so some stuff I still need to learn more about.我是 Docker 的新手,所以有些东西我仍然需要了解更多。

Another question i want to ask is, if someone else want to run my container, like clone from github, they will have to set themselves the env variables to connect to db right?我想问的另一个问题是,如果其他人想要运行我的容器,比如从 github 克隆,他们将不得不为自己设置 env 变量以连接到数据库,对吗? Create a .env and stuff like that..创建一个 .env 之类的东西..

config.php配置文件

<?php

define('DB_SERVER', getenv('DB_SERVER'));
define('DB_USERNAME', getenv('DB_USERNAME'));
define('DB_PASSWORD', getenv('DB_PASSWORD')); 
define('DB_NAME', getenv('DB_NAME'));

$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

if ($link === false) {
    die ("ERROR: Could not connect. " . mysqli_connect_error());
}

?>

.env .env

DB_SERVER=db
DB_USERNAME=dbuser
DB_PASSWORD=dbpassword
DB_NAME=company

docker-compose.yml docker-compose.yml

db:
  build: ./backend
  restart: always
  ports:
    - "3306:3306"
  volumes:
    - /var/lib/mysql
  environment:
    - MYSQL_ROOT_PASSWORD=dbpassword
php:
  build: ./frontend
  ports:
    - "80:80"
  volumes: 
    - ./frontend:/var/www/html
  environment:
    - DB_SERVER=${DB_SERVER}
    - DB_USERNAME=${DB_USERNAME}
    - DB_PASSWORD=${DB_PASSWORD}
    - DB_NAME=${DB_NAME}
  env_file: ./.env
  links:
    - db

Dockerfile in ./frontend ./frontend 中的 Dockerfile

FROM php:7.2-apache

RUN docker-php-ext-install mysqli

WORKDIR /var/www/html

COPY . /var/www/html/

Dockerfile in ./backend ./backend 中的 Dockerfile

FROM mysql:5.7

COPY ./demo.sql /docker-entrypoint-initdb.d

I don't know if the way it is right now, is getting the variables form the env file, when i run with the docker-compose up, and go to 192.168.99.100, my application is working, but i get我不知道现在的方式是否从 env 文件中获取变量,当我使用 docker-compose up 运行并转到 192.168.99.100 时,我的应用程序正在运行,但是我得到了

In the .yml file, i think i don't need the environment variables set, but i was testing if i could get that variables, directly put into the config.php, for someone who will run my container, only need to run "docker-compose up" and not set everything.在 .yml 文件中,我认为我不需要设置环境变量,但我正在测试是否可以获取该变量,直接放入 config.php,对于将运行我的容器的人来说,只需要运行“ docker-compose up”,而不是设置所有内容。

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'dbuser'@'172.17.0.3' (using password: YES) in /var/www/html/config.php on line 8警告:mysqli_connect():(HY000/1045):第 8 行 /var/www/html/config.php 中的用户 'dbuser'@'172.17.0.3'(使用密码:YES)访问被拒绝

I don't know how to solve that.我不知道如何解决这个问题。 So basically, it's two questions, why this error, and if I'm doing the environment variables right.所以基本上,这是两个问题,为什么会出现这个错误,以及我是否正确地设置了环境变量。

我设法让它工作,我真的不知道我改变了什么,但基本上我删除了environment:php容器,并移动到db容器,使用来自 dockerhub 的 mysql 图像环境变量。

I have solved this problem You need to add variable PHP_FPM_CLEAR_ENV=no我已经解决了这个问题你需要添加变量 PHP_FPM_CLEAR_ENV=no

 environment: - PHP_FPM_CLEAR_ENV=no

After that everything is ok!之后一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM