This is my docker-compose.yml
version: \'2\'
services:
wordpress:
image: wordpress
ports:
- \"8080:80\"
environment:
WORDPRESS_DB_P
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.