The50
The50

Reputation: 1168

Docker can't connect to MySQL running docker-compose up

I am using this Docker config: https://github.com/romaricp/kit-starter-symfony-4-docker

I start the environment by using:

docker-compose build

followed by:

docker-compose up -d

Everything is running fine but there is a problem with MySQL service. I get an Symfony error:

"An exception occurred in driver: could not find driver"

as well as PMA error when I try to login to DB:

mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve

Any idea how could I make it work?

docker-compose.yml:

version: '3'
services:
apache:
    build: .docker/apache
    container_name: sf4_apache
    ports:
      - 80:80
    volumes:
      - .docker/config/vhosts:/etc/apache2/sites-enabled
      - .:/home/wwwroot/sf4
    depends_on:
      - php

mysql:
    image: mysql
    container_name: sf4_mysql
    volumes:
        - .docker/data/db:/var/lib/mysql
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: sf4
        MYSQL_USER: sf4
        MYSQL_PASSWORD: sf4

php:
    build: .docker/php
    container_name: sf4_php
    volumes:
      - .:/home/wwwroot/sf4
    environment:
      - maildev_host=sf4_maildev
    depends_on:
      - maildev
      - mysql

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: sf4_phpmyadmin
    ports:
        - 8080:80
    links:
        - mysql

maildev:
    image: djfarrelly/maildev
    container_name: sf4_maildev
    ports:
      - 8001:80

Also I opened the mysql logs and I see this:

2019-04-07T12:00:30.943414Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.15' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.

Maybe the port is wrong and that's why I can't connect?

Upvotes: 1

Views: 4377

Answers (3)

aram yazdanpanah
aram yazdanpanah

Reputation: 75

I think you're having trouble linking the containers. I suggest that you use a custom network instead of linking. That all containers can see each other

Upvotes: 1

Abdelkarim EL AMEL
Abdelkarim EL AMEL

Reputation: 1533

For phpmyadmin service, i think you should set the PMA_HOST and PMA_PORT environment variables like this:

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: sf4_phpmyadmin
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    ports:
        - 8080:80
    links:
        - mysql

Your mysql container should have the command instruction at the start to set the authentication plugin (there is an issue with the connectors with mysql 8), more details here

mysql:
    image: mysql
    container_name: sf4_mysql
    command: "--default-authentication-plugin=mysql_native_password"
    volumes:
        - .docker/data/db:/var/lib/mysql
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: sf4
        MYSQL_USER: sf4
        MYSQL_PASSWORD: sf4

in your Symfony app, the connection string is located in .env file, and it should have the following format :

DATABASE_URL=mysql://mysql_user:mysql_user_password@mysql_host:mysql_port/db_name

mysql_user: your mysql user (ex: root)

mysql_user_password: the user's password (ex: root)

mysql_host: is should contain your mysql container service name located in your docker-compose.yml file (mysql in this case)

mysql_port: mysql container internal port (3306, in this case)

db_name: the database you want to connect to.

the DATABASE_URL can look like this :

DATABASE_URL=mysql://root:root@mysql:3306/sf4

stop your containers after these changes and start them up again.

Hope this will help.

Upvotes: 1

nologin
nologin

Reputation: 1442

You have to expose the mysql port 3306:

  mysql:
    image: mysql
    container_name: sf4_mysql
    volumes:
    - .docker/data/db:/var/lib/mysql
    restart: always
    ports:
    - "3306:3306"
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: sf4
        MYSQL_USER: sf4
        MYSQL_PASSWORD: sf4
    networks:
    - default

Upvotes: 1

Related Questions