Skip to content

Instantly share code, notes, and snippets.

@devinci-it
Created June 2, 2024 02:15
Show Gist options
  • Select an option

  • Save devinci-it/e08a618f6fd92e8aa65cc68fddfdffcd to your computer and use it in GitHub Desktop.

Select an option

Save devinci-it/e08a618f6fd92e8aa65cc68fddfdffcd to your computer and use it in GitHub Desktop.
DOCKER file setup and config files for php/laravel local development environement.

Laravel Docker Development Environment

This repository provides a Docker-based development environment for Laravel projects. It includes a Dockerfile, docker-compose.yaml, and a setup script to quickly get you up and running with a new Laravel project.

Prerequisites

Before you begin, make sure you have the following installed on your local machine:

Getting Started

Step 1: Create a Laravel Project

Run the following command to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel <project-name>

Replace <project-name> with your desired project name.

Step 2: Set Up the Docker Environment

Use the provided setup script to configure Docker for your Laravel project:

# Clone this repository or download the Dockerfile and docker-compose.yaml
# Replace <gist-url> with your actual Gist URL
curl -sS https://gist.githubusercontent.com/yourgisturl/setup.sh | bash

Step 3: Update Environment Variables

After running the setup script, update the .env file in your Laravel project with the following MySQL settings:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=<project-name>
DB_USERNAME=<project-name>user
DB_PASSWORD=<project-name>password

Replace <project-name>, <project-name>user, and <project-name>password with your chosen values.

Step 4: Build and Run Docker Containers

Build and run the Docker containers using docker-compose:

docker-compose up -d --build

Step 5: Access Your Laravel Application

Your Laravel application is now running in Docker:

  • Access the application at http://localhost:8000
  • SSH into the app container: docker-compose exec app bash
  • MySQL Database: <project-name>
  • MySQL Username: <project-name>user
  • MySQL Password: <project-name>password

Step 6: Additional Configuration

  • Apache Reverse Proxy: If needed, configure Apache as a reverse proxy to the Docker container. Sample configuration is provided in the Gist.
  • SSL Certificates: If using HTTPS, generate and configure SSL certificates as described in the Gist.

Contributing

Feel free to fork this repository and customize it to fit your specific Laravel project needs. Pull requests are welcome!

License

This project is licensed under the MIT License.

# compose.yaml - Docker Compose file for PHP Laravel Development Environment
#
# This Docker Compose file defines a PHP Laravel development environment with
# PHP 8.3, Composer, Node.js with npm, Laravel Installer, and MariaDB.
#
# Services:
# - app: PHP Laravel application service
# - db: MariaDB database service
#
# Usage:
# 1. Update the environment variables MYSQL_ROOT_PASSWORD, MYSQL_DATABASE,
# MYSQL_USER, and MYSQL_PASSWORD as needed for your project.
#
# 2. Run Docker Compose to start the services:
# docker-compose -f compose.yaml up -d
#
# 3. Access the Laravel development server at http://localhost:8000
#
# 4. SSH into the app container:
# docker-compose -f compose.yaml exec app bash
#
# 5. Stop and remove containers, networks, and volumes:
# docker-compose -f compose.yaml down -v
#
version: '3'
services:
app:
build:
context: .
dockerfile: DOCKER
ports:
- "8000:8000"
- "2222:22"
volumes:
- .:/var/www
depends_on:
- db
environment:
- MYSQL_HOST=db
- MYSQL_DATABASE=myapp
- MYSQL_USER=myappuser
- MYSQL_PASSWORD=mypassword
db:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: myapp
MYSQL_USER: myappuser
MYSQL_PASSWORD: mypassword
ports:
- "3306:3306"
volumes:
- mariadb_data:/var/lib/mysql
volumes:
mariadb_data:
; ******************************************************************************
; * Dockerfile for PHP Laravel Development Environment
; *
; * This Dockerfile creates a development environment for PHP Laravel projects.
; * It includes PHP 8.3, Composer, Node.js with npm, and Laravel Installer.
; *
; * Arguments:
; * - PROJECT_NAME: The name of the project/application.
; *
; * Environment Variables:
; * - APP_NAME: The name of the application.
; * - MYSQL_USER: MySQL database username.
; * - MYSQL_PASSWORD: MySQL database password.
; * - MYSQL_DATABASE: MySQL database name.
; *
; * Exposed Ports:
; * - 22: SSH port
; * - 8000: Laravel development server port
; *
; * Volumes:
; * - /var/www: Application code volume
; *
; * Usage:
; * 1. Build the Docker image:
; * docker build --build-arg PROJECT_NAME=myapp -t laravel-dev .
; *
; * 2. Run the Docker container:
; * docker run -p 8000:8000 -p 2222:22 --name laravel-container -v $(pwd):/var/www laravel-dev
; *
; ******************************************************************************
; ******************************************************************************
; * Base Image and Environment Setup
; ******************************************************************************
FROM ubuntu:20.04
; ******************************************************************************
; * Arguments and Environment Variables
; ******************************************************************************
ARG PROJECT_NAME=myapp
ENV APP_NAME=${PROJECT_NAME}
ENV MYSQL_USER=${PROJECT_NAME}user
ENV MYSQL_PASSWORD=${PROJECT_NAME}password
ENV MYSQL_DATABASE=${PROJECT_NAME}
; ******************************************************************************
; * Installing Dependencies
; ******************************************************************************
RUN apt-get update && apt-get install -y \
software-properties-common \
&& add-apt-repository ppa:ondrej/php \
&& apt-get update && apt-get install -y \
php8.3 \
php8.3-mysql \
php8.3-xml \
php8.3-mbstring \
php8.3-curl \
php8.3-zip \
unzip \
curl \
git \
openssh-server \
&& apt-get clean
; ******************************************************************************
; * Installing Composer and NVM
; ******************************************************************************
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash \
&& export NVM_DIR="$HOME/.nvm" \
&& [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" \
&& nvm install 20 \
&& nvm use 20 \
&& nvm alias default 20
; ******************************************************************************
; * Installing Node.js and npm
; ******************************************************************************
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get install -y nodejs \
&& npm install -g npm@latest
; ******************************************************************************
; * Installing Laravel Installer
; ******************************************************************************
RUN composer global require laravel/installer
; ******************************************************************************
; * Creating User and Group
; ******************************************************************************
RUN groupadd -r ${PROJECT_NAME} && useradd -r -g ${PROJECT_NAME} ${PROJECT_NAME}
; ******************************************************************************
; * Setting Working Directory and Copying Application Code
; ******************************************************************************
WORKDIR /var/www
COPY . .
; ******************************************************************************
; * Installing Application Dependencies
; ******************************************************************************
RUN composer install
RUN npm install && npm update
; ******************************************************************************
; * Exposing Ports and Configuring SSH
; ******************************************************************************
EXPOSE 22
EXPOSE 8000
RUN echo 'root:password' | chpasswd
RUN ssh-keygen -A
; ******************************************************************************
; * Running Services
; ******************************************************************************
CMD service ssh start && php artisan serve --host=0.0.0.0 --port=8000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment