Docker has gained significant popularity in the software development community for its ability to simplify the deployment and management of applications. In this blog post, we will guide you through the process of setting up Docker on XAMPP for Laravel development. By leveraging the power of Docker containers, you can streamline your Laravel development environment and enhance collaboration with your team. Let’s get started!
Step 1: Install Docker
Before diving into the setup process, ensure that Docker is installed on your system. Visit the official Docker website (https://www.docker.com/get-started) to download and install Docker based on your operating system. Look at the installation guidelines provided for your particular platform.
Step 2: Create a New Laravel Project
Using the command line, navigate to your preferred directory and create a new Laravel project. Execute the following command.
composer create-project --prefer-dist laravel/laravel myproject
This command will create a fresh Laravel project named “myproject” with all the necessary files and dependencies.
Step 3: Configure the Docker Environment
To configure the Docker environment for your Laravel project, follow these steps:
- Create a Dockerfile
In the root directory of your Laravel project, create a new file named Dockerfile. Open the file in a text editor and add the following content.
FROM php:7.4-apache # Set the working directory WORKDIR /var/www/html # Copy existing application directory contents to the working directory COPY . /var/www/html # Install dependencies RUN apt-get update && \ apt-get install -y \ libzip-dev \ zip \ unzip \ && docker-php-ext-install zip # Enable Apache mod_rewrite RUN a2enmod rewrite # Update Apache configuration COPY docker/apache.conf /etc/apache2/sites-available/000-default.conf # Install Composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Install Laravel dependencies RUN composer install --no-scripts # Generate Laravel key RUN php artisan key:generate # Set file permissions RUN chown -R www-data:www-data /var/www/html/storage # Expose port 80 EXPOSE 80
- Create an Apache Configuration File
Create a new directory named docker within the root directory of your Laravel project. Inside the docker directory, create a file named apache.conf and add the following contents.
<VirtualHost *:80> DocumentRoot /var/www/html/public <Directory /var/www/html> AllowOverride All </Directory> </VirtualHost>
Step 4: Build and Run the Docker Container
Now, it’s time to build and run the Docker container for your Laravel project. Follow these steps:
- Build the Docker Container
In the root directory of your Laravel project (where the Dockerfile is located), open the terminal or command prompt and execute the following command
docker build -t myproject .
This command will build the Docker container using the Dockerfile configuration. Replace myproject with the desired name for your container.
- Run the Docker Container
Once the container is built, run it using the following command
docker run -p 8080:80 myproject
This command runs the Docker container and maps port 8080 on your local machine to port 80 inside the container. Feel free to change the port mapping if desired.
Step 5: Access Your Laravel Application
Congratulations! Your Laravel application is now running within the Docker container. To access it, open your web browser and visit http://localhost:8080. You should see your Laravel project up and running.
Conclusion
In this blog post, we explored the process of setting up Docker on XAMPP for Laravel development. Docker provides a convenient and efficient way to manage your Laravel environment, ensuring consistency across different systems. By following these steps, you can streamline your development workflow, improve collaboration, and deploy your Laravel applications with ease. Embrace the power of Docker and elevate your Laravel development experience today!