Building a Laravel App Using ServerSideUp Image with Docker
Docker provides a powerful way to containerize your Laravel application, making it easier to manage dependencies, environments, and deployments. In this guide, we'll walk through setting up a Laravel application using a custom Dockerfile, the ServerSideUp image, and Redis integration via Docker Compose.
Step 1: Laravel Setup
Before diving into Docker, make sure you have a basic Laravel project set up. If you haven't already installed Laravel, you can do so with Composer:
This will create a fresh Laravel application in the my-laravel-app
directory.
Step 2: Understanding the Dockerfile
The Dockerfile is the blueprint for building Docker images. Let's break down the Dockerfile we'll be using:
Dockerfile Explained
-
Base Stage for Apache and PHP Extensions:
- We begin by creating a base stage using
php:${PHP_VERSION}-apache
, where we install PHP and Apache, making it easier to extend this stage later.
- We begin by creating a base stage using
-
Adding Redis Extension:
- The next stage (
php-ext-redis
) installs the Redis PHP extension. This stage usespecl
to install Redis and then enables it.
- The next stage (
-
Building Frontend Assets:
- The
node
stage handles frontend dependencies and asset compilation. We copy the necessary frontend files, runnpm install
, and build the assets.
- The
-
Production Image:
- This is the main stage where we prepare the production environment. We install necessary PHP extensions like
pdo_mysql
,bcmath
,mysqli
,zip
, andexif
. Redis is also enabled in this stage. - After copying the application code and built assets, we clean up the workspace, install Composer dependencies with optimization flags, and configure Apache, Supervisor, and Cron jobs.
- This is the main stage where we prepare the production environment. We install necessary PHP extensions like
-
Continuous Integration (CI) Stage:
- The final stage uses the ServerSideUp CLI image, configured with extended memory and additional PHP extensions like
intl
,gd
,xsl
,exif
, andpcov
. This stage is tailored for running tests or CI processes.
- The final stage uses the ServerSideUp CLI image, configured with extended memory and additional PHP extensions like
Step 3: Adding Redis and Setting Up docker-compose.yml
To fully utilize Redis and manage your Laravel application's environment, you'll want to add Redis to the Docker Compose setup and streamline the environment configuration.
Here’s the updated docker-compose.yml
file:
Adding Redis
- The
redis
service is added to thedocker-compose.yml
file, running the Redis server in a container. This service is lightweight and uses thealpine
variant of Redis for efficiency.
Step 4: Running the Application
Once your docker-compose.yml
is set up, and your Dockerfile is ready, you can bring up your application with the following command:
This command starts up all the services defined in the docker-compose.yml
, including your Laravel app, MySQL, and Redis. The environment variables are loaded from the .env
file, making configuration management straightforward.
Step 5: Testing Redis Integration
To test if Redis is working correctly with your Laravel application, you can try caching something via the Laravel Artisan CLI:
In the Tinker shell:
If Redis is properly integrated, you'll be able to store and retrieve values from the cache successfully.
Conclusion
By following this guide, you've set up a Dockerized Laravel application using a custom Dockerfile and the ServerSideUp image, with Redis integration via Docker Compose. This setup not only ensures a consistent environment across development and production but also simplifies managing dependencies and configurations.
Whether you're developing locally, running tests in CI, or deploying to production, Docker offers a powerful and flexible way to handle your Laravel application's infrastructure. With Redis added, your app is now more powerful, capable of handling session storage, caching, and queues more efficiently.
Happy coding! 🚀