Dockerizing a Laravel application in PHP may seem like a challenge, but with the right steps, the process becomes simple and accessible. In this guide, I will show you how to do it effectively, ensuring your application is ready to run in any environment without complications.
Docker is an open-source platform that allows you to build, test, and deploy applications in containers. These containers are lightweight and isolated environments that contain everything needed to run your application, including code, libraries, configurations, and dependencies. Using Docker makes it easier to create consistent development environments and simplifies the deployment process.
Before you begin, make sure you have installed on your machine:
The first step is to create a file named Dockerfile
at the root of your Laravel project. This file specifies how the Docker image for your application will be built. Here is an example of a basic Dockerfile:
# Use the base PHP image with Apache
FROM php:8.0-apache
# Install the necessary PHP extensions
RUN docker-php-ext-install pdo pdo_mysql
# Set the working directory
WORKDIR /var/www/html
# Copy the application code into the container
COPY . .
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install the project dependencies
RUN composer install
# Expose the port for the web server
EXPOSE 80
The next step is to create a file named docker-compose.yml
at the root of your project. This file allows you to define the services that your application will need, such as the database. A simple example looks like this:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:80"
volumes:
- .:/var/www/html
networks:
- app-network
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
networks:
- app-network
networks:
app-network:
driver: bridge
Now that you have the Dockerfile
and docker-compose.yml
files, it’s time to build and run the containers. Open a terminal and navigate to the root of your project, then execute the following command:
docker-compose up -d
This command will download the necessary images, create the containers, and start them in the background. The -d
option stands for "detached," allowing the containers to run without blocking the terminal.
Once your containers are up and running, you can access your Laravel application by entering http://localhost:8080
in your browser. If everything is set up correctly, you should see the Laravel welcome page.
To stop the containers, you can run:
docker-compose down
This command stops and removes the containers but retains the data in your database due to Docker’s configuration.
Dockerizing your Laravel application in PHP is a straightforward process that brings numerous benefits to development and deployment. With this guide, you will be able to set up your environment in no time and without complications.
If you're interested in learning more about useful practices in the world of technology and web development, I invite you to read more news of this kind on my blog. Your next adventure in the world of development awaits!
Page loaded in 24.54 ms