Search code examples
mysqldockerdoctrine-ormsymfony

Symfony 3 in Docker - Weird Connection Refused error


Working on a Symfony 3 project.

All my CLI doctrine commandos concerning the database work, like:

doctrine:database:create or doctrine:schema:update --force

Weirdly enough, I'm trying to get some data from my repository in my controller method, but there i seem to bounce on a Connection refused error when I try to navigate to the page in question in my browser.

Below some configs:

config.yml part

doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  utf8mb4
        default_table_options:
          charset: utf8mb4
          collate: utf8mb4_unicode_ci

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

parameters.yml

parameters:
    database_host: 127.0.0.1
    database_port: 3306
    database_name: db_name
    database_user: db_user
    database_password: db_pw

** Controller **

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
/**
 * @Route("/", name="homepage")
 */
public function indexAction()
{
    return $this->render('default/index.html.twig');
}

/**
 * @Route("/roedel", name="roedel")
 */
public function roedelAction()
{
    return $this->render('default/roedel.html.twig');
}

/**
 * @Route("/territorium", name="territorium")
 */
public function territoriumAction()
{
    return $this->render('default/territorium.html.twig');
}

/**
 * @Route("/dejacht", name="jacht")
 */
public function jachtAction()
{
    $artists = $this->getDoctrine()
        ->getRepository('AppBundle:Artist')
        ->findAll();

    return $this->render('default/jacht.html.twig', [
        'artists' => $artists
    ]);
}
}

Extra information:

  • Using docker as this is a testcase, though I'm not sure if this should matter since CLI commandos for doctrine do work.

Solution

  • When you're trying to run Symfony's console command against MySQL that runs inside a docker container, then there are two possible scenarios.

    You run a symfony command from your local machine, e.g. Mac, Windows, etc. - then the DB host has to be your docker machine IP (typically 192.168.99.100, but depend on your configuration) and you have to have properly mapped port 3306 for mysql container - more about port mapping https://docs.docker.com/compose/compose-file/#/ports.

    You run a symfony command from the "web app" container - then the DB host is set to mysql, and you have to have properly configured network between the "web app" and mysql containers - more about networks https://docs.docker.com/compose/compose-file/#/networks

    More convenient, for me, would be the second approach, because you don't have to alter DB parameters...