[英]How to connect Docker PHP container to MySql container?
I have created a docker container with MySQL
: 我已经用MySQL
创建了一个docker容器:
docker run --name mysqlfordocker -p3307:3306 -e MYSQL_ROOT_PASSWORD=password_db-d mysql
It works as expected. 它按预期工作。
It is used by MySQLWorckbech
and PHP
application which are not in the container with MySQL
: 由MySQLWorckbech
和PHP
应用程序使用,它们不在MySQL
的容器中:
//CONNECTION BY PHP APPLICATION NOT IN CONTAINER
$servername = "127.0.0.1:3307";
$username_db = "username_db";
$password_db = "password_db";
$db = "db";
The Dockerfile
for my PHP
application is: 我的PHP
应用程序的Dockerfile
是:
FROM php:7.0-apache
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
COPY / /var/www/html
EXPOSE 80
Then I created a container with my PHP application
: 然后我用PHP application
创建了一个容器:
docker run –d –p 5000:80 myphpapplication
The PHP application
runs, but it has issues with database connection to MySQL
container. PHP application
可以运行,但是与MySQL
容器的数据库连接存在问题。
I tried to configure a connection servername
in a different way but without any luck: 我尝试以其他方式配置连接servername
,但没有任何运气:
$servername = "127.0.0.1:3307";
$servername = "locahosto:3307";
$servername = "10.0.75.1:3307"; //DockerNAT
$servername = "172.17.0.2:3307"; //Ip by docker inspect mysql
Could you please help me on this? 您能帮我吗?
There are two parts to this, one is getting the right IP address of the container. 这有两个部分,一个是获取容器的正确IP地址。 Either the actual IP address should work- 172.17.0.2, but you should also be able to use 172.17.0.1. 实际的IP地址都应该工作172.17.0.2,但您也应该能够使用172.17.0.1。
Also you normally put the port number as a separate parameter... 同样,您通常将端口号作为单独的参数...
$servername = "172.17.0.1";
$username_db = "username_db";
$password_db = "password_db";
$db = "db";
$port= = 3307;
$db = mysqli_connect($servername, $username_db, $password_db, $db, $port);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.