从.env或dockerFile/docker-compose.yml获取env变量,进行配置,php以连接到mysql数据库 [英] Get env variables from .env or dockerFile/docker-compose.yml, to config,php to connect to mysql database

查看:82
本文介绍了从.env或dockerFile/docker-compose.yml获取env变量,进行配置,php以连接到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在docker中定义一个env变量,而不是对db_username和密码进行硬编码.我是Docker的新手,所以我仍然需要了解一些内容.

Instead of hardcoding the db_username and password, I want to define a env variable in docker. I am new to Docker, so some stuff I still need to learn more about.

我要问的另一个问题是,如果其他人想要运行我的容器(例如从github克隆),他们将不得不自行设置env变量以连接到数据库,对吗?创建一个.env之类的东西.

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? Create a .env and stuff like that..

config.php

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

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

./frontend中的Dockerfile

Dockerfile in ./frontend

FROM php:7.2-apache

RUN docker-php-ext-install mysqli

WORKDIR /var/www/html

COPY . /var/www/html/

./backend中的Dockerfile

Dockerfile in ./backend

FROM mysql:5.7

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

我不知道它现在的方式是否是从env文件获取变量,当我使用docker-compose up运行并转到192.168.99.100时,我的应用程序正在运行,但是我得到了

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

在.yml文件中,我认为我不需要设置环境变量,但是我正在测试是否可以将那些变量直接放置到config.php中,对于将运行我的容器的人来说,只需要运行"docker-compose up"而不设置所有内容.

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.

警告:mysqli_connect():(HY000/1045):拒绝用户访问的'dbuser'@'172.17.0.3'(使用密码:是)/var/www/html/config.php,第8行

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

我不知道该怎么解决.因此,基本上,这是两个问题,为什么会出现此错误,以及我是否正确执行了环境变量.

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.

推荐答案

我已经解决了这个问题您需要添加变量PHP_FPM_CLEAR_ENV = no

I have solved this problem You need to add variable PHP_FPM_CLEAR_ENV=no

environment:
  - PHP_FPM_CLEAR_ENV=no

之后一切正常!

这篇关于从.env或dockerFile/docker-compose.yml获取env变量,进行配置,php以连接到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆