Docker php_network_getaddresses error(Docker php_network_getaddresses 错误)
问题描述
我正在运行以下由 PHPDocker 生成的 Docker 容器:
I have following Docker containers running that were generated by PHPDocker:
learn-php-mysql:
image: mysql:5.7
container_name: learn-php-mysql
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: learning
MYSQL_DATABASE: learning
MYSQL_USER: learning
MYSQL_PASSWORD: learning
learn-php-webserver:
image: phpdockerio/nginx:latest
container_name: learn-php-webserver
volumes:
- ./code:/code
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8080:80"
links:
- learn-php-php-fpm
learn-php-php-fpm:
build: .
dockerfile: php-fpm/Dockerfile
container_name: learn-php-php-fpm
volumes:
- ./code:/code
- ./php-fpm/php-ini-overrides.ini:/etc/php/7.1/fpm/conf.d/99-overrides.ini
links:
- learn-php-mysql:mysql
一切正常,除了尝试通过 PHP 代码连接到 MySQL 服务器时:
Everything works fine, except for when trying to connect to MySQL server via PHP code:
$mysqli = new mysqli("db", "learning", "learning", "learning");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s
", $mysqli->connect_error);
exit();
}
它会抛出以下错误:
连接失败:php_network_getaddresses:getaddrinfo 失败:名称或服务未知
Connect failed: php_network_getaddresses: getaddrinfo failed: Name or service not known
为什么会这样?
.
.
您可以在此处找到此存储库.当运行 docker-compose up
时,转到 http://localhost:8080/classes/serialize.php 产生同样的错误.
You can find repository for this here. And when running docker-compose up
go to http://localhost:8080/classes/serialize.php to produce same error.
推荐答案
通过 PHP(或任何其他)连接时,使用容器的名称作为主机,如果是 learn-php-mysql.
When making connection via PHP (or any other), use the container's name as host, which in case is learn-php-mysql.
因此
$mysqli = new mysqli("learn-php-mysql", "learning", "learning", "learning");
会起作用的.
这篇关于Docker php_network_getaddresses 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Docker php_network_getaddresses 错误
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01