Docker撰写LAMP数据库连接错误

Docker Compose LAMP Database connection error

所以在 运行 docker-compose up 之后,我在访问 http://localhost:8000/

时收到消息 Error establishing a database connection

docker ps -a 的输出:

➜  ~ docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
5a3c015efeec        dockercompose_wordpress   "docker-php-entryp..."   17 minutes ago      Up 16 minutes       0.0.0.0:8000->80/tcp     dockercompose_wordpress_1
4e46c85345d5        dockercompose_db          "docker-entrypoint..."   17 minutes ago      Up 16 minutes       0.0.0.0:3306->3306/tcp   dockercompose_db_1

这样对吗?或者它应该只显示一个容器,因为 wordpress depends_on db?

所以我希望在 localhost:8000 上看到我的 Wordpress 站点。
已导入数据库确保我 sed 将所有 url 更改为指向 http://localhost.
还将包含我的源文件的 ./html 挂载到容器的 /var/www/html.

我错过了什么吗?

文件夹结构:

Folder
|
|-db
| |-Dockerfile
| |-db.sql
|
|-html
| |- (Wordpress files)
|
|-php
| |-Dockerfile
|
|-docker-composer.yml

docker-composer.yml:

version: '3'

services:
  db:
    build:
      context: ./db
      args:
        MYSQL_DATABASE: coown 
        MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: coown 
      MYSQL_ROOT_PASSWORD: root

  wordpress:
    build:
      context: ./php
    depends_on:
      - db
    ports:
      - "8000:80"
    volumes:
      - ./html:/var/www/html

db/Dockerfile:

FROM mysql:5.7
RUN chown -R mysql:root /var/lib/mysql/

ARG MYSQL_DATABASE
ARG MYSQL_ROOT_PASSWORD

ENV MYSQL_DATABASE=$MYSQL_DATABASE
ENV MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD

ADD db.sql /etc/mysql/db.sql
RUN cp /etc/mysql/db.sql /docker-entrypoint-initdb.d

EXPOSE 3306

php/Dockerfile:

FROM php:7.0-apache
RUN docker-php-ext-install mysqli

docker-compose up的一些输出:

db_1         | 2017-06-12T19:21:33.873957Z 0 [Warning] CA certificate ca.pem is self signed.
db_1         | 2017-06-12T19:21:33.875841Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
db_1         | 2017-06-12T19:21:33.876030Z 0 [Note] IPv6 is available.
db_1         | 2017-06-12T19:21:33.876088Z 0 [Note]   - '::' resolves to '::';
db_1         | 2017-06-12T19:21:33.876195Z 0 [Note] Server socket created on IP: '::'.
db_1         | 2017-06-12T19:21:33.885002Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170612 19:21:33
db_1         | 2017-06-12T19:21:33.902676Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1         | 2017-06-12T19:21:33.902862Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1         | 2017-06-12T19:21:33.902964Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1         | 2017-06-12T19:21:33.903006Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1         | 2017-06-12T19:21:33.905557Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1         | 2017-06-12T19:21:33.910940Z 0 [Note] Event Scheduler: Loaded 0 events
db_1         | 2017-06-12T19:21:33.911310Z 0 [Note] mysqld: ready for connections.
db_1         | Version: '5.7.18'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
db_1         | 2017-06-12T19:21:33.911365Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check.
db_1         | 2017-06-12T19:21:33.911387Z 0 [Note] Beginning of list of non-natively partitioned tables
db_1         | 2017-06-12T19:21:33.926384Z 0 [Note] End of list of non-natively partitioned tables
wordpress_1  | 172.18.0.1 - - [12/Jun/2017:19:28:39 +0000] "GET / HTTP/1.1" 500 557 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"

您是否使用 "db" 主机连接 PHP (Wordpress?wp-config.php?) 到您的数据库,而不是通常的 "localhost"?