1

So after running docker-compose up I get the message Error establishing a database connection when visiting http://localhost:8000/

Output of 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

Is this right? Or should it only show one container since wordpress depends_on db?

So I am expecting to see my Wordpress site at localhost:8000.
Had imported the database making sure I sed to change all url to point to http://localhost.
Had also mounted ./html which contains my source files to container's /var/www/html.

Did I miss anything?

Folder Structure:

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

Some output of 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"

1 Answer 1

3

are you using "db" host to connect PHP (Wordpress? wp-config.php?) to your database instead of the usual "localhost"?.

Sign up to request clarification or add additional context in comments.

4 Comments

Good question. DB host is defined as define('DB_HOST', 'localhost'); Is that correct? I have no idea how to find the link created by docker.
Right, so changing it to define('DB_HOST', 'db'); worked :)
You saved me so much pain ! Too bad I can only upvote you once ^^
2018 and this still works! upvoted @Gonzalo! all yesterday I couldn't figure out how/why this had changed from my v1 docker-compose file to v3. makes complete sense now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.