From 133e1fb1d4747e16d8ffe7b9284a35267f861da1 Mon Sep 17 00:00:00 2001 From: Noel Palo Date: Sun, 22 Aug 2021 11:45:55 +0800 Subject: [PATCH 1/2] Docker Structure - Dockerfile - docker-compose NginX settings ReadMe Info --- .gitignore | 5 +- README.md | 22 +++++++- docker/README.md | 62 +++++++++++++++++++++ docker/containers/Dockerfile | 5 ++ docker/containers/nginx/conf.d/default.conf | 22 ++++++++ docker/docker-compose/docker-compose.yml | 54 ++++++++++++++++++ src/README.md | 11 ++++ src/public/index.html | 10 ++++ 8 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 docker/containers/Dockerfile create mode 100644 docker/containers/nginx/conf.d/default.conf create mode 100644 docker/docker-compose/docker-compose.yml create mode 100644 src/public/index.html diff --git a/.gitignore b/.gitignore index ca537a4..b5c76f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ # Environment -/.idea \ No newline at end of file +/.idea + +# MySQL +/docker/containers/mysql/ \ No newline at end of file diff --git a/README.md b/README.md index b905f95..40ab327 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,21 @@ # Docker Study -> This is a simple repo to learn the basics in setting up a -> *Web development environment with __Docker__*. \ No newline at end of file +> This is a simple repo to learn the basics in setting up a +> *Web development environment with NginX, MySQL, and PHP inside a __Docker__ container*. + +## Web Stack +- Docker Containers + - nginx + - mysql + - php +- NginX + - nginx:stable-alpine +- MySQL 5.7 + - mysql:5.7.22 +- PHP 7 + - php:7.2-fpm-alpine + +## Table of Contents +| Branch Name | What's Inside | +| ------------- | ------------- | +| master | Documentation | +| webstack/v1.0.0 | nginx:stable-alpine, mysql:5.7.22, php:7.2-fpm-alpine | \ No newline at end of file diff --git a/docker/README.md b/docker/README.md index bbf4582..cd5037f 100644 --- a/docker/README.md +++ b/docker/README.md @@ -4,3 +4,65 @@ > ## Docker Directory - This directory will host the docker related files. + +## Docker Commands +> Notes of some simple docker commands for building, starting, and stopping the service containers + +> ### Note: +> In this structure, we always need to add a -f flag and the path to the docker compose yaml file +> which in out case is -f ./docker/docker-compose/docker-compose.yml + +### Building the service containers +``` +// Build the services/containers +docker-compose -f ./docker/docker-compose/docker-compose.yml build +``` + +### Starting the service +``` +docker-compose -f ./docker/docker-compose/docker-compose.yml up + +// add the -d (detach) option to run in the background +docker-compose -f ./docker/docker-compose/docker-compose.yml up -d + +// use the container name for a specific service +// docker-compose up <_ContainerName_> +docker-compose -f ./docker/docker-compose/docker-compose.yml up php +``` + +### Stopping the service +``` +docker-compose -f ./docker/docker-compose/docker-compose.yml down -v + +// -v, --volumes Remove named volumes declared in the `volumes` +// section of the Compose file and anonymous volumes +// attached to containers. + +// to stop a specific service, use the stop command followed by th container name +// docker-compose stop <_ContainerName_> +docker-compose -f ./docker/docker-compose/docker-compose.yml stop mysql +``` + +### Restarting the service +``` +docker-compose -f ./docker/docker-compose/docker-compose.yml restart + +// use the container name for a specific service +docker-compose -f ./docker/docker-compose/docker-compose.yml restart php +``` + +### Running arbitrary commands inside a service container +``` +// Use docker-compose exec to run command inside the container. +// - e.g.: To run php artisan migrate commad +// - format: docker-compose exec <_ContainerName_> php <_PathToArtisan_> migrate +docker-compose -f ./docker/docker-compose/docker-compose.yml exec php php /var/www/html/artisan migrate +``` + +``` +// building & starting (detach) +docker-compose -f ./docker/docker-compose/docker-compose.yml up -d --build + +// Check if index.php exists +docker-compose -f ./docker/docker-compose/docker-compose.yml exec nginx ls /var/www/html/public +``` \ No newline at end of file diff --git a/docker/containers/Dockerfile b/docker/containers/Dockerfile new file mode 100644 index 0000000..8a3478c --- /dev/null +++ b/docker/containers/Dockerfile @@ -0,0 +1,5 @@ +# Install PHP 7.2 +FROM php:7.2-fpm-alpine + +# Install PHP Extension +RUN docker-php-ext-install pdo pdo_mysql \ No newline at end of file diff --git a/docker/containers/nginx/conf.d/default.conf b/docker/containers/nginx/conf.d/default.conf new file mode 100644 index 0000000..67638f8 --- /dev/null +++ b/docker/containers/nginx/conf.d/default.conf @@ -0,0 +1,22 @@ +server { + listen 80; + index index.php index.html; + # server_name ; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /var/www/html/public/; + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass php:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + location / { + try_files $uri $uri/ /index.php?$query_string; + } +} diff --git a/docker/docker-compose/docker-compose.yml b/docker/docker-compose/docker-compose.yml new file mode 100644 index 0000000..a0a0dcb --- /dev/null +++ b/docker/docker-compose/docker-compose.yml @@ -0,0 +1,54 @@ +version: '3' + +networks: + web-app: + +services: + # Container: NginX + nginx: + networks: + - web-app + container_name: nginx + image: library/nginx:stable-alpine + ports: + - "9100:80" + volumes: + - ../../src:/var/www/html + - ../../docker/containers/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf + depends_on: + - php + - mysql + + # Container: MySQL + mysql: + networks: + - web-app + container_name: mysql + image: library/mysql:5.7.22 + restart: unless-stopped + tty: true + ports: + - "3306:3306" + volumes: + - ../../docker/containers/mysql:/var/lib/mysql + environment: + MYSQL_DATABASE: db_docker_study + MYSQL_USER: dbUserDev + MYSQL_PASSWORD: dbUserDev123 + MYSQL_ROOT_PASSWORD: dbUserRoot123 + SERVICE_TAGS: dev + SERVICE_NAME: mysql + + # Container: PHP + php: + networks: + - web-app + container_name: php + build: + context: ./ + dockerfile: ../../docker/containers/Dockerfile + image: php:php7.2-fpm-alpine + ports: + - "9000:9000" + volumes: + - ../../src:/var/www/html diff --git a/src/README.md b/src/README.md index f81cab0..891ac07 100644 --- a/src/README.md +++ b/src/README.md @@ -4,3 +4,14 @@ ## Web App/Project Directory - This directory will host the Web App/Project related files. + +### Test Run +``` +// Build and Start Services and Containers +// - see: ./docker/README file for more +docker-compose -f ./docker/docker-compose/docker-compose.yml build +docker-compose -f ./docker/docker-compose/docker-compose.yml up -d + +// Access Web +http://localhost:9100/ +``` \ No newline at end of file diff --git a/src/public/index.html b/src/public/index.html new file mode 100644 index 0000000..38b31ee --- /dev/null +++ b/src/public/index.html @@ -0,0 +1,10 @@ + + + + + Test - NginX + + +

It Works!!!

+ + \ No newline at end of file From a264a413ab6b6d5fc16b2a48bb965abb86c0b462 Mon Sep 17 00:00:00 2001 From: Noel Palo Date: Sun, 22 Aug 2021 12:15:14 +0800 Subject: [PATCH 2/2] update readme file --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 40ab327..8636f49 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,4 @@ ## Table of Contents | Branch Name | What's Inside | | ------------- | ------------- | -| master | Documentation | | webstack/v1.0.0 | nginx:stable-alpine, mysql:5.7.22, php:7.2-fpm-alpine | \ No newline at end of file