Add PHP/MySQL/PhpMyAdmin docker-compose

Signed-off-by: Alireza Far <imail01999@gmail.com>
This commit is contained in:
Alireza Far
2021-08-24 00:41:33 +04:30
parent 4bbd137d73
commit dfa2f6b866
7 changed files with 297 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
FROM php:8.0-apache
COPY . /var/www/html
EXPOSE 80

View File

@@ -0,0 +1,26 @@
<?php
/**
* This is a sample code to show you how Docker containers work together.
* DO NOT USE this source and pattern in production.
*
*
* You can see the result by going to `localhost:8000`.
* 8000-8080:
* Host Apache port to run PHP index.php inside /var/www/html in the container.
* 8001 : Refers to 3306 & 33060 of the MySQL port in the container
* 8003 : Refers to 80 of the PhpMyAdmin [user=root, no password]
*
*/
require_once "query.php";
/**
* IP of MySQL
* We can add specific IPs in docker-compose.yaml file for each service(container)
*/
$mysqlHost = "172.17.0.3";
run_MySQL_queries($mysqlHost);

View File

@@ -0,0 +1,73 @@
<?php
function run_MySQL_queries($mysqlHost)
{
/**
* Connect to MySQL via PDO
*
* PDO modules must be installed before using, moduels' name:
* pdo pdo_mysql
*/
$pdo = new PDO("mysql:host=$mysqlHost", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/**
* Create a new database called `MyDatabase` if does not exist
*/
$pdo->exec("CREATE DATABASE IF NOT EXISTS MyDatabase");
/**
* Use the database we created
*/
$pdo->exec("USE MyDatabase");
/**
* Check if the `test` table exists before
* If not, creates a new one
*/
$stmt = $pdo->prepare("SHOW TABLES LIKE 'test'");
$stmt->execute();
if ($stmt->rowCount() == 0) {
$stmt = $pdo->prepare("CREATE TABLE test (column1 VARCHAR (255) )");
$stmt->execute();
}
/**
* Insert a sample data `Hello world` into table
*/
$stmt = $pdo->prepare("INSERT INTO test (column1) VALUES ('Hello world')");
$stmt->execute();
/**
* Fetch the data we inserted
*/
$stmt = $pdo->prepare("SELECT * FROM test LIMIT 1");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
/**
* Show the data as string
*/
echo $result[0]['column1'];
}