Demaunt
Demaunt

Reputation: 1243

Laravel cannot connect to dockerise database

I created three containers (PhP-NGINX-MySql) to support default laravel project located on host machine.

When I try to connect to DB from laravel I get error:

Route::get('/', function () {
    dd(App\User::all());
    return view('welcome');
});

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from users)

Here is my .env in laravel-5.3.16

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33061
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

and ansible-playbook:

---
- hosts: localhost
  environment:
    PYTHONPATH: /usr/local/lib/python2.7/site-packages/

  tasks:

  - name: get currernt location
    command: pwd
    register: my_way

  - set_fact: host_dir="{{my_way.stdout}}"

  - name: create image with nginx
    docker_image:
      path: /home/demaunt/Jun/dock_click/engie
      dockerfile: engie.dockerfile
      name: engie_image

  - name: create image with php
    docker_image:
      path: /home/demaunt/Jun/dock_click/piha
      dockerfile: piha.dockerfile
      name: piha_image

  - name: run piha container
    docker_container:
      name: piha_cont
      image: piha_image
      volumes:
        - "/home/demaunt/Dockjun/laravel-5.3.16:/var/www/wapclick"
      links:
        - diba_cont:db
      env:
        DB_PORT: 3306
        DB_HOST: database

  - name: run engie container
    docker_container:
      name: engie_cont
      image: engie_image
      volumes_from:
        - piha_cont
      ports:
        - "8080:80"
      links:
        - piha_cont:app  

  - name: run diba container
    docker_container:
      name: diba_cont
      image: mysql:5.6
      env:
        MYSQL_DATABASE: homestead
        MYSQL_USER: homestead
        MYSQL_PASSWORD: secret
        MYSQL_ROOT_PASSWORD: secret 
      ports:
       - 33061:3306

What is more strange is that when I run php artisan migrate I get succesfull mesage:

Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table

Here are containers running:

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                           NAMES
e926382db347        engie_image         "nginx -g 'daemon ..."   19 seconds ago      Up 18 seconds       443/tcp, 0.0.0.0:8080->80/tcp   engie_cont
c9563c839e45        piha_image          "docker-php-entryp..."   19 seconds ago      Up 18 seconds       9000/tcp                        piha_cont
5de541049da2        mysql:5.6           "docker-entrypoint..."   20 seconds ago      Up 19 seconds       0.0.0.0:33061->3306/tcp         diba_cont

Upvotes: 1

Views: 1090

Answers (1)

Mcsky
Mcsky

Reputation: 1445

Try "diba_cont" as DB_HOST that should do the job. Docker declare automatically a DNS rule with the name of the container to contact the container

Upvotes: 1

Related Questions