Skip to content

Instantly share code, notes, and snippets.

@jcavat
Last active November 14, 2024 21:19
Show Gist options
  • Save jcavat/2ed51c6371b9b488d6a940ba1049189b to your computer and use it in GitHub Desktop.
Save jcavat/2ed51c6371b9b488d6a940ba1049189b to your computer and use it in GitHub Desktop.
docker-compose with php/mysql/phpmyadmin/apache
Create this directories structure:
.
├── docker-compose.yml
├── Dockerfile
├── dump
│   └── myDb.sql
├── sessions
└── www
└── index.php
version: "2"
services:
www:
build: .
ports:
- "8001:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
FROM php:7.1.2-apache
RUN docker-php-ext-install mysqli
<!-- put in ./www directory -->
<html>
<head>
<title>Hello...</title>
<meta charset="utf-8">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php echo "<h1>Hi! I'm happy</h1>"; ?>
<?php
$conn = mysqli_connect('db', 'user', 'test', "myDb");
$query = 'SELECT * From Person';
$result = mysqli_query($conn, $query);
echo '<table class="table table-striped">';
echo '<thead><tr><th></th><th>id</th><th>name</th></tr></thead>';
while($value = $result->fetch_array(MYSQLI_ASSOC)){
echo '<tr>';
echo '<td><a href="#"><span class="glyphicon glyphicon-search"></span></a></td>';
foreach($value as $element){
echo '<td>' . $element . '</td>';
}
echo '</tr>';
}
echo '</table>';
$result->close();
mysqli_close($conn);
?>
</div>
</body>
</html>
-- put in ./dump directory
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!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 */;
CREATE TABLE `Person` (
`id` int(11) NOT NULL,
`name` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `Person` (`id`, `name`) VALUES
(1, 'William'),
(2, 'Marc'),
(3, 'John');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
@asiandevs
Copy link

Hi @varaskkar - Thank you.

I think, a small correction. Tree structure would be for this one is as below —

Before executing docker-compose [no need to create directory | or if you create then not "/"]
[root@ip-10-0-8-114 ~]# tree dockercompose03
dockercompose03
├── conf
├── docker-compose.yml
├── dockerfile
├── dump
│ └── myDb.sql
└── www
└── index.php

After executing docker-compose up
[root@ip-10-0-8-114 ~]# tree dockercompose03
dockercompose03
├── conf
├── docker-compose.yml
├── dockerfile
├── dump
│ └── myDb.sql
├── log
│ ├── access.log
│ ├── error.log
│ └── other_vhosts_access.log
├── src
└── www
└── index.php

5 directories, 7 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment