Deploying a Laravel PHP App with PostgreSQL Using Docker and Docker Compose

Deploying a Laravel PHP App with PostgreSQL Using Docker and Docker Compose

Deploying a Laravel PHP application with a PostgreSQL database using Docker and Docker Compose is an efficient way to manage and scale web applications.

Docker allows you to package the application and its dependencies into a container, ensuring consistency across different environments.

Docker Compose simplifies the orchestration of multiple containers, such as the application server and the database.

Requirements:

  1. Docker: Docker must be installed on your machine. Docker provides the containerization platform that allows you to run your application and database in isolated environments.
  2. Docker Compose: Docker Compose must be installed to manage multi-container Docker applications. With Compose, you can define and run your multi-container application using a YAML file.
  3. Laravel Web App Project: A Laravel application should be initialized or available in the project directory. If not already created, you can create a new Laravel project using Composer.
  4. PostgreSQL: A PostgreSQL database image will be used for the database service. Ensure that your Laravel application is configured to connect to a PostgreSQL database.

Steps to Deploy:

    • The Dockerfile defines the environment where your application will run. It includes instructions to install PHP, Composer, and Laravel dependencies.
    • This file defines the services that make up your application, including the web service (Laravel) and the database service (PostgreSQL).
    • Update the .env file in your Laravel project to configure the database connection settings for PostgreSQL.
    • Start the application and database containers using Docker Compose.

Access the Application:

After the containers are up and running, you can access the Laravel application in your browser by navigating to http://localhost:8000 or another configured port.

Run the Application:

docker-compose up -d

Configure Laravel for PostgreSQL:

DB_CONNECTION=pgsql
DB_HOST=db
DB_PORT=5432
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=secret

Create a docker-compose.yml file:

version: '3.8'

services:
  # Application service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: laravel-app
    container_name: laravel-app
    restart: unless-stopped
    working_dir: /var/www
    volumes:
      - .:/var/www
      - ./vendor:/var/www/vendor
      - ./storage:/var/www/storage
    networks:
      - laravel

  # PostgreSQL service
  db:
    image: postgres:13
    container_name: postgres-db
    restart: unless-stopped
    environment:
      POSTGRES_DB: laravel_db
      POSTGRES_USER: laravel_user
      POSTGRES_PASSWORD: secret
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - laravel

# Volumes for persisting data
volumes:
  pgdata:

# Networks for container communication
networks:
  laravel:

Create a Dockerfile for the Laravel application:

FROM php:8.1-fpm

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Install PHP extensions
RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

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

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www

# Change current user to www
USER www-data

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Conclusion

Deploying a Laravel application with PostgreSQL using Docker and Docker Compose offers a modular, consistent, and scalable environment.

It abstracts away the complexities of managing dependencies and environments, enabling seamless development and deployment across various systems.

This approach is especially beneficial in development, CI/CD pipelines, and when scaling applications across different environments.








Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+