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

pel*_*can 0 drupal docker dockerfile docker-compose

我有以下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
Run Code Online (Sandbox Code Playgroud)

这是我的工作目录:

在此输入图像描述

这是 drupal dockerfile,我所做的就是拉取 drupal 镜像:

  1. 在此输入图像描述

这是 mariadb dockerfile:

在此输入图像描述

它会自动生成如下图所示的“db”子文件夹:

在此输入图像描述

我的问题是每次我在 localhost:8080 的 drupal UI 上输入 mariadb 时,它都会抛出以下错误: 在此输入图像描述

更新:

根据 @Tarun Lalwani 的回答,我的问题是,在 Drupal UI 中,我会输入我的用户名、密码和数据库名称,但如果您在 Drupal 屏幕截图中展开高级选项,您会看到主机名指向“ localhost”,而它应该指向 mariadb 数据库服务器的实际主机名,在 DOCKER WORLD 中,正在运行的容器的主机名是其服务名称,即“mariadb_service”,如 docker-compose.yml 文件中所示 - 请参阅屏幕截图。希望我不是唯一遇到这个问题的新手,并会帮助其他人,谢谢塔伦·拉尔瓦尼!

Tar*_*ani 5

您还需要为 Drupal 中的数据库设置主机名。该数据库主机将maria_service根据您文件中的服务名称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
    environment:
      DB_HOST: maria_service
      DB_USER: joel
      DB_PASSWORD: password
      DB_NAME: mariadb
      DB_DRIVER: mysql
Run Code Online (Sandbox Code Playgroud)