Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress on docker-compose no run

This is my docker-compose.yml

version: '2'
services:
  wordpress:
    image: wordpress
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_PASSWORD: example
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example

the services run normally, but, a few seconds later, the wordpress container stop.

This is my docker logs wordpress container:

WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html

MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known

Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10

Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10

MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known

Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10

Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10


MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known
Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10

Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10

Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10

MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known

Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10

Why does this happen?

like image 862
alvarezsh Avatar asked Jul 16 '16 02:07

alvarezsh


3 Answers

Solution:

Remember to link the mysql container to the wordpress container:

links:
   - db:mysql

Like this:

version: '2'
services:
  wordpress:
    image: wordpress
    ports:
      - "8080:80"
    links:
      - db:mysql
    environment:
      WORDPRESS_DB_PASSWORD: example
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example

This important detail is not mentioned on the WordPress image official Dockerhub page

like image 172
Vingtoft Avatar answered Oct 24 '22 14:10

Vingtoft


Are you setting the DB host (and other needed MySql attributes), in your Compose file wordpress service (other than password, shown in your post)? e.g.:

environment:
  WORDPRESS_DB_PASSWORD: example
  WORDPRESS_DB_HOST: db
  WORDPRESS_DB_USER: {xxx}
  WORDPRESS_DB_NAME: {xxx}
  WORDPRESS_TABLE_PREFIX: {xxx}

In particular the "host" value, which in your setup should be db. You should not have to do any linking, although it would be a good idea to add

depends_on:
  - db

to your wordpress service block which will set the dependency order to start the db container before your wordpress container. (A links attribute would do the same, but trying to keep things simple.)

Note:

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

Links allow you to define extra aliases by which a service is reachable from another service. They are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.

https://docs.docker.com/compose/networking/#/links

I would not mess around with networks unless you really understand what you are doing, as in most cases the defaults will work fine. If you have some special case, you can always optimize that later.

like image 41
ldg Avatar answered Oct 24 '22 14:10

ldg


This anwser works for me, cause I didn't have enough reputation to vote that anwser, I copy it here:

depends_on:
  - db

That just makes sure the database container is fully loaded before the wordpress container. You need to tell docker to link the db container from the wordpress container to reference it by name.

What docker-compose does under the hood is take the ip docker gives the dbcontainer and add a /etc/hosts entry to the wordpress container so you can reference it by name.

So try adding this to the wordpress section

links:
  - db
like image 31
NOZUONOHIGH Avatar answered Oct 24 '22 13:10

NOZUONOHIGH