Q:

Apache with Docker shows a [2002] Can't connect to local MySQL server

I use Mac OSX and MAMP. 
I wanted to make a Docker Container for a web project, because it uses a php 5.5 version, and i didn't want to make changes on my MAMP. 
So here is the 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

This is the 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

And I made a little vhost :
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>

Then I make docker-compose up, I generate my containers. 
When I go to the console and 
 mysql -h localhost -P 3306 --protocol=tcp -u root -proot
It works perfectely ! 
But in my navigator I have a 2002 error ...
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
But I've modified my credentials ...
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);

Honestely, I don't understand ... :s 
Have you got an idea please ? 
Thanks. 

A:

You must change the app's host database config to the name of the database container that is linked to your app. 
Here, in your example, the container name is mysql, therefore you need this change:
-define('DB_HOST', 'localhost');
+define('DB_HOST', 'mysql');

