Your PHP project on which you want to develop
Objective: create a simple php project that is representative of a PHP project you might be working on. This project will serve as a support for the rest of the tutorial.
For the purpose of this tutorial we will create a simple PHP project consisting of a single PHP page index.php that will access a MySQL database containing a single table.
Creating the project file structure
Create the following file structure:
- a parent folder docker-lampx.lateambichon.com
- inside this folder create :
- an empty file called index.php
- a folder called .devcontainer
- inside the .devcontainer folder create:
- an empty file called Dockerfile
- an empty file called docker-compose-.yml
- a folder called docker
- inside the docker folder create:
- an empty file called docker-lampx.lateambichon.conf
- an empty called dump_database.sql
- an empty file called mod_deflate.conf
We will then see what these files and folders that we’ve just created correspond to.
index.php
Objective: This file will be used to:
- as a support to demonstrate the debugging possibilities within a docker container
- also it will demonstrate the possibility for a container to communicate with another container: communication with the MySQL container
Here is the source code of this file:
<?php
$servername = "docker-mysql";
$dbname = "dockerltb";
$username = "dockerltb";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "<p>Connected successfully to database</p>";
}
catch(PDOException $e)
{
echo "<p>Connection to database failed: " . $e->getMessage() . "</p>";
}
echo "<p>This is team bichon!!!</p>";
echo "<p>phpinfo() result :</p>";
echo phpinfo();
?>
dump_database.sql
Objective: This file will be used to fill our MySQL database.
In practice, in your project, it can be a dump of the production database whose data you will have previously anonymized (or not :p), or any other useful dataset for your development environment.
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
DROP TABLE IF EXISTS `dockerltb`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `dockerltb` (
`id` INTEGER NOT NULL,
`content` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='List of possible actions';
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `dockerltb`
--
LOCK TABLES `dockerltb` WRITE;
/*!40000 ALTER TABLE `dockerltb` DISABLE KEYS */;
INSERT INTO `dockerltb` VALUES (1,'This is the team!!!');
/*!40000 ALTER TABLE `dockerltb` ENABLE KEYS */;
UNLOCK TABLES;
Leave a Reply