Dockerizing laravel application
Dockerizing a Laravel application involves creating a Docker image that includes all the necessary components to run the application. Here are the steps to Dockerize a Laravel application:
-
Install Docker and Docker Compose: Before you begin, make sure you have Docker and Docker Compose installed on your system. You can download Docker from the official website (https://www.docker.com/) and follow the installation instructions. Docker Compose is included in the Docker installation.
-
Create a new Laravel project: If you haven't already, create a new Laravel project using Composer. You can do this by running the following command in your terminal:
composer create --prefer-dist laravel/laravel project-name
- Create a Dockerfile: In the root directory of your Laravel project, create a new file called Dockerfile. This file will contain the instructions for building the Docker image. Here's an example Dockerfile for a Laravel application:
# Use an official PHP runtime as a parent image
FROM php:7.3-fpm
# Set the working directory in the container to /app
WORKDIR /app
# Add the current directory contents into the container at /app
ADD . /app
# Install dependencies
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libzip-dev \
zlib1g-dev
# Install Laravel and its dependencies
RUN docker-php-ext-install -j0 pdo pdo_mysql mbstring zip opcache gd xml xmlsec zlib pcntl
RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN composer install --no-autoloader --no-scripts
# Expose the port that will be used to access the application
EXPOSE 8000
# Define the command that will be run when the container launches
CMD ["php-fpm"]
This Dockerfile uses the official PHP 7.3 image as a base and installs the necessary dependencies for Laravel. It also installs Xdebug for debugging and exposes port 8000 for accessing the application.
- Create a docker-compose.yml file: In the root directory of your Laravel project, create a new file called docker-compose.yml. This file will define the services that will be run in the containers and how they will be linked together. Here's an example docker-compose.yml file for a Laravel application:
version: '3.8'
services:
web:
build: .
container_name: laravel
ports:
- "80:8000"
- "2222:22"
volumes:
- .:/app
depends_on:
- db
environment:
- DB_HOST=db
- DB_DATABASE=laravel
- DB_USERNAME=laravel
- DB_PASSWORD=laravel
command: php artisan serve --host 0.0.0.0
db:
image: mysql:5.7
container_name: laravel-db
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD=laravel
MYSQL_DATABASE=laravel
MYSQL_USER=laravel
MYSQL_PASSWORD=laravel
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
This docker-compose.yml file defines two services: web and db. The web service builds the Docker image using the Dockerfile in the current directory and exposes port 80 for accessing the application. It also mounts the current directory as a volume and links it to the container's /app directory. The db service uses the official MySQL image and sets up the necessary environment variables and volumes for the database.
- Build the Docker image: In the terminal, navigate to the root directory of your Laravel project and run the following command to build the Docker image:
docker-compose build
This command will build the Docker image using the instructions in the Dockerfile and the docker-compose.yml file.
- Start the containers: Once the Docker image has been built, you can start the containers by running the following command:
docker-compose up -d
This command will start the web and db containers in detached mode (in the background).
- Access the application: You can access the Laravel application by navigating to http://localhost in your web browser. If you've set up SSL, you can access the application using https://localhost instead.
That's it! You've successfully Dockerized a Laravel application. This setup provides a consistent development environment, makes it easy to deploy the application, and allows for easy scaling and management.