Docker containers for Drupal environment not linked

拈花ヽ惹草 提交于 2021-01-29 08:56:20

问题


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:

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.


回答1:


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.


来源:https://stackoverflow.com/questions/59392169/docker-containers-for-drupal-environment-not-linked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!