0

I am trying to setup my environment to build a wordpress site but docker-compose fails with connection refuded.

I sent more than 2 days trying to figure out, browsing the web but in vain. I tried solutions available on stack overflow, still in vain.

This is my my docker-compose

version: '3.7'
services:
  wordpress:
    depends_on:
      - db
    image: wordpress
    container_name: wp-web
    ports:
      - 7000:80
    restart: unless-stopped
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: banana
      WORDPRESS_DB_PASSWORD: banana
      WORDPRESS_DB_NAME: banana
    working_dir: /var/www/html
    volumes:
      - ./wp-content:/var/www/html/wp-content
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
  db:
    image: mysql:5.7
    container_name: db_server
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./db_data:/home/abelmbula/Documents/dockerapp/lib/mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: banana
      MYSQL_DATABASE: banana
      MYSQL_USER: banana
      MYSQL_PASSWORD: banana
    ports:
     - 8889:3306
volumes:
 db_data:

I get this out put

Starting db_server ... done
Recreating wp-web  ... done
Attaching to db_server, wp-web
db_server    | Initializing database
db_server    | 2019-06-14T13:32:53.477264Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_server    | 2019-06-14T13:32:53.479113Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
db_server    | 2019-06-14T13:32:53.479146Z 0 [ERROR] Aborting
db_server    | 
wp-web       | [14-Jun-2019 13:33:03 UTC] PHP Warning:  mysqli::__construct(): (HY000/2002): Connection refused in Standard input code on line 22
wp-web       | 
wp-web       | MySQL Connection Error: (2002) Connection refused
db_server exited with code 1

Is there something wrong with my file above?

masseyb
  • 3,745
  • 1
  • 17
  • 29
Abel LIFAEFI MBULA
  • 95
  • 1
  • 1
  • 10
  • What is in your `/home/abelmbula/Documents/dockerapp/lib/mysql` folder? As per the `db_server` startup log mysql is aborting because the folder mapped to db_data has things in it that mysql doesn't like. – Alexandre Voyer Jun 14 '19 at 14:16
  • It is where I store everything related to docker instead of /var/lib/mysql and the folder is empty. – Abel LIFAEFI MBULA Jun 14 '19 at 14:54
  • It seems to be PORT issue. You are exposing mysql via `8889` port, but in `wordpress` service you are connecting to DB via 3306 port. Use `8889` and check – Sameer K Jun 14 '19 at 14:58
  • @SameerK: I tried it but still the same error. – Abel LIFAEFI MBULA Jun 14 '19 at 15:07
  • Is there any other configuration within wordpress where it referring DB on port 3306? And also any specific reason to use port 8889. – Sameer K Jun 14 '19 at 15:12
  • I think you can not mention port number in `WORDPRESS_DB_HOST: db:3306` as per the example i saw in wordpress image docker hub. Just replace with `WORDPRESS_DB_HOST: db` and see – Sameer K Jun 14 '19 at 15:16
  • No, not at all. I tried to only use 3306 but in vain. – Abel LIFAEFI MBULA Jun 14 '19 at 15:17
  • I delete 3306 on WP HOST, nothing. – Abel LIFAEFI MBULA Jun 14 '19 at 15:24
  • I tried with docker-compose file that you have provided and it perfectly works fine on my local machine. Final try from my end, see you other mysql service running in your local machine. – Sameer K Jun 14 '19 at 15:39
  • refer this https://stackoverflow.com/questions/34068671/docker-compose-wordpress-mysql-connection-refused. restart your docker demon and check. – Sameer K Jun 14 '19 at 15:40
  • 1
    @bam the error is explicit `--initialize specified but the data directory has files in it`, you're creating a named volume: `db_data` but you're actually using the path (relative to your `docker-compose.yml`): `./db_data:...` to store the data. Purge the data in the `./db_data` directory and restart the `db` service or use the named volume that you've created (e.g. `db_data:...` != `./db_data:...`). Note: if you opt to use the volume you've created then you should ensure that it's empty else you'll continue getting the same error. – masseyb Jun 14 '19 at 15:59
  • e.g. unclear what this is `./db_data:/home/abelmbula/Documents/dockerapp/lib/mysql` by default `mysql` stores the data in `/var/lib/mysql`, ref. the documentation (https://hub.docker.com/_/mysql > "Where to Store Data"): "The -v /my/own/datadir:/var/lib/mysql part of the command mounts the /my/own/datadir directory from the underlying host system as /var/lib/mysql inside the container, where MySQL by default will write its data files." – masseyb Jun 14 '19 at 16:06

2 Answers2

1

This is a minimal example:

version: '3.7'
services:
  wordpress:
    depends_on:
    - mysql
    image: wordpress
    container_name: wordpress
    hostname: wordpress
    domainname: example.com
    ports:
    - 8080:80
    restart: unless-stopped
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: banana
      WORDPRESS_DB_PASSWORD: banana
      WORDPRESS_DB_NAME: banana
    working_dir: /var/www/html
    volumes:
    - wp_content:/var/www/html/wp-content
    - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
  mysql:
    image: mysql:5.7
    container_name: mysql
    hostname: mysql
    domainname: example.com
    command: --default-authentication-plugin=mysql_native_password
    volumes:
    - db_data:/var/lib/mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: banana
      MYSQL_DATABASE: banana
      MYSQL_USER: banana
      MYSQL_PASSWORD: banana
    expose:
    - 3306
volumes:
  db_data:
  wp_content:

This example uses the db_data volume defined in the volumes to store the database files (by default docker volumes exist in /var/lib/docker/volumes on the host operating system), it adds a named docker volume for the wordpress content, it does not publish the database port to the host rather it exposes it inside the docker network so that the wordpress service can access it and it sets example hostname and domainname entries for the services.

masseyb
  • 3,745
  • 1
  • 17
  • 29
  • You should be able to `copy` & `paste` this example into a `docker-compose.yml` then `docker-compose up` the services, the `wordpress` service will throw a couple `MySQL Connection Error: (2002) Connection refused` errors until it can connect to the database, once it's connected you're all set, can access the `wordpress` service on `http://localhost:8080`. – masseyb Jun 14 '19 at 16:29
0

I had the same issue too, I tried two things that worked for me, may be works for you too!

1 - Add for wordpress and db on docker-compose.yml file this flag restart: unless-stopped 2 - Let MySQL starts first to make WordPress connect with db.

services:
    db:
    container_name: mysql
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_PASSWORD: wordpress
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_USER: wordpress
    image: mysql:5.7
    restart: unless-stopped
    volumes:
        - /Users/ucqi/Apps/wp/www.tsm.com/db:/var/lib/mysql:rw
  wordpress:
    container_name: wordpress
    depends_on:
        - db
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_USER: wordpress
    image: wordpress:latest
    ports:
        - published: 8000
    target: 80
    restart: unless-stopped
version: '3.3'

Hope that works for you

Mahmod Issa
  • 51
  • 2
  • 5