1
votes

I have a docker-compose as follow:

version: '3.2'
services:
  web:
    build: .
    image: lap
    volumes:
      - ./webroot:/var/www/app
      - ./configs/php.ini:/usr/local/etc/php/php.ini
      - ./configs/vhost.conf:/etc/apache2/sites-available/000-default.conf

    links:
      - dbs:mysql
  dbs:
    image: mysql
    ports:
      - "3307:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
    volumes:
      - ./configs/conf.d:/etc/mysql/conf.d
      - /var/mysql:/var/lib/mysql   

docker --version

Docker version 17.05.0-ce, build 89658be

docker-compose --version

docker-compose version 1.13.0, build 1719ceb

lsb_release -a

No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 8.10 (jessie) Release: 8.10 Codename: jessie

If I run following commands and check /etc/host on php container I see no linking ( which I see when I do docker container --link )

docker-compose up -d docker exec -it web_1 bash root@web_1> cat /etc/hosts

127.0.0.1 localhost

::1 localhost ip6-localhost ip6-loopback

fe00::0 ip6-localnet

ff00::0 ip6-mcastprefix

ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

172.18.0.3 9c401ea7d031

The dbs container is assigned 127.18.0.2 ip address, what I was expecting was something like this in above /etc/hosts

127.18.0.2 dbs mysql

I see the same behavior in one of my Mac OSX system , probably I am doing something wrong or missing anything ?

2
Maybe something wrong with your mounts? This blog helped me really to make my connection work, although it's about postgres and not mysql: lvthillo.com/…DenCowboy
Try to ping "dbs" or "mysql" from web container. They are supposed to be accessible by only service name. In my case, I saw my links alias is working though it's not /etc/hostsNikson Kanti Paul
@NiksonKantiPaul please make that an answer , thanks for the tipsakhunzai

2 Answers

2
votes

Containers are accessible by service-name from docker-compose. links of docker-compose is just an alias of this service-name. It supposed to be reachable from same network either by service-name or alias-name.

In your case:

try to ping from web to dbs or mysql.

0
votes

Legacy container links are deprecated and will be removed some time in the future from docker. The problem at hand is that you want your web container talk to your db container.

The solution is simple.

  • Use environmental variables to dynamically define database access DB_HOST.
  • Load this environment in your app, and if this variable is not defined then as a fallback value use localhost or 127.0.0.1.

Assuming that's done, you can now do this on your compose...

You can now define DB_HOST=dbs in your dockerfile.

version: '3.2'
    services:
      web:
          build: .
          image: lap
          volumes:
             - ./webroot:/var/www/app
             - ./configs/php.ini:/usr/local/etc/php/php.ini
             - ./configs/vhost.conf:/etc/apache2/sites-available/000-default.conf
          environment:
             - DB_HOST=dbs
      dbs:
          image: mysql
          ports:
             - "3307:3306"
          environment:
             - MYSQL_ROOT_PASSWORD=root
          volumes:
             - ./configs/conf.d:/etc/mysql/conf.d
             - /var/mysql:/var/lib/mysql   

This will fix this conundrum.