Laravel docker for api

64 Views Asked by At

I'm using Windows 11 with XAMPP, which has PHP version 7.3 installed. However, I want to create a new Laravel project with the latest version (currently 10) along with the Passport package, MySQL (latest), and Redis (latest) using Docker and Docker Compose. I already have an SQL file on my system, so when the Docker image is built, the SQL file should be imported into the database. Additionally, I want both XAMPP and Docker to run simultaneously without any port conflicts.

Below is the Dockerfile I've configured, but it's not working as expected. Every time I fix one issue, another one pops up. Could someone provide a corrected Docker and Docker Compose file for my requirements?

Dockerfile:

Use an official PHP runtime
FROM php:8.2-apache

Enable Apache modules
RUN a2enmod rewrite

RUN apt-get update && apt-get install -y \
    git \
    unzip \
    libzip-dev \
    default-mysql-client \
    && docker-php-ext-install pdo pdo_mysql zip

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

Install Laravel Installer globally
RUN composer global require laravel/installer

Set the working directory to /var/www/html
WORKDIR /var/www/html

Check if Laravel project exists, if not then create a new Laravel project
RUN if [ ! -d "app-api" ]; then \
    composer create-project --prefer-dist laravel/laravel app-api; \
fi

Copy existing application directory contents
COPY ./app-api .

Run migrations and Passport install
RUN cd app-api && php artisan migrate

Generate autoload files
RUN composer dump-autoload --optimize

Expose port 80
EXPOSE 80

Start Apache
CMD ["apache2ctl", "-D", "FOREGROUND"]

docker-compose.yml:

version: '3'

services:
  appadmin-api:
    build:
      context: .
    ports:
      - "8082:80"  # Change the port mapping for Apache
    volumes:
      - ./appadmin-api:/var/www/html
    environment:
      - APACHE_DOCUMENT_ROOT=/var/www/html/public
    depends_on:
      - mysql
      - phpmyadmin

  mysql:
    image: mysql:8
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - "3307:3306"
    environment:
      MYSQL_ROOT_PASSWORD: appdev@eu7j
      MYSQL_DATABASE: app
      MYSQL_USER: appdev
      MYSQL_PASSWORD: appdev@eu7j
    volumes:
      - ./mysql/initdb/app.sql:/docker-entrypoint-initdb.d/app.sql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "8083:80"
    environment:
      PMA_HOST: mysql
0

There are 0 best solutions below