Docker compose for Laravel v11 project.
Docker Compose is a tool for defining and running multi-container Docker applications. It simplifies the process of managing multiple containers that make up a single application. In this answer, we will discuss how to use Docker Compose for a Laravel v11 project.
First, let's make sure you have the required tools installed. You will need Docker, Docker Compose, and PHP installed on your system. You can download Docker and Docker Compose from the official Docker website (https://www.docker.com/). PHP can be installed using a package manager like Homebrew (on macOS) or using a installer like XAMPP or WAMP.
Once you have installed the required tools, let's create a new Laravel project using the following command:
composer create --prefer-dist laravel/laravel project-name
Next, let's create a docker-compose.yml
file in the root directory of your Laravel project. This file will define the services that make up your application. Here's an example docker-compose.yml
file for a Laravel v11 project:
version: '3.8'
services:
web:
build:
context: .
dockerfile: .docker/Dockerfile
container_name: project_name_web
image: project-name:web
ports:
- "80:80"
- "443:443"
- "32768:32768"
- "32770:32770"
- "32771:32771"
- "32772:32772"
- "32773:32773"
- "32774:32774"
- "32775:32775"
- "32776:32776"
- "32777:32777"
volumes:
- .:/var/www/html
- /var/run/docker.sock:/var/run/docker.sock
- ./php/php.ini:/usr/local/etc/php/php.ini
- ./php/extensions:/usr/local/lib/php/extensions
- ./storage:/var/www/html/storage
- ./bootstrap/cache:/var/www/html/bootstrap/cache
- ./public:/var/www/html/public
environment:
- DB_DATABASE=database_name
- DB_USERNAME=database_user
- DB_PASSWORD=database_password
- DB_HOST=db
- MAIL_MAILER=smtp
- MAIL_HOST=smtp.gmail.com
- MAIL_PORT=587
- [email protected]
- MAIL_PASSWORD=your_email_password
- APP_KEY=base64:your_app_key
- Laravel_SAFE_MODE=false
depends_on:
- db
command: php artisan serve --host 0.0.0.0 --port 80
db:
image: mysql:8.0
container_name: project_name_db
volumes:
- db_data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=database_password
- MYSQL_DATABASE=database_name
- MYSQL_USER=database_user
- MYSQL_PASSWORD=database_password
ports:
- "3306:3306"
volumes:
db_data:
This docker-compose.yml
file defines two services: web
and db
. The web
service is built using the Dockerfile
in the .docker
directory, and the db
service uses the official MySQL image. The web
service depends on the db
service, meaning that the db
service must be started before the web
service.
The web
service also sets several environment variables, including the Laravel application key, database connection details, and email settings. It also mounts several directories and volumes, including the Laravel project directory, the php.ini
file, and the storage
directory.
The db
service sets its own environment variables for the MySQL root password and database details. It also mounts a volume for the MySQL data.
Finally, the volumes
section defines a volume for the MySQL data.
To start your Laravel application using Docker Compose, run the following command in the root directory of your Laravel project:
docker-compose up -d
This command will build the web
service if necessary, start all services, and run them in the background. Your Laravel application should now be accessible at http://localhost
.
That's it! You have successfully set up Docker Compose for a Laravel v11 project. This setup can be extended to include other services, such as Redis or a reverse proxy, as needed.