justinvoelker
justinvoelker

Reputation: 573

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

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--using the WordPress example straight from the Docker website. Using that exact example, the database cannot be found by the wordpress container. This is the error it displays:

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

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

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:

$ 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

Upvotes: 2

Views: 690

Answers (1)

user2105103
user2105103

Reputation: 13095

This must be a config issue. I executed your exact version 2 file (only changed the published port). Spun it up via:

 docker-compose up

Switched to another shell, and entered the web container.

 docker exec -it web_1 /bin/bash

 root@d089d5b25d99:/var/www/html# ping mysql
 PING mysql (172.18.0.2): 56 data bytes
 64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.080 ms
 64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.082 ms

Web can ping mysql just fine. Now let's try the mysql container:

 docker exec -it mysql_1 /bin/bash

 root@c8b13ff81973:/# ping web
 PING web (172.18.0.3): 56 data bytes
 64 bytes from 172.18.0.3: icmp_seq=0 ttl=64 time=0.081 ms
 64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.083 ms

It can ping the web container just fine.

Upvotes: 1

Related Questions