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?
I did not use yml, but had same logs and this did not work
But then I found, that I have to add :mysql to linked container
Now it started.
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 thewordpress
container. You need to tell docker to link thedb
container from thewordpress
container to reference it by name.What
docker-compose
does under the hood is take the ip docker gives thedb
container and add a/etc/hosts
entry to thewordpress
container so you can reference it by name.So try adding this to the wordpress section
links: - db
Solution:
Remember to link the mysql container to the wordpress container:
Like this:
This important detail is not mentioned on the WordPress image official Dockerhub page
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.:
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 addto 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:
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.