Laravel + Apache + MySQL development environment with Docker and Docker Compose. Based on a dev.to Article and Laravel + Apache + Docker by veevidify.
- Latest versions of Docker and Docker Compose installed.
- On Linux, you need to add your user to the docker group.
- PHP: 8.3-apache (via
laravel-apache/dev:latest) - MySQL: 8.0 (via
mysql:8.0) - phpMyAdmin: 5.2.2 (via
phpmyadmin/phpmyadmin:5.2.2) - Composer: latest (copied into the PHP container)
Clone the repo and create the folder structure for the laravel source code:
git clone https://github.com/rolodoom/laravel-apache.dev.git
cd laravel-apache.dev
mkdir -p app dbCopy env.example to .env and modify it to your needs:
cp env.example .envBuild the images and start the services:
docker compose build --no-cache
docker compose up -dIn this template, the
app/folder is ignored by Git. When creating a new project, Laravel should be installed inside this folder in your local clone.
Create Laravel Project:
./composer create-project laravel/laravel .Modify Laravel src/.env file to match your container credentials:
APP_URL=http://localhost:<APP_PORT>
DB_CONNECTION=mysql
DB_HOST=mysql-db
DB_PORT=<MYSQL_PORT>
DB_DATABASE=laravel
DB_USERNAME=dbuser
DB_PASSWORD=secret
Fix permissions:
./fix-permissionsInitialize Laravel:
./php-artisan key:generate
./php-artisan migrate
./php-artisan config:cacheNOTE: To regenrate DB use ./php-artisan migrate:fresh --seed
The project can be accessed at http://localhost:<APP_PORT>
You can also visit http://localhost:<PHPMYADMIN_PORT> to access phpMyAdmin after starting the containers.
The default username is root, and the password is the same as supplied in the .env file.
Run any composer command inside the ${PROJECT_NAME}-app container, example:
./composer create-project laravel/laravel .
./composer dump-autoloadThe ./container script is a helper utility to interact with the
${PROJECT_NAME}-app container.
When executed without arguments, it opens an interactive shell inside
the container as user devuser (UID 1000), matching the host user to
avoid permission issues.
./containerThis gives you a Bash session inside the application container.
You can also pass any command as arguments to execute it directly inside the container:
./container vendor/bin/pint
./container ls -alAll commands are executed as devuser inside the ${PROJECT_NAME}-app
container.
This ensures consistency with the containerized environment and avoids relying on host-installed tools.
Running ./database will connect to your ${PROJECT_NAME}-db container's daemon using mysql client.
Run ./database_backup to backup your database inside the ${PROJECT_NAME}-db container's daemon using mysqldump client. The backup file will be stored inside the ./backup folder as <DB_DATABASE>_backup_YYYYMMDD_HHMMSS.sql.
./database_backupRunning ./fix-permissions will fix storage and bootstrap/cache permissions in /var/www/html folder inside the container.
Running ./php-artisan will take you inside the ${PROJECT_NAME}-app container and run any php artisan command, example:
./php-artisan make:controller BlogPostController --resourceRun ./vendor/bin/phpunit to execute tests inside the ${PROJECT_NAME}-app container, example:
./phpunit --group=failingHave a bug or an issue with this template? Open a new issue here on GitHub.
This code in this repository is released under the MIT license, which means you can use it for any purpose, even for commercial projects. In other words, do what you want with it. The only requirement with the MIT License is that the license and copyright notice must be provided with the software.