Mark Lightfoot 5 years ago
commit
a5d7c070b9
4 changed files with 107 additions and 0 deletions
  1. 36 0
      README.md
  2. 8 0
      db/create_database.sql
  3. 34 0
      docker-compose.yaml
  4. 29 0
      src/index.php

+ 36 - 0
README.md

@@ -0,0 +1,36 @@
+# Docker-compose example
+
+This is a quick example that allows live development on a server. This is very basic and lacks a number of features that would be on an average site (eg fastcgi, redis, etc).
+
+It is accessible on `localhost:80`
+
+## db folder
+
+- Contains create_database.sql
+
+This is standard sql to create a database to work off. In the example of cloudM, you'd have an SQL dump or such here to populate the DB. This is referenced in `docker-compose.yaml` 
+
+```docker-compose.yaml
+volumes:
+      - "./db/:/docker-entrypoint-initdb.d"
+```
+With the specific image used (mysql:5.7) anything placed in the container at docker-entrypoint-initdb.d will be ran as sql files when the database has been created.
+
+## src
+ 
+Code lives here. The example here is a simple php file to pull some data from the DB and display it. it is mounted in the docker-compose.yaml so thus can edited on-the-fly without restarts.
+
+## docker-compose.yaml
+
+defines how everything meshes together. run the command `docker-compose up` to bring it all live, with down to tear it away. 
+
+```docker-compose.yaml
+    build: 
+      context: .
+      dockerfile: web-dockerfile
+```
+This defines that the image will be built from a file within the current directory, with web-dockerfile being the Dockerfile.
+
+## web-dockerfile
+
+Creates the docker image. It uses an image from the dockerhub, updates and then enables the mysqli module for php to allow connections. The command `docker-php-ext-install X' is provided by the creators of the docker image.

+ 8 - 0
db/create_database.sql

@@ -0,0 +1,8 @@
+USE test;
+
+CREATE TABLE test_table (
+  ID int,
+  Name varchar(30)
+);
+
+INSERT INTO test_table (ID, Name) VALUES (1, 'Jeff');

+ 34 - 0
docker-compose.yaml

@@ -0,0 +1,34 @@
+version: '3.3'
+
+services:
+  web:
+    image: richarvey/nginx-php-fpm:1.5.0
+    ports:
+      - "80:80"
+    networks:
+      - frontend
+      - backend  
+    restart: always
+    volumes:
+      - "./src/:/var/www/html/"
+    depends_on: 
+      - database
+  database:
+    image: mysql:5.7
+    volumes:
+      - "./db/:/docker-entrypoint-initdb.d"
+    restart: always
+    ports:
+      - "3306:3306"
+    networks:
+      - backend
+    environment:
+      MYSQL_DATABASE: "test"
+      MYSQL_USER: "test"
+      MYSQL_PASSWORD: "test"
+      MYSQL_ROOT_PASSWORD: "root"
+      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
+
+networks:
+  frontend:
+  backend:

+ 29 - 0
src/index.php

@@ -0,0 +1,29 @@
+<?php 
+
+$server = "database";
+$db     = "test";
+$user   = "test";
+$pass  = "test";
+
+$conn = new mysqli($server, $user, $pass, $db);
+
+/*if ($conn->connect_error) {
+    die("conn fail: " . $conn->connect_error);
+}
+echo "connected";
+*/
+
+$query = "SELECT * FROM test_table;";
+$result = $conn->query($query);
+
+if ($result->num_rows > 0) {
+    // output data of each row
+    while($row = $result->fetch_assoc()) {
+        echo "id: " . $row["ID"]. " - Name: " . $row["Name"]. "<br>";
+    }
+} else {
+    echo "0 results";
+}
+$conn->close();
+
+?>