Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 connection on Mysql docker container

I use docker for MySql and Adminer interface, the connection between Adminer and MySql works but the Symfony connection is refused. Symfony isn't in a container.

What can i do for the connection between Symfony 4 and MySql container works ?

this is my docker-compose.yml :

version: '3'
services:

  mysql:
    image: mysql:5.7
    volumes:
    - ./data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: "rootpwd"
      MYSQL_USER: 'testuser'
      MYSQL_PASSWORD: 'testpassword'

  adminer:
    image: adminer
    restart: always
    ports:
    - 8080:8080
    links:
      - mysql:db

volumes:
  mysql:

my .env on symfony 4:

# This file defines all environment variables that the application needs.
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE.
# Use ".env.local" for local overrides during development.
# Use real 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=825aa371242c34bdcc0c693ac4241bcf
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS='^localhost|example\.com$'
###< symfony/framework-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://testuser:[email protected]:3306/dbname

###< doctrine/doctrine-bundle ###

and my doctrine.yml :

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    #env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
        unix_socket: /tmp/mysql.sock
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

UPDATE


Well, thanks for your help it works, Symfony or more especially doctrine not found the container, like you said I exposed the Mysql port, but I have to make an other change : This line works for me :

DATABASE_URL=mysql://root:[email protected]:3306/dbname

and not this line :

DATABASE_URL=mysql://testuser:[email protected]:3306/dbname

In fact it seems like that my docker-compose up doesn't work very well and compose with old version, docker doesn't "see" my change on docker-compose.yml, so the password and user stay "root". I'm going to try this : https://forums.docker.com/t/how-to-delete-cache/5753/2 and this: https://github.com/docker/compose/issues/1487

Sometimes, database need to be delete => Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup. https://hub.docker.com/_/postgres/

like image 248
cisco_bro Avatar asked Nov 27 '18 14:11

cisco_bro


1 Answers

You need to expose database port like eg.

version: '3'
services:

  mysql:
    image: mysql:5.7
    volumes:
    - ./data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: "rootpwd"
      MYSQL_USER: 'testuser'
      MYSQL_PASSWORD: 'testpassword'
    ports:
        - 3306:3306
  adminer:
    image: adminer
    restart: always
    ports:
    - 8080:8080
    links:
      - mysql:db

volumes:
  mysql:

and then in .env file

DATABASE_URL=mysql://testuser:[email protected]:3306/dbname

Make sure that the ip address matches the IP of your docker host.

like image 131
breq Avatar answered Oct 10 '22 01:10

breq