Search code examples
mysqldockercontainersdrupal-8

Docker containers for Drupal environment not linked


I'm learning Docker and experimenting by installing Drupal. The process is to create 2 containers with one running Apache and the other running MySQL.

I have the following docker-compose.yml file:

version: '3'
services:
  d8:
    container_name: d8
    image: drupal
    build: .
    ports:
      - "8585:80"
    links: 
      - d8mysql:mysql
  d8mysql:
    container_name: d8mysql
    image: mysql
    restart: always
    environment: 
      MYSQL_ROOT_PASSWORD: password
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: drupal
      MYSQL_USER: user
    ports:
      - "3306:3306"

And this Dockerfile:

FROM drupal:latest
FROM mysql:latest

WORKDIR /var/www/html

When I run docker-compose up, both my containers are successfully created, I can login and interact with both.

The problem is the drupal site is not connected to the database. During the setup process, I supply the database name, user and password as identified in the d8mysql section. I confirmed the database and user are in the container but drupal can not connect. Screenshot below:

enter image description here

Looking at the d8 section, I included links with an argument of the mysql container - which my research indicates should do the trick but doesn't.

I also checked the /etc/hosts file of the Apache container but it has no entry for the MySQL container which apparently should be there to facilitate the desired connection.


Solution

  • Since you have not defined tag for mysql image by default uses latest which for the time being is 8.0.18.

    Mysql 8 has changed its password authentication method which seems to cause incompatibility issues with drupal image. See github issue.

    One option is to install mysql_native_password plugin as proposed to the github issue above. I haven't tested it though.

    The other options which I have validated it is working is to use an older docker tag for mysql image, e.g mysql:5.7.28

    Working docker-compose:

    version: '3'
    services:
      d8:
        container_name: d8
        image: drupal
        ports:
          - "8585:80"
      d8mysql:
        container_name: d8mysql
        restart: always
        image: mysql:5.7.28
        environment: 
          MYSQL_ROOT_PASSWORD: password
          MYSQL_PASSWORD: password
          MYSQL_DATABASE: drupal
          MYSQL_USER: user
        # This is obsolete if you don't plan to connect to this db server from another machine outside docker network
        # ports:
        #   - "3306:3306"
    

    Notes:

    • I didn't use your Dockerfile, so I removed build section
    • Your Dockerfile does not do anything special. When defining multiple FROM instructions only the later is effective.
    • I removed link section as it is not required. Docker-compose by default creates an isolated network in which containers are added when stack starts. In this network, declared services names(d8,d8mysql for your case) act as domain names. Thus a container can access another via that name.