8

I got a docker-compose setup with two containers: One is the php/apache service and the other container is the database (mysql).

Here is my docker-compose.yml

version: '2'
services:
   app:
     depends_on:
       - db
     links:
       - db:mysql
     build: .
     image: app
     ports:
       - "80:80"
     restart: always
     links:
       - db:db
     volumes:
       - ../:/var/www/html/

   db:
     image: mysql:latest
     restart: unless-stopped
     volumes:
       - ./db_data:/var/lib/mysql
       - ./databaseDumps:/tmp/databaseDumps
     environment:
       MYSQL_USER: "myApp"
       MYSQL_PASSWORD: "root"
       MYSQL_ROOT_PASSWORD: "root"
       MYSQL_DATABASE: "myAppDatabase"
       MYSQL_ROOT_HOST: "%"

And here is my app Dockerfile:

FROM php:7-apache

COPY prefilled_files/000-default.conf /etc/apache2/sites-available/000-default.conf
RUN apt-get -qq update
RUN apt-get -qq -y install libpng-dev curl git nano vim zip unzip mysql-client libmysqlclient-dev
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
RUN npm install -g bower
RUN npm install -g gulp
RUN docker-php-ext-install pdo pdo_mysql gd mysqli
EXPOSE 8080 80

The main problem is, the mysql database and the app container working well, and I can connect to the mysql database from the app container via

$root@app: mysql -h db -u myApp -p

BUT

if I try to execute composer install on my symfony project, following error message appears:

 [Doctrine\DBAL\Driver\PDOException]        
  SQLSTATE[HY000] [2002] Connection refused  



  [PDOException]                             
  SQLSTATE[HY000] [2002] Connection refused  

Here are my parameters of my app:

parameters:
    database_driver: pdo_mysql
    database_host: db
    database_port: 3306
    database_name: myAppDatabase
    database_user: myApp
    database_password: root

Why is this happening? I've read through several forums and sites but nothing helped. I tried the following solutions and nothing helped:

  • clearing symfony cache ;)
  • expose 3306 on mysql container
  • link app and mysql container together
  • removing all images and containers from my computer and reinstalled everything
  • I tried on windows and on ubuntu 17.04. Same behavior

connecting to a docker-compose mysql container denies access but docker running same image does not

docker-compose wordpress mysql connection refused

UPDATE:

I tried to access my database with a little php script from https://gist.github.com/chales/11359952 . PHP/My Script can actually connect to the database, so the problem has to be in with my composer install or in doctrine or in my configuration of symfony.

tl;dr

  • 2 Docker container via docker compose
  • I can access the database via mysql command on the app container but not over composer install. Why?
Rubinum
  • 547
  • 3
  • 18
  • Instead of `database_host: db` try with `database_host: foo`. The error should be unknown host. But if the error continues the same, the problem is that your config file is another. – Robert Jul 02 '17 at 17:58
  • @Robert I got this when I try "database_host: foo": `[Doctrine\DBAL\Exception\ConnectionException] An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known ` – Rubinum Jul 03 '17 at 06:14
  • Is there any chance that composer is running so fast when MySQL is not up yet? – Robert Jul 03 '17 at 11:32
  • Good catch, I tried to wait 1 hours after docker-compose up but it didn't work either. I got a SSD so I didn't think, that this is a problem. – Rubinum Jul 04 '17 at 07:38

1 Answers1

7

I think it's the way it's trying to find the container via the host name 'db', I've found that on the machine running docker, it doesn't seem to pick up the names of the guest containers (could be some DNS config you could change) but the way I've worked round it is to find the IP address of the MySQL container. I have docker_db_1 as the container name for MySQL, so I run (assuming *nix)

docker inspect docker_db_1 | grep IPAddress

Which in my case gives me

    "SecondaryIPAddresses": null,
    "IPAddress": "",
            "IPAddress": "172.18.0.2",

And I use this IP address (172.18.0.2) to connect to rather than db.

a_guest
  • 34,165
  • 12
  • 64
  • 118
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Also lookout if you application has more than one database connection defined in the config.yml. Symfony will also lookout for that connections and will test them. – Rubinum Aug 23 '17 at 13:05