-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3a9936
commit ec7cde7
Showing
9 changed files
with
618 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copy this file to .env IN THE SAME FOLDER as your docker-compose.yml | ||
# Make sure that ".env" string is in your .gitignore | ||
# This is OK for development ONLY. Don't do this for production! | ||
# For production you would "docker swarm secrets" or AWS secrets or other, based on your hosting service | ||
|
||
DOCKER_DB_HOST=cmsc508.com | ||
DOCKER_DB_USER=24fa_... | ||
DOCKER_DB_PASS=Shout4_..._JOY | ||
DOCKER_DB_DBNAME=24fa_hr_24fa_... | ||
|
||
## this is used for the local database root password. | ||
|
||
DOCKER_DB_ROOT_PASSWORD=MyRootPassword44 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Apache-PHP docker-compose example | ||
|
||
This example is primarily for development. | ||
|
||
In this example, we use docker and docker-compose to locally host an apache/php server | ||
and local web page. Further, the database is running locally in a container, initialized with the DDL-SQL file found | ||
in the .db folder. Finally, a phpmyadmin container is running to provide management of the database. | ||
|
||
*./site* - this folder contains the web site. For this example, it's a simple PHP page that lists table in a web site. | ||
|
||
*./db* - this folder contains the SQL file used to initialize the database. It is run once when the container is created. | ||
|
||
*./docker-apache-php* - a docker file that creates the necessary apache-php image from the docker registry. | ||
|
||
*./docker-compose.yml* - a docker compose file that create the docker-apache-php image, remaps our *./site* folder | ||
to the web server root, and connects our local port 8080 to the web server port 80 inside the container. | ||
|
||
*./env-sample* - a sample .env file. Copy this file to `.env` and edit it as necessary. | ||
|
||
## Create a local .env file | ||
|
||
Docker compose can only load environment variables from a `.env` file stored IN THE SAME FOLDER as `docker-compose.yml`. To | ||
load everything properly, you need to copy the `.env-sample` file to `.env` and then edit the `.env` file as appropriate. | ||
|
||
Make sure that `.env` is listed in your `.gitignore` so that any `.env` files are NOT stored in your repo. | ||
|
||
## Running the example | ||
|
||
```shell | ||
docker-compose up -d | ||
``` | ||
|
||
If all goes well, you can visit: <http://localhost:8080> to see a listing tables in the HR database. `phymyadmin` will be available at <http://localhost:8081>. | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM php:apache | ||
|
||
# Install the mysqli extension | ||
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
services: | ||
web: | ||
build: | ||
context: . | ||
dockerfile: docker-apache-php | ||
container_name: apache-php-container | ||
ports: | ||
- "8080:80" # Map port 8080 on the host to port 80 in the container | ||
environment: | ||
- MYSQL_HOST=db | ||
- MYSQL_USER=${DOCKER_DB_USER} | ||
- MYSQL_PASSWORD=${DOCKER_DB_PASS} | ||
- MYSQL_DATABASE=${DOCKER_DB_DBNAME} | ||
volumes: | ||
- ./site:/var/www/html | ||
depends_on: | ||
- db | ||
|
||
db: | ||
image: mysql:8.0 | ||
container_name: mysql-container | ||
restart: always | ||
environment: | ||
MYSQL_ROOT_PASSWORD: ${DOCKER_DB_ROOT_PASSWORD} | ||
MYSQL_USER: ${DOCKER_DB_USER} | ||
MYSQL_PASSWORD: ${DOCKER_DB_PASS} | ||
MYSQL_DATABASE: ${DOCKER_DB_DBNAME} | ||
ports: | ||
- "3306:3306" # MySQL port | ||
volumes: | ||
- ./db:/docker-entrypoint-initdb.d # Mount the folder containing SQL files | ||
|
||
phpmyadmin: | ||
image: phpmyadmin/phpmyadmin | ||
container_name: phpmyadmin-container | ||
restart: always | ||
ports: | ||
- "8081:80" # Expose phpMyAdmin on port 8081 | ||
environment: | ||
PMA_HOST: db | ||
MYSQL_ROOT_PASSWORD: ${DOCKER_DB_ROOT_PASSWORD} # For access to phpMyAdmin | ||
depends_on: | ||
- db | ||
|
||
volumes: | ||
mysql_data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
// Database connection parameters | ||
$servername = getenv('MYSQL_HOST'); // Docker service name for the MySQL container | ||
$username = getenv('MYSQL_USER'); | ||
$password = getenv('MYSQL_PASSWORD'); | ||
$dbname = getenv('MYSQL_DATABASE'); | ||
|
||
// Create connection | ||
$conn = new mysqli($servername, $username, $password, $dbname); | ||
|
||
// Check connection | ||
if ($conn->connect_error) { | ||
die("Connection failed: " . $conn->connect_error); | ||
} | ||
|
||
// Query to get all tables in the database | ||
$sql = "SHOW TABLES"; | ||
$result = $conn->query($sql); | ||
|
||
if ($result->num_rows > 0) { | ||
echo "<h1>Tables in the database:</h1>"; | ||
echo "<ul>"; | ||
// Output each table name | ||
while($row = $result->fetch_array()) { | ||
$table_name = $row[0]; | ||
echo "<li><a href='show-table.php?table=$table_name'>$table_name</a></li>"; | ||
} | ||
echo "</ul>"; | ||
} else { | ||
echo "No tables found in the database."; | ||
} | ||
|
||
$conn->close(); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
// Check if the table name is provided as a query parameter | ||
if (!isset($_GET['table'])) { | ||
die("No table specified."); | ||
} | ||
|
||
$table_name = $_GET['table']; | ||
|
||
// Database connection parameters | ||
|
||
$servername = getenv('MYSQL_HOST'); // Docker service name for the MySQL container | ||
$username = getenv('MYSQL_USER'); | ||
$password = getenv('MYSQL_PASSWORD'); | ||
$dbname = getenv('MYSQL_DATABASE'); | ||
|
||
|
||
// Create connection | ||
$conn = new mysqli($servername, $username, $password, $dbname); | ||
|
||
// Check connection | ||
if ($conn->connect_error) { | ||
die("Connection failed: " . $conn->connect_error); | ||
} | ||
|
||
// Query to retrieve the contents of the specified table | ||
$sql = "SELECT * FROM `$table_name`"; | ||
$result = $conn->query($sql); | ||
|
||
if ($result->num_rows > 0) { | ||
echo "<a href='/'>[home]</a><br/>"; | ||
echo "<h1>Contents of table: $table_name</h1>"; | ||
echo "<table border='1'>"; | ||
|
||
// Output table headers | ||
$fields = $result->fetch_fields(); | ||
echo "<tr>"; | ||
foreach ($fields as $field) { | ||
echo "<th>" . htmlspecialchars($field->name ?? '') . "</th>"; | ||
} | ||
echo "</tr>"; | ||
|
||
// Output table rows | ||
while($row = $result->fetch_assoc()) { | ||
echo "<tr>"; | ||
foreach ($row as $value) { | ||
echo "<td>" . htmlspecialchars($value ?? '') . "</td>"; | ||
} | ||
echo "</tr>"; | ||
} | ||
|
||
echo "</table>"; | ||
} else { | ||
echo "No records found in table: $table_name"; | ||
} | ||
|
||
$conn->close(); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters