Odyss3us
Odyss3us

Reputation: 6635

Symfony can't connect to MySQL Docker container

I have three Docker containers running on Mac OS sierra, namely web, mysql and mongo, and have linked both mongo and mysql into web, which is essentially a Ubuntu Xenail base, with Apache and PHP added.

I am currently mounting my local Symfony project into the web container, and that seems to be working fine, but when I try to interact with the DB in any way, I get:

An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

I've tried almost every combination of parameter values, but keep getting the same result.

I suspect it might have something to do with the way that I am linking the containers?

I'm in the process of learning Docker, so please excuse my limited knowledge.

Thanks!

Web dockerfile:

FROM ubuntu:xenial
MAINTAINER Some Guy <[email protected]>

RUN apt-get update && apt-get install -y \
apache2 \
vim \
php \
php-common \
php-cli \
php-curl \
php-mysql \
php-mongodb \
libapache2-mod-php \
php-gd

RUN mkdir -p /var/www/symfony.local/public_html
RUN chown -R $USER:$USER /var/www/symfony.local/public_html

RUN chmod -R 755 /var/www

COPY config/php/php.ini /usr/local/etc/php/
COPY config/apache/sites-available/*.conf /etc/apache2/sites-available/

RUN a2enmod rewrite

RUN a2dissite 000-default.conf
RUN a2ensite symfony.local.conf

EXPOSE  80

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Mysql dockerfile:

FROM mysql:5.7
MAINTAINER Some Guy <[email protected]>

# Set the root users password
ENV MYSQL_ROOT_PASSWORD password

# Copy over the DB dump to be run upon creation
COPY sql/ /docker-entrypoint-initdb.d

# Copy over the custom mysql config file
COPY config/ /etc/mysql/conf.d

EXPOSE 3306

Run commands:

docker run --name mongo -d mongo #Im making use of the official Mongo image
docker run --name mysql -v /usr/local/var/mysql:/var/lib/mysql -d someguy/local:mysql
docker run --name web -d -p 80:80 --link mysql:mysql --link mongo:mongo -v ~/Sites/symfony.local/:/var/www/symfony.local/public_html/ someguy/local:web

Symfony parameters.yml file:

parameters:
    database_host: mysql
    database_port: 3306
    database_name: gorilla
    database_user: root
    database_password: password

UPDATE:

So I've moved over to using docker-compose, but am still receiving the same error.

docker-compose.yml file

version: "2"
services:
  web:
    build: ./web
    ports:
      - "80:80"
    volumes:
      - ~/Sites/symfony.local/:/var/www/symfony.local/public_html/
    depends_on:
      - db
      - mongo
  mongo:
    image: mongo:latest
  mysql:
    image: mysql:latest
    ports:
        - "3306:3306"
    environment:
        MYSQL_ROOT_PASSWORD: password

Upvotes: 0

Views: 3513

Answers (1)

Eugen Mayer
Eugen Mayer

Reputation: 9896

An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

Means, it has nothing to do with your network per se - the links are just fine.

What you are lacking is the how the user has been created, if the user has been created https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L122 .. so actually without a host limitation per se.

The question in your case is, what is inside your "sql/" folder - those scripts are executed during the entrypoint.

Be sure to never use exitX in those scripts, they will interrupt the main script, see https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh#L151

Check your docker logs for mysql to ensure the script did not print you any warnings, use https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/docker-entrypoint.sh as an reference.

And last but not least, please use docker-compose. If you have issues with the timings ( mysql starting to slow and your web-container freaks out ), use a "wait for mysql" entrypoint in web:

#!/bin/bash
# this script does only exist to wait for the database before we fire up tomcat / standalone

RET=1
echo "Waiting for database"
while [[ RET -ne 0 ]]; do
    sleep 1;
    if [ -z "${db_password}" ]; then
        mysql -h $db_host -u $db_user -e "select 1" > /dev/null 2>&1; RET=$?
    else
        mysql -h $db_host -u $db_user -p$db_password -e "select 1" > /dev/null 2>&1; RET=$?
    fi
done

Set db_host, $user, $pasword accordingly using ENV or whatever suits you.

Upvotes: 1

Related Questions