我使用环境变量创建了自己的 dockerfile (ubuntu:xenial)。这个 dockerfile 使用 php7.0-fpm php7.0-xml php7.0-mbstring php-mysql
dockerfiles 包含:
ENV MYSQL_HOST=192.168.0.2
ENV MYSQL_DBNAME=dbname_xyz
ENV MYSQL_USERNAME=username_xyz
ENV MYSQL_PASSWORD=password_xyz
...
RUN echo "clear_env = no" >> /etc/php/7.0/fpm/pool.d/www.conf
在 server.php 中,我尝试使用这些变量,但它们显然不为人所知
$host = $_SERVER["MYSQL_HOST"];
$dbname = $_SERVER["MYSQL_DBNAME"];
$username = $_SERVER["MYSQL_USERNAME"];
$password = $_SERVER["MYSQL_PASSWORD"];
$pdo = new \PDO("mysql:host=$host;dbname=$dbname", $username, $password);
运行显示错误:
FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: MYSQL_HOST in /var/webdav/server.php on line 4
执行:container-shell 显示正确的值
php -r "echo getenv('MYSQL_HOST');"
env | grep MYSQL
有什么建议我要改变吗?
在 AymDEV 的反馈之后
更新 20211215 在 piotrekr 的反馈之后更新 20211215
完整的泊坞窗文件:
FROM ubuntu:xenial
MAINTAINER me@whatever.us
# Changing WEBDAV_PASSWORD doesn't work
# MYSQL_x aren't known to server.php
ENV WEBDAV_USERNAME=admin
ENV WEBDAV_PASSWORD=admin
ENV MYSQL_HOST=192.168.0.2
ENV MYSQL_DBNAME=dbname_xyz
ENV MYSQL_USERNAME=username_xyz
ENV MYSQL_PASSWORD=password_xyz
# Defaults
WORKDIR /var/webdav
VOLUME /var/webdav/public
VOLUME /var/webdav/data
# Install zip
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y zip unzip php-zip
# Install nginx with php7 support
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y nginx php7.0-fpm php7.0-xml php7.0-mbstring php-mysql && \
rm -rf /var/lib/apt/lists/*
# Install SabreDAV
RUN php -r "readfile('http://getcomposer.org/installer');" > composer-setup.php && \
php composer-setup.php --install-dir=/usr/bin --filename=composer && \
php -r "unlink('composer-setup.php');" && \
composer require sabre/dav ~3.2.2 && \
rm /usr/bin/composer
# Set up entrypoint
COPY /scripts/install.sh /install.sh
# Configure nginx
COPY /config/nginx/default /etc/nginx/sites-enabled/default
COPY /config/nginx/fastcgi_params /etc/nginx/fastcgi_params
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
ln -sf /dev/stderr /var/log/nginx/error.log
# copy server.php for client -- sabredav communication
COPY /web/server.php /var/webdav/server.php
#make environment variables available to php
RUN echo "clear_env = no" >> /etc/php/7.0/fpm/pool.d/www.conf
#nginx will be process with PID=1
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
CMD /install.sh && service php7.0-fpm start && nginx
完整的 server.php
<?php
date_default_timezone_set('Europe/Berlin');
$baseUri = '/';
$host = $_ENV["MYSQL_HOST"];
$dbname = $_ENV["MYSQL_DBNAME"];
$username = $_ENV["MYSQL_USERNAME"];
$password = $_ENV["MYSQL_PASSWORD"];
$pdo = new \PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
require_once 'vendor/autoload.php';
$authBackend = new \Sabre\DAV\Auth\Backend\PDO($pdo);
$principalBackend = new \Sabre\DAVACL\PrincipalBackend\PDO($pdo);
$carddavBackend = new \Sabre\CardDAV\Backend\PDO($pdo);
$caldavBackend = new \Sabre\CalDAV\Backend\PDO($pdo);
$nodes = [
new \Sabre\CalDAV\Principal\Collection($principalBackend),
new \Sabre\CalDAV\CalendarRoot($principalBackend, $caldavBackend),
new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend),
];
$server = new \Sabre\DAV\Server($nodes);
if (isset($baseUri)) $server->setBaseUri($baseUri);
$server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
$server->addPlugin(new \Sabre\DAV\Browser\Plugin());
$server->addPlugin(new \Sabre\CalDAV\Plugin());
$server->addPlugin(new \Sabre\CardDAV\Plugin());
$server->addPlugin(new \Sabre\DAV\Sync\Plugin());
$server->exec();