5-deploying-a-laravel-application-on-aws-with-docker.html

Deploying a Laravel Application on AWS with Docker

Deploying a Laravel application on AWS using Docker can significantly enhance your development workflow and production deployment. This article will guide you through the process step-by-step, ensuring you understand the definitions, use cases, and actionable insights. Let’s dive in!

What is Laravel?

Laravel is a popular PHP framework designed for building web applications following the model-view-controller (MVC) architectural pattern. It provides an elegant syntax and comes packed with features that streamline development, such as routing, authentication, and caching.

Why Use Docker?

Docker is a containerization platform that allows developers to package applications and their dependencies into containers. This ensures that your application runs consistently across different environments, reducing the "it works on my machine" problem.

Using Docker with AWS (Amazon Web Services) provides several benefits:

  • Scalability: Easily scale your application by running multiple containers.
  • Portability: Move your containers across environments effortlessly.
  • Isolation: Run multiple applications on the same machine without conflicts.

Prerequisites

Before we start, ensure you have the following:

  • An AWS account
  • Docker installed on your local development machine
  • Basic knowledge of Laravel and Docker
  • AWS CLI configured on your local machine

Step 1: Set Up Your Laravel Application

First, create a new Laravel application if you haven't done so already. You can do this using Composer:

composer create-project --prefer-dist laravel/laravel my-laravel-app
cd my-laravel-app

Step 2: Create a Dockerfile

Next, create a Dockerfile in the root of your Laravel application. This file will define the environment in which your application will run.

# Use the official PHP image as a base
FROM php:8.0-fpm

# Set the working directory
WORKDIR /var/www

# Install system dependencies
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Copy existing application directory contents
COPY . .

# Install Laravel dependencies
RUN composer install

# Expose port 9000
EXPOSE 9000

# Run the PHP server
CMD ["php-fpm"]

Step 3: Create a Docker Compose File

Docker Compose allows you to define and run multi-container Docker applications. Create a file named docker-compose.yml in the root directory of your Laravel application.

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "9000:9000"
    volumes:
      - .:/var/www
    networks:
      - laravel_network

  db:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: root
    networks:
      - laravel_network

networks:
  laravel_network:

Step 4: Build and Run Your Containers

Now that you have your Dockerfile and docker-compose.yml, you can build and run your containers.

docker-compose up -d --build

This command will build your application and the database container, running them in detached mode.

Step 5: Configure Your Database

Once the containers are up, you need to configure your Laravel application to connect to the MySQL database. Open your .env file and update the database settings:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=user
DB_PASSWORD=password

Step 6: Migrate Your Database

With your application configured, you should run the Laravel migrations to set up your database structure:

docker-compose exec app php artisan migrate

Step 7: Deploy to AWS

To deploy your application on AWS, you’ll need to push your Docker containers to Amazon Elastic Container Registry (ECR) and then run them using Amazon Elastic Container Service (ECS).

7.1: Create a Repository in ECR

  1. Sign in to the AWS Management Console.
  2. Navigate to ECR and create a new repository.
  3. Note the repository URI.

7.2: Authenticate Docker to Your ECR

Run the following command to authenticate Docker to your ECR:

aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account-id.dkr.ecr.your-region.amazonaws.com

7.3: Tag and Push Your Docker Image

Tag your Docker image:

docker tag my-laravel-app:latest your-account-id.dkr.ecr.your-region.amazonaws.com/my-laravel-app:latest

Push the image to ECR:

docker push your-account-id.dkr.ecr.your-region.amazonaws.com/my-laravel-app:latest

7.4: Create an ECS Task Definition

  1. Navigate to Amazon ECS in the AWS Management Console.
  2. Create a new task definition.
  3. Select the Fargate launch type.
  4. Configure the task to use the Docker image from ECR.

7.5: Run Your ECS Service

  1. Create a new cluster.
  2. Launch a service using the task definition you created.
  3. Set up necessary networking and IAM roles as needed.

Conclusion

Deploying a Laravel application on AWS with Docker streamlines your development and deployment processes, making it easier to manage and scale your applications. By following the steps outlined in this article, you can ensure a smooth deployment experience.

With Docker’s containerization and AWS’s robust infrastructure, your Laravel applications can reach their full potential. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.