[英]How to configure and run old php 5.4 web application in docker with mysql?
I have an old application based on php 5.4 and mysql.我有一个基于 php 5.4 和 mysql 的旧应用程序。 I had developed that in 2015. I want to run that application using docker as changing php version for individual application is not a feasible solution.我在 2015 年开发了它。我想使用 docker 运行该应用程序,因为更改单个应用程序的 php 版本不是一个可行的解决方案。
What I did is as follows:我所做的如下:
Dockerfile文件
FROM php:5.4-apache
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
RUN a2enmod rewrite
COPY . /var/www/html/
RUN chown -R www-data:www-data /var/www/html
RUN docker-php-ext-install mysql
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
docker-compose.yml docker-compose.yml
version: "3.7"
services:
webapp:
build:
context: ./
dockerfile: Dockerfile
ports:
- "8000:80"
volumes:
- .:/var/www/html
# first load the 'db' service
depends_on:
- db
links:
- db
db:
image: mysql:5.7
restart: always
ports:
- "13306:3306"
environment:
MYSQL_USER: root
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: pizzabite
volumes:
- ./mysql:/var/lib/mysql
The problem here is I was accessing mysql using old mysql methods ie mysql_connect() etc.这里的问题是我使用旧的 mysql 方法访问 mysql,即 mysql_connect() 等。
database.php数据库.php
<?php
// Connecting to the database server
$servername = "localhost:13306";
$username = "root";
$password = "admin";
$port = "13306";
$dbname = "pizzabite";
$connect = mysql_connect($servername, $username, $password) or die("<br/>Check your server connection...");
// $connect = mysqli_connect($servername, $username, $password, $dbname,$port); // I do not want to use mysqli_* function
$selectedDB = mysql_select_db($dbname) or die(mysqli_error());
//include('date.php');
?>
This file is included everywhere using include()
but when run using docker this file gives error:使用include()
到处都包含此文件,但是当使用 docker 运行时,此文件会出现错误:
Warning: mysql_connect(): No such file or directory in /var/www/html/database.php on line 10
Check your server connection...
When using 0.0.0.0:13306 it gives me this error:使用0.0.0.0:13306 时,它给了我这个错误:
Warning: mysql_connect(): Connection refused in /var/www/html/database.php on line 10
Check your server connection...
docker-compose ps
command output: docker-compose ps
命令输出:
Name Command State Ports
---------------------------------------------------------------------------------------------------
pizza_corner_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:13306->3306/tcp, 33060/tcp
pizza_corner_webapp_1 /usr/sbin/apachectl -D FOR ... Up 0.0.0.0:8000->80/tcp
You are trying to connect to localhost:13306
, because your PC port 13306
is forwarded to the mysql container.您正在尝试连接到localhost:13306
,因为您的 PC 端口13306
已转发到 mysql 容器。
However, that code is executed from the php-apache
container, so 'localhost' is the container, not your pc.但是,该代码是从php-apache
容器执行的,因此 'localhost' 是容器,而不是您的 pc。 There is no port 13306
available there.那里没有可用的端口13306
。
You should connect to db:13306
because in your docker-compose the database container is called db
.您应该连接到db:13306
因为在您的 docker-compose 中,数据库容器称为db
。
Also, be sure to enable mysql in your php container:另外,请务必在您的 php 容器中启用 mysql:
RUN docker-php-ext-install mysql && docker-php-ext-enable mysql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.