Write down Docker Compose file for Laravel and MySQL application
Here is an example Docker Compose file for a Laravel application with a MySQL database:
version: '3'
services:
# Laravel Application
app:
image: composer:latest
volumes:
- ./laravel-app:/var/www
working_dir: /var/www
command: bash -c "composer install && php artisan serve --host=0.0.0.0 --port=8000"
ports:
- "8000:8000"
depends_on:
- mysql
environment:
DB_CONNECTION: mysql
DB_HOST: mysql
DB_PORT: 3306
DB_DATABASE: laravel_db
DB_USERNAME: laravel_user
DB_PASSWORD: secret
# MySQL Database
mysql:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: laravel_db
MYSQL_USER: laravel_user
MYSQL_PASSWORD: secret
ports:
- "3306:3306"
volumes:
- ./mysql-data:/var/lib/mysql
Explanation:
The
app
service uses the officialcomposer:latest
image for Laravel dependencies and running the development server. It connects thelaravel-app
folder to the container and sets up environment variables for the MySQL database.The
mysql
service uses the officialmysql:latest
image, with environment variables for the MySQL root password, database name, and user details. It also connects a volume (./mysql-data
) to save MySQL data.Both services are in the same network, so they can talk to each other using their service names (
app
andmysql
).
To use this Docker Compose file:
Create a directory for your Laravel application and place this
docker-compose.yml
file in the root of that directory.Run the following command to start the containers:
docker-compose up
Access your Laravel application at
http://localhost:8000
.To stop and remove the containers, use:
docker-compose down
Be sure to change the settings to fit your Laravel app's needs. Also, think about using environment files for private information and setup.