gpt4 book ai didi

php - Laravel 无法连接到 dockerise 数据库

转载 作者:行者123 更新时间:2023-11-28 23:12:33 25 4
gpt4 key购买 nike

我创建了三个容器 (PhP-NGINX-MySql) 来支持位于主机上的默认 laravel 项目。

当我尝试从 laravel 连接到数据库时出现错误:

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)

这是我在 laravel-5.3.16 中的 .env

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

和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

更奇怪的是,当我运行 php artisan migrate 时,我得到了成功消息:

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

这里是正在运行的容器:

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

最佳答案

尝试将“diba_cont”作为 DB_HOST 来完成这项工作。 Docker 自动声明一个带有容器名称的 DNS 规则来联系容器

关于php - Laravel 无法连接到 dockerise 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45300536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com