Skip to content
Snippets Groups Projects
Commit fc7a2152 authored by Muhammad Rifko Favian's avatar Muhammad Rifko Favian
Browse files

fix: setup docker to connect db

parent c7ec16ff
Branches
Tags
No related merge requests found
FROM php:8.0-apache FROM php:8.0-apache
\ No newline at end of file
RUN docker-php-ext-install pdo pdo_mysql
\ No newline at end of file
...@@ -2,7 +2,12 @@ version: '3.3' ...@@ -2,7 +2,12 @@ version: '3.3'
services: services:
web: web:
image: php:8.0-apache image: php:8.0-apache
build:
context: ./
dockerfile: Dockerfile
container_name: tubes-1 container_name: tubes-1
depends_on:
- db
ports: ports:
- 8008:80 - 8008:80
volumes: volumes:
...@@ -16,5 +21,3 @@ services: ...@@ -16,5 +21,3 @@ services:
- 3306:3306 - 3306:3306
volumes: volumes:
- ./db:/docker-entrypoint-initdb.d - ./db:/docker-entrypoint-initdb.d
depends_on:
- web
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>PHP TITLE</title> <title>PHP TITLE</title>
</head> </head>
<body> <body>
<h1>HAI HAI HAI HAI</h1> <h1>HAI HAI HAI HAI</h1>
<p>lalalalala</p> <p>lalalalala</p>
<?php <?php
$host = 'db'; // Service name from docker-compose
$dbname = 'tubes1_WBD'; // Your database name
$user = 'root'; // Your database user
$pass = ''; // Your database password
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
echo "Connected successfully!";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// PHP code // PHP code
$phpVersion = phpversion(); $phpVersion = phpversion();
echo "<p>PHP Version: $phpVersion</p>"; echo "<p>PHP Version: $phpVersion</p>";
...@@ -15,4 +28,5 @@ ...@@ -15,4 +28,5 @@
<p>asdf</p> <p>asdf</p>
</body> </body>
</html>
</html>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment