I am trying make one Docker image with Apache and PHP linked to another image with MySQL. Apache and PHP works great on first look and MySQL container is successfully connected to third container with phpmyadmin and it works. But when I am trying to create new connection with PDO it returns SQLSTATE[HY000] [2002] No such file or directory.
try {
$msql = new \PDO('mysql:host=localhost;dbname=test', 'root', 'password');
echo 'Connected';
} catch (\PDOException $e) {
echo 'Error: ' . $e->getmessage();
}
phpinfo() returns pdo_mysql (mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $). PHP version is 7.0.22.
This is Dockerfile:
FROM ubuntu:16.04
RUN apt-get update
# Apache
RUN apt-get -y install apache2
# PHP
RUN apt-get -y install php libapache2-mod-php php-mcrypt php7.0-mysql
RUN phpenmod pdo_mysql
# phpmyadmin
#RUN apt-get -y install phpmyadmin
# Start Apache
CMD /etc/init.d/mysql start -D FOREGROUND
CMD /usr/sbin/apache2ctl -D FOREGROUND
EXPOSE 80
I used mysql:5.7.13 image for mysql container with exposed 3306 port and both containers are in the same network. Any ideas where is the problem?