Laravel Unleashed: Effortless Deployment with Docker
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 dependenciesFROM node:20 AS node WORKDIR /app # Copy NPM filesCOPY resources resourcesCOPY package*.json *.config.js ./ # Install and build NPM dependenciesRUN npm install &&\ npm run build # Base imageFROM 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_rewriteRUN a2enmod rewrite # Specify document root dirENV APACHE_DOCUMENT_ROOT /var/www/html/public # Changing apache DocumentRoot valueRUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.confRUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf # Install redis extRUN 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 extRUN docker-php-ext-enable redis # Copy files and set premissionsCOPY --chown=www-data:www-data . /var/www/htmlCOPY --chown=www-data:www-data --from=node app/resources resourcesCOPY --chown=www-data:www-data --from=node app/public/build public/build # Copy the Composer PHAR from the Composer imageCOPY --from=composer:2 /usr/bin/composer /usr/bin/composer # Install Composer dependenciesRUN 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.