2

I have 2 Docker containers using the following configuration:

version: '2'

volumes:
  db_data: {}

services:

  db:
    container_name: database-mysql-5.7
    build:
      context: ./docker/db/
    ports:
      - "3321:3306"
    volumes:
      - db_data:/var/lib/mysql

  web:
    container_name: apache-symfony-4
    build:
      context: ./docker/web/
    image: php
    links:
      - db
    depends_on:
      - db
    ports:
      - "8021:80"
    volumes:
      - ./app/:/var/www/symfony

I can connect to the db container from my local host (Mac OS) and I can connect to the db container (and execute sql statements successfully) from the web container. Additionally, when I exec to the web container I can execute doctrine:migrations:diff commands successfully.

However, when I try and run code like the following:

$User = $this->getDoctrine()
                ->getRepository(User::class)
                ->findAll();

I receive the following error:

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

octrine\DBAL\Exception\ConnectionException:
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused

  at vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:108
  at Doctrine\DBAL\Driver\AbstractMySQLDriver->convertException('An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused', object(PDOException))
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:176)
  at Doctrine\DBAL\DBALException::wrapException(object(Driver), object(PDOException), 'An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused')
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:161)
  at Doctrine\DBAL\DBALException::driverException(object(Driver), object(PDOException))
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php:47)

Here is the contents of my .env file

# This file is a "template" of which env vars need to be defined for your application
# Copy this file to .env file for development, create environment variables when deploying to production
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=7c2deaf7464ad40b484d457e02a56918
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS=localhost,example.com
###< symfony/framework-bundle ###

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://hereswhatsontap:####@db:####/hereswhatsontap
###< doctrine/doctrine-bundle ###

Additionally, if I try and use the DBAL it returns the same error:

/**
     * @Route("/test")
     */
    public function testAction(Connection $connection){
        $users = $connection->fetchAll('SELECT * FROM users');

        return $this->render("startup/startup.html/twig", [
            "NewUser" => $users
        ]);
    }

Attempts to use the root username/password are also failing.

Ok, last additional data (I promise :)

Executing the following code works fine from within the /public folder of Symfony

<?php
$dsn = 'mysql:host=db;dbname=hereswhatsontap;';
$username = 'hereswhatsontap';
$password = '####';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

$dbh = new PDO($dsn, $username, $password, $options);

$sql = "SELECT * from user";
$statement = $dbh->prepare($sql);
$statement->execute();
$users = $statement->fetchAll(2);
print_r($users);
?>
2
  • can you copy your .env configuration ? Commented Mar 19, 2018 at 15:14
  • Added my .env file Commented Mar 20, 2018 at 2:36

2 Answers 2

4

Thanks a lot for your comment, it saved me some further hours of research lol. I would just add for newbies in Docker like me, that if you want to get your container IP you can run the following command (so that you won't have to look for in the whole configuration dump you get by using only inspect):

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' YOURCONTAINERID

Thanks again!

Sign up to request clarification or add additional context in comments.

Comments

1

This issue was caused by a full lack of understanding for me on how the containers communicate amongst each other.

The web container and the db container are essentially on their own 'vpn' network which means that the web container can communicate with db directly and would then use the internal port (3306) based on my configuration.

So removing the port (which I had as the external port) from my .env files' DATABASE_URL value was the answer.

DANG!

1 Comment

4 hours into that problem and this answer solve my problem too, oh god thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.