Directus in docker cannot connect to mysql database in docker

Viewed 1209

I have the following docker compose file. MySQL and phpmyadmin is working but directus cannot be initialised because it cannot connect to database.

version : '3'

services:
  db:
    image: mysql:8
    container_name: dev_mysql
  environment:
    MYSQL_USER: directus
    MYSQL_PASSWORD: pa55word
    MYSQL_ROOT_PASSWORD: pa55word
    MYSQL_DATABASE: directus
  volumes:
    - ./db_data:/var/lib/mysql
  ports:
    - 9000:3306
phpmyadmin:
  image: phpmyadmin/phpmyadmin
  container_name: dev_pma
  links:
    - db
  depends_on:
    - db
  environment:
    PMA_HOST: db
    PMA_PORT: 3306
    MYSQL_ROOT_PASSWORD: pa55word
  restart: always
  ports:
    - 8080:80
directus:
  container_name: directus
  image: directus/directus:v9.0.0-rc.59
  ports:
    - 8055:8055
  links:
    - db
  depends_on:
    - db
  environment:
    KEY: '255d861b-5ea1-5996-9aa3-922530ec40b1'
    SECRET: '6116487b-cda1-52c2-b5b5-c8022c45e263'

    DB_CLIENT: 'mysql'
    DB_HOST: 'db'
    DB_PORT: '3306'
    DB_DATABASE: 'directus'
    DB_USER: 'directus'
    DB_PASSWORD: 'pa55word'

    ADMIN_EMAIL: 'admin@directus.com'
    ADMIN_PASSWORD: 'pa55word'

The docker compose log message I got is

Starting dev_mysql ... done

Starting dev_pma ... done

Recreating directus ... done

Attaching to dev_mysql, dev_pma, directus

dev_mysql | 2021-04-17 08:39:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.

dev_mysql | 2021-04-17 08:39:14+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'

dev_pma | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.21.0.3. Set the 'ServerName' directive globally to suppress this message

dev_pma | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.21.0.3. Set the 'ServerName' directive globally to suppress this message

dev_pma | [Sat Apr 17 08:39:15.199501 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.16 configured -- resuming normal operations

dev_pma | [Sat Apr 17 08:39:15.199574 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

dev_mysql | 2021-04-17 08:39:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.

dev_mysql | 2021-04-17T08:39:15.116541Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1

dev_mysql | 2021-04-17T08:39:15.122954Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive

dev_mysql | 2021-04-17T08:39:15.138364Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.

directus | 08:39:15 ✨ Initializing bootstrap...

dev_mysql | 2021-04-17T08:39:16.230177Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.

dev_mysql | 2021-04-17T08:39:16.390962Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock

dev_mysql | 2021-04-17T08:39:16.419710Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...

dev_mysql | 2021-04-17T08:39:16.426595Z 0 [System] [MY-010232] [Server] XA crash recovery finished.

dev_mysql | 2021-04-17T08:39:16.527057Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.

dev_mysql | 2021-04-17T08:39:16.528010Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.

dev_mysql | 2021-04-17T08:39:16.539042Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.

dev_mysql | 2021-04-17T08:39:16.586905Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.

directus | 08:39:40 � Can't connect to the database

directus exited with code 1

Can anyone one please help to have a look? Thank you.

1 Answers

By default docker-compose will create a network where containers hostnames are based on their name. Normally container names are generated based on the service name in docker-compose.yml. In your case, you specify your own name for the services container.

Try changing DB_HOST: 'db' to

directus:
  ...
  environment:
    DB_HOST: 'dev_mysql'

https://docs.docker.com/compose/networking/

Also, links directive is deprecated so you should remove it. It is unnecessary in your file anyway as all services in your docker-compose.yml will have their own private network.

https://docs.docker.com/compose/compose-file/compose-file-v3/#links

Related