docker-compose drupal不接受我的数据库名称,并在下面抛出错误

我有以下docker-compose.yml文件:

 version: '3' services: maria_service: build: ./db_maria restart: always environment: MYSQL_DATABASE: mariadb MYSQL_USER: joel MYSQL_PASSWORD: password MYSQL_ROOT_PASSWORD: password volumes: - ./db:/var/lib/mysql drupal_service: build: ./website restart: always ports: - 8080:80 volumes: - /var/www/html/modules - /var/www/html/profiles - /var/www/html/themes # this takes advantage of the feature in Docker that a new anonymous # volume (which is what we're creating here) will be initialized with the # existing content of the image at the same location - /var/www/html/sites depends_on: - maria_service 

这是我的工作目录:

在这里输入图像说明

这里是drupal dockerfile,我正在做的是拉动drupal图像:

  1. 在这里输入图像说明

这里是mariadb dockerfile:

在这里输入图像说明

它会自动生成这个“db”子文件夹见下面的图片:

在这里输入图像说明

我的问题是每当我在localhost:8080的drupal UI上inputmariadb时,它会在下面引发这个错误: 在这里输入图像说明

更新:

基于@Tarun Lalwani的回答,我的问题是,在Drupal的用户界面,我会input我的用户名,密码和数据库名称,但是如果你展开那个Drupal截图的高级选项,你会看到HOSTNAME指向“ localhost“,当它应该指向在DOCKER WORLD中的mariadb数据库服务器的实际主机名时,正在运行的容器的主机名是ITS SERVICE NAME,例如在docker-compose.yml文件中看到的”mariadb_service“ – 参见截图。 希望我不是唯一的新手碰到,并会帮助别人,谢谢塔伦Lalwani!

您还需要在Drupal中为DB设置主机名。 这个数据库主机将根据您docker-compose.yml文件的服务名称docker-compose.yml 。 这需要通过扩展高级选项来完成

高级配置

使用环境variables

您也可以尝试设置这些设置的环境variables

 version: '3' services: maria_service: build: ./db_maria restart: always environment: MYSQL_DATABASE: mariadb MYSQL_USER: joel MYSQL_PASSWORD: password MYSQL_ROOT_PASSWORD: password volumes: - ./db:/var/lib/mysql drupal_service: build: ./website restart: always ports: - 8080:80 volumes: - /var/www/html/modules - /var/www/html/profiles - /var/www/html/themes # this takes advantage of the feature in Docker that a new anonymous # volume (which is what we're creating here) will be initialized with the # existing content of the image at the same location - /var/www/html/sites depends_on: - maria_service environment: DB_HOST: maria_service DB_USER: joel DB_PASSWORD: password DB_NAME: mariadb DB_DRIVER: mysql