Docker compose v2 containers not finding each other (though v1 does)

admin

Administrator
Staff member
Trying to learn Docker, I am having trouble getting two containers to see each other. When using docker-compose version 2 formatted files, I receive errors trying to reach the database from the front end. I tried what I thought would be a foolproof approach--<a href="https://docs.docker.com/compose/wordpress/" rel="nofollow noreferrer">using the WordPress example straight from the Docker website</a>. Using that exact example, the database cannot be found by the wordpress container. This is the error it displays:

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

I boiled it down to a simple example to show a working version 1 file and non-working version 2 file. The only difference between the two is the removal of the version number and the "services" grouping.

Version 1 docker-compose file that DOES work

Code:
web:
  image: wordpress
  links:
    - mysql
  environment:
    - WORDPRESS_DB_PASSWORD=password
  ports:
    - "8080:80"
mysql:
  image: mysql:5.7
  environment:
    - MYSQL_ROOT_PASSWORD=password
    - MYSQL_DATABASE=wordpress

Version 2 docker-compose that does NOT work

Code:
version: '2'
services:
  web:
    image: wordpress
    links:
      - mysql
    environment:
      - WORDPRESS_DB_PASSWORD=password
    ports:
      - "8080:80"
  mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=wordpress

I'm not actually trying to run WordPress with these settings, just trying to figure out why a docker-compose version 2 file does not work when the exact same file, formatted as a version 1 file, does work. I have tried many other example applications that connect to a database and I can get none of them to work.

Environment I am using:

Code:
$ uname -a
Linux vir-docker 4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
$ docker -v
Docker version 1.13.1, build 092cba3
$ docker-compose -v
docker-compose version 1.11.1, build 7c5d5e4