Laravel Unleashed: Effortless Deployment with Docker

Cargo Ship

Introduction

Imagine acquiring a new laptop – would you prefer to configure all settings manually, or start developing with just a single command? This challenge also exists in production environments, and Docker streamlines the setup process to address it effectively.

What is Docker

Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. The service has both free and premium tiers. The software that hosts the containers is called Docker Engine. It was first started in 2013 and is developed by Docker, Inc.

-- Wikipedia

What is Docker Container

A Docker container is a standardized, encapsulated environment that runs applications. A container is managed using the Docker API or CLI.

-- Wikipedia

What is Docker Image

A Docker image is a read-only template used to build containers. Images are used to store and ship applications.

-- Wikipedia

Download Docker Desktop App

To begin using Docker, visit the Docker website and download the Docker Desktop application.

Setup Docker Compose for Local Development

Prepare your Laravel app for local development using Laravel Sail with either Composer or the Laravel Sail Docker image:

docker run --rm --interactive --tty \
--user "$(id -u):$(id -g)" \
--volume "$(pwd):/var/www/html" \
--workdir /var/www/html \
laravelsail/php82-composer:latest \
composer require laravel/sail --dev

Then, publish the docker-compose file (select all the services your app requires):

docker run --rm --interactive --tty \
--user "$(id -u):$(id -g)" \
--volume "$(pwd):/var/www/html" \
--workdir /var/www/html \
laravelsail/php82-composer:latest \
php artisan sail:install

Next, start the app services by using the Sail command:

./vendor/bin/sail up -d

Now, you can utilize PHP, Composer, Artisan, or npm commands through the Sail binary. Proceed to install and build your front-end dependencies, and migrate your database:

# Install NPM dependencies
./vendor/bin/sail npm install
 
# Build the front-end
./vendor/bin/sail npm run build
 
# Migrate the database
./vendor/bin/sail php artisan migrate

Containerize Laravel app using Dockerfile

The Dockerfile utilizes specific instructions, such as RUN, to install, build, and copy the application files.

The Dockerfile has Multi-stage builds feature, with multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

Create a new file with Dockerfile name to containerize the app:

# Node image only used to install and build NPM dependencies
FROM node:20 AS node
 
WORKDIR /app
 
# Copy NPM files
COPY resources resources
COPY package*.json *.config.js ./
 
# Install and build NPM dependencies
RUN npm install &&\
npm run build
 
# Base image
FROM php:8.2-apache
 
# Install addiotnal dependencies (not included in the base image)
RUN apt-get update && \
apt-get install -y \
libzip-dev \
zip
 
# Enable mod_rewrite
RUN a2enmod rewrite
 
# Specify document root dir
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
 
# Changing apache DocumentRoot value
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
 
# Install redis ext
RUN pecl channel-update pecl.php.net && \
pecl install redis-5.3.7 && \
rm -rf /tmp/pear
 
# Install addiotnal php ext (not included in the base image)
RUN docker-php-ext-install \
pdo_mysql \
bcmath \
zip
 
# Enable redis ext
RUN docker-php-ext-enable redis
 
# Copy files and set premissions
COPY --chown=www-data:www-data . /var/www/html
COPY --chown=www-data:www-data --from=node app/resources resources
COPY --chown=www-data:www-data --from=node app/public/build public/build
 
# Copy the Composer PHAR from the Composer image
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
 
# Install Composer dependencies
RUN composer install --optimize-autoloader --no-dev

The .dockerignore file is utilized to exclude files or directories from being added to the image. For instance, we can use it to ignore the git directory, as it is not necessary for the image.

/.git
/node_modules
/tests
/vendor

Conclusion

In conclusion, containerizing a Laravel application with Docker simplifies the development and deployment process. By using Docker Compose, Laravel Sail, and a Dockerfile, you can efficiently manage your application's environment and dependencies, ensuring a consistent and streamlined experience across different platforms.