0

My docker-compose looks like:

version: '3.4'

services:
php-fpm:
    build:
      context: ./docker
      dockerfile: Dockerfile
      target: php-fpm
    ports:
      - 443:443
    volumes:
      - .:/var/www/html
    env_file:
      - .env
  mysql:
    image: mysql:8.0
    command:
      - --default-authentication-plugin=mysql_native_password
    volumes:
      - ./mysql-data:/var/lib/mysql
    env_file:
      - .env
    ports:
      - 3306:3306

My config/packages/doctrine.yaml looks like:

doctrine:
  dbal:
    dbname: testdb
    host: 'mysql'
    port: 3306
    user: root
    password: root
    driver: pdo_mysql
    server_version: '8.0'
    unix_socket: /tmp/mysql.sock

My .env file looks like:

#SQL
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=testdb
MYSQL_USER=user
MYSQL_PASSWORD=pass

When I am trying to execute the following command:

sudo docker-compose exec php-fpm bin/console doctrine:migrations:migrate

I am getting the following error:

In AbstractMySQLDriver.php line 112:                                 
  An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused  
In Exception.php line 18:
  SQLSTATE[HY000] [2002] Connection refused  
In PDOConnection.php line 38:
  SQLSTATE[HY000] [2002] Connection refused  

Why?

PS. When I bash into mysql container and type command:

mysql -u root -proot

Everything is fine!

  • Your containers do not see each other. Under `php-fpm`, add `links` with `- mysql:mysql` – Reqven Feb 20 '21 at 22:45
  • Both containers are in same network because of docker-compose. You don't need *links*. Are both containers running? What is output of **docker ps** after you run **docker-compose up** – Pavol Velky Feb 20 '21 at 23:29
  • So you are running those commands outside of the container and your local system does not know how to connect to your database. You could ssh into the container then run the migration. `docker exec -it containername /bin/bash` then `php bin/console doctrine:migrations:migrate` – Robert Saylor Feb 21 '21 at 13:56
  • this might help: https://stackoverflow.com/questions/33001750/connect-to-mysql-in-a-docker-container-from-the-host – Robert Saylor Feb 21 '21 at 14:01

0 Answers0