ivankia / dockerized_laravel_api_boilerplate

Dockerized Laravel API boilerplate (PHP, Nginx, MySql, Composer, Node, PHP Artisan)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dockerized Laravel API boilerplate (PHP, Nginx, MySql, Composer, Node, PHP Artisan)

Introduction

  • Include Persistant MySQL Storage and Custom Nginx configuration for Laravel.
  • Nginx and MySQL expose Ports 8080 and 4306 respectively.

Docker Images installed

  1. PHP, php:7.4.0-fpm-alpine
  2. Nginx, nginx:stable-alpine
  3. MySQL, mysql:5.7.22
  4. Composer, composer:latest
  5. Node, node:14.1.0
  6. PHP Artisan

Running the Containerized Laravel application

  1. Clone the repository.
  2. Rename the ".env.example" file in the root directory to ".env".
  3. Generate the new application key as follows:
php artisan key:generate
  1. Run the containers:
  • Running the containers in detached mode:
docker-compose up -d
  • Run the containers in interactive mode:
docker-compose up -it
  • Rebuild the containers everytime and run in detached mode:
docker-compose up -d --build
  1. Create Migration and Seed the User's table as follows:
docker-compose exec php php artisan migrate
  1. Open any Web browser or Postman to test the API with the following GET requests:
  1. Stop and destroy the containers:
docker-compose down

Other useful Commands

  • Composer
docker-compose run --rm composer install your-vendor-name/package-name
  • NPM
docker-compose run --rm npm install
docker-compose run --rm npm run dev
  • Artisan
docker-compose exec php php artisan make:model Test -a
  • Artisan manually
docker container exec -it php ash
php artisan make:model Test -a
  • Docker
docker compose build
docker compose up
docker-compose down
docker ps
docker ps -a
docker container rm container-name
docker container rm container-id
docker container rm container-id -f
docker images
docker image rm image-name
docker image rm image-id
docker image rm image-id -f

Steps to Containerize any Laravel application from scratch

  1. Install Docker.
  2. Install Composer.
  • NOTE: Locally installed Composer is only be used to create the initial laravel project and dockerized environment will actually use the Composer container.
  1. Create directories mysql and nginx.
  2. Create Dockerfile, docker-compose.yml and dockerignore files by copying the content from this repository.
  3. Create a new laravel project using Composer.
  4. Run the containers:
  • Running the containers in detached mode:
docker-compose up -d
  • Run the containers in interactive mode:
docker-compose up -it
  • Rebuild the containers everytime and run in detached mode:
docker-compose up -d --build
  1. Stop and destroy the containers:
docker-compose down

About

Dockerized Laravel API boilerplate (PHP, Nginx, MySql, Composer, Node, PHP Artisan)


Languages

Language:VBA 81.0%Language:PHP 18.2%Language:HTML 0.7%Language:PLSQL 0.2%Language:Dockerfile 0.0%