我在从PHP 7.0.1-apache容器连接到我链接的mysql容器时遇到问题.
PHP容器Dockerfile:
FROM PHP:7.0.1-apache
# Initialize html and PHP.ini
copY src/ /var/www/html/
copY config/PHP.ini /usr/local/etc/PHP/
# Update modules.
RUN apt-get update
泊坞窗,compose.yml:
web:
build: .
ports:
- "80:80"
links:
- "db"
volumes:
- "./src/:/var/www/html/"
db:
image: "MysqL"
ports:
- "3306:3306"
environment:
- "MysqL_ROOT_PASSWORD=somepword"
<?PHP
$servername = "127.0.0.1";
$username = "root";
$password = "somepword";
$db = "test_db";
// Create connection
$conn = new MysqLi($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection Failed: " . $conn->connect_error);
}
echo "Connected successfully";
错误日志:
PHP Fatal error: Uncaught Error: Class 'MysqLi' not found in /var/www/html/index.PHP:8\nStack trace:\n#0 {main}\n thrown in /var/www/html/index.PHP on line 8
我不知道我哪里出错了,基本上看起来我的PHP容器中没有正确配置MysqL.有什么建议?
解决方法:
是的,正如您所说,您的设置不包括MysqLi扩展,您需要手动安装.
像这样将MysqLi的安装添加到Dockerfile中,你应该做得很好:
FROM PHP:7.0.1-apache
RUN apt-get update && apt-get install -y MysqL-client libMysqLclient-dev \
&& docker-PHP-ext-install MysqLi
# Initialize html and PHP.ini
copY src/ /var/www/html/
copY config/PHP.ini /usr/local/etc/PHP/
最后也失去了apt-get更新.如果你有什么想运行apt-get update&& apt-get升级实际更新模块.老实说,你不应该这样做.这将导致您的Dockerfile在几周内不会生成相同的构建,只是因为依赖关系已经改变了哪种类型的Docker部分无法解决它?