我使用Mac OSX和MAMP。 我想为一个Web项目制作一个Docker容器,因为它使用的是PHP 5.5版本,并且我不想在MAMP上进行更改。 这是docker-compose.yml
version: '2'
services:
www:
build: "./docker/apache"
volumes:
- .:/var/www
- ./docker/silexx.conf:/etc/apache2/sites-enabled/silexx.conf
working_dir: "/var/www"
command : "bash -c 'source /etc/apache2/envvars; apache2 -DFOREGROUND'"
ports:
- 80:80
links:
- mysql
mysql:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'silexx_dev'
MYSQL_ROOT_PASSWORD: 'root'
ports:
- 3306:3306
pma:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
restart: always
ports:
- 8080:80
volumes:
- /sessions
这是Dockerfile:
FROM debian:jessie
RUN apt-get update
RUN apt-get install -y --force-yes apt-transport-https apache2 php5 php5-cli libicu-dev php5-gd php5-curl php5-mysql php5-memcached php5-intl php5-json php5-mcrypt curl sudo
RUN a2enmod rewrite
# install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get install -y --force-yes make git locales gcc g++
RUN echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
RUN locale-gen
ENV LC_ALL en_US.UTF-8
RUN rm /etc/apache2/sites-*/*
COPY default /etc/apache2/sites-available/default.conf
RUN a2ensite default
CMD "source /etc/apache2/envvars && apache2 -DFOREGROUND"
ENV LANG en_US.UTF-8
CMD "/etc/init.d/mysql start"
# forward request and error logs to docker log collector
RUN ln -sf /proc/1/fd/1 /var/log/apache2/access.log
RUN ln -sf /proc/1/fd/2 /var/log/apache2/error.log
EXPOSE 80
我做了一个小鬼:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/var/www/core/"
ServerName demo.silexx.local
ServerAlias demo.silexx.local
#ErrorLog "logs/silexx.com-error_log"
#CustomLog "logs/silexx.com-access_log" common
</VirtualHost>
然后我将docker-compose起来,生成容器。
当我进入控制台并 mysql -h localhost -P 3306 --protocol = tcp -u root -proot
它完美地工作!
但是在我的导航器中,我遇到了2002错误... SQLSTATE [HY000] [2002]无法通过套接字'/var/run/mysqld/mysqld.sock'(2)连接到本地MySQL服务器
但是我修改了我的凭据...
define('DB_HOST', 'localhost');
define('DB_INTRANET', 'silexx_dev');
define('DB_INTRANET_UTI', 'root');
define('DB_INTRANET_PWD', 'root');
$oPDO = new PDO ('mysql:host=' . DB_HOST . ';dbname=' . DB_INTRANET, DB_INTRANET_UTI, DB_INTRANET_PWD);
$oPDO -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
老实说,我听不懂...:s 请问您有个主意吗?
谢谢。
答案 0 :(得分:1)
您必须将应用程序的host
数据库配置更改为链接到应用程序的数据库容器的名称。
在您的示例中,容器名称为mysql
,因此您需要进行以下更改:
-define('DB_HOST', 'localhost');
+define('DB_HOST', 'mysql');