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?
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
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 db
container 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
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.
I did not use yml, but had same logs and this did not work
docker run -dP --link elated_yonath -e WORDPRESS_DB_USER=libik -e WORDPRESS_DB_PASSWORD=prdik wordpress
But then I found, that I have to add :mysql to linked container
docker run -dP --link elated_yonath:mysql -e WORDPRESS_DB_USER=libik -e WORDPRESS_DB_PASSWORD=prdik wordpress
Now it started.