Docker Compose setup for MySQL, PHP 8, Apache, and Laravel

Docker Compose setup for MySQL, PHP 8, Apache, and Laravel

Here's a tutorial for creating a Docker Compose setup for MySQL, PHP 8, Apache, and Laravel:

1. Prerequisites:

  • Ensure you have Docker and Docker Compose installed on your system.

2. Create a Project Directory:

  • Create a new directory for your project, e.g., my-laravel-app.

3. Create the docker-compose.yml File:

  • Inside the project directory, create a file named docker-compose.yml with the following content:
version: "3.8"

services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: your_mysql_root_password
      MYSQL_DATABASE: your_laravel_database
      MYSQL_USER: your_laravel_user
      MYSQL_PASSWORD: your_laravel_password
    volumes:
      - dbdata:/var/lib/mysql

  app:
    build: .
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www/html
    depends_on:
      - db

volumes:
  dbdata:

4. Create the Dockerfile:

  • In the same directory, create a file named Dockerfile with the following content:
FROM php:8.0-apache

RUN apt-get update && apt-get install -y \
    libpng-dev \
    zlib1g-dev \
    libzip-dev \
    libicu-dev \
    libonig-dev \
    zip \
    unzip \
    curl

RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo_mysql gd zip intl opcache

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

WORKDIR /var/www/html

5. Start the Containers:

  • Open a terminal in your project directory and run the following command:
docker-compose up -d

6. Install Laravel (if not already installed):

  • If you don't have an existing Laravel project, create one inside the my-laravel-app directory:
docker-compose exec app composer create-project laravel/laravel .

7. Access Laravel:

  • Once the containers are up and running, open your web browser and access your Laravel application at http://localhost:8080.

Additional Notes:

  • Customize the environment variables in the docker-compose.yml file with your desired MySQL credentials and database name.

  • Modify the Dockerfile if you need additional PHP extensions or other dependencies.

  • Use docker-compose exec app bash to access the PHP container's shell for further commands.

Did you find this article valuable?

Support The art of Code by becoming a sponsor. Any amount is appreciated!