doujiong2533 2017-10-27 15:29
浏览 85

使用PHP和MySql的Docker - $ servername错误[重复]

I have a project developed with PHP and MySql.

When I Use the connection to DB I set the connection as a follow:

 // Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];


/* -------------------------------- CONNESSIONE -------------------------------------- */
$servername = "localhosto";
$username_db = "xxxxxx";
$password_db = "yyyyyy";
$db = "zzzzzz";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$db", $username_db, $password_db);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo "Connected successfully<br/><br/>";
}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage()."<br/><br/>";
}

the project run correctly and connect to MySql without problems.

Next step I was created a Docker Image with a follow docker file:

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

I have create the image and container:

docker build -t mysoftware C:\Users\mysoftware
docker run -d -p 5000:80 mysoftware

but the Php application don't connect to MySql.

I need to change the connection with the name of server:

$servername = "SERVER_NAME";

Why I have an error if the app run correcly without container ??

</div>
  • 写回答

2条回答 默认 最新

  • douxiluan6555 2017-10-27 15:40
    关注

    (Assuming *nix) Once you have the mysql docker container running...

    docker ps
    

    and find the name of the mysql container that is running. Then to find the IP address...

    docker inspect <name_of_container> | grep IPAddress
    

    Then use this IP address for the server to connect to.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错