Laravel is a popular PHP framework known for its robustness and ease of use. Docker, on the other hand, is a platform that allows developers to build, package, and distribute applications using containerization. In this article, we will guide you through the process of setting up a Laravel Docker environment for efficient development. By leveraging the power of Docker, you can ensure consistent and reproducible development environments across different machines, streamline the deployment process, and facilitate collaboration with other team members. So, let’s get started!
Prerequisites:
- Make sure you have Docker and Docker Compose installed on your machine. If not, you can download and install them from the official Docker Website.
- You should have basic knowledge of Laravel and its directory structure.
Step 1: Create a new Laravel project
To begin, open your terminal or command prompt and navigate to the desired directory where you want to create your Laravel project. Run the following command:
$ docker run --rm -v $(pwd):/app composer create-project --prefer-dist laravel/laravel project-name
This command will create a new Laravel project in a directory named “project-name”.
Step 2: Create a Docker Compose file
In the root directory of your Laravel project, create a new file named “docker-compose.yml”. This file will define the services required for your Docker environment. Add the following content to the file:
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: laravel_app
ports:
- "8000:8000"
volumes:
- .:/app
depends_on:
- db
environment:
- DB_HOST=db
- DB_DATABASE=laravel
- DB_USERNAME=root
- DB_PASSWORD=secret
db:
image: mysql:5.7
environment:
- MYSQL_DATABASE=laravel
- MYSQL_ROOT_PASSWORD=secret
volumes:
- ./mysql:/var/lib/mysql
This file defines two containers: “app” and “db” for your Laravel application and MySQL database, respectively. The “app” container is built from a Dockerfile that will be created in the next step.
Step 3: Create a Dockerfile
In the root directory of your Laravel project, create a new file named “Dockerfile”. This file will contain instructions for building the Docker image for your Laravel application. Add the following content to the file:
FROM php:7.4-apache
RUN docker-php-ext-install pdo_mysql
RUN a2enmod rewrite
This Dockerfile uses the official PHP 7.4 image with Apache as the web server. IT installs the pdo_mysql extension and enables the mod_rewrite Apache module.
Step 4: Build and run the Docker containers
Open your terminal or command prompt and navigate to the root directory of your Laravel project. Run the following command:
$ docker-compose up --build
This command will build the Docker image for your Laravel application based on the Dockerfile and start the containers specified in the docker-compose.yml file.
Step 5: Access your Laravel application
Once the Docker containers are up and running, you can access your Laravel application by opening your web browser and navigating to http://localhost:8000. You should see the Laravel welcome page indicating that your application is working correctly.
FAQs:
Q: How do I stop the Docker containers?
A: To stop the containers, open your terminal or command prompt and press Ctrl+C. Alternatively, you can run the following command:
$ docker-compose down
Q: How can I access the MySQL database inside the Docker container?
A: You can use a database management tool like phpMyAdmin or MySQL Workbench to connect to the MySQL database running inside the Docker container. Configure the tool to connect to “localhost” on port 3306 with the username “root” and password “secret” specified in the docker-compose.yml file.
Q: How can I install additional PHP extensions?
A: If you need to install additional PHP extensions, you can add the necessary commands to the Dockerfile and rebuild the Docker image using the “docker-compose up –build” command. Make sure to run the appropriate installation commands for the desired extensions.
Q: How can I modify the Laravel environment variables?
A: You can modify the Laravel environment variables by editing the “.env” file in the root directory of your Laravel project. After making the necessary changes, rebuild the Docker image using the “docker-compose up –build” command to apply the modifications.