Its important to note that the build steps you will find right here is a basic starting point for dockerizing your laravel app in development environment. and this could be useful for teams that have a development environment hosted in heroku. this is not a production setup
First let's outline the steps required for starting up a laravel project in development environment.
1- Installing PHP and Composer
PHP is the language of the framework and composer is the dependency manager for the project,
2- Using Composer to install project dependencies
The Laravel framework utilizes Composer for installation and dependency management.
3- Serving your Laravel project using the artisan cli tool
When we want to run our laravel project we run the php artisan serve
command to serve our project at port 8000 by default
Now lets Dockerize;
The first step in writing our docker build steps is determing the base image for our docker container and the various dependencies that we would to install in our docker container to have our laravel project running confortably.
Our base image will be a php:8.0.5
which is basically a linux image with php installed
In the next step we will run the command below to update our package/dependency manager and dependences required for our laravel project
apt-get update -y && apt-get install -y openssl zip unzip git
Next is to install composer by running this command
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
We might also like to install php extensions for mysql support and pdo for those using a mysql database in their project
docker-php-ext-install pdo_mysql
Then we install our laravel project dependency via composer
composer install --optimize-autoloader --no-dev
Now the tricky part is serving the application. Usually when serving our laravel application, port 8000 is allocated by default as the server port. but heroku platform doesnt respect ports defined by us, so we must make use of the PORT provided by heroku, luckly heroku defines an environment variable $PORT
so therefore we will write the command to server the application this way π
php artisan serve --host=0.0.0.0 --port $PORT
Now putting all of this together in a Dockerfile at our root directory, we will have thisπ
FROM php:8.0.5
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . /app
RUN composer install --optimize-autoloader --no-dev
CMD php artisan serve --host=0.0.0.0 --port $PORT
EXPOSE $PORT
And for laravel projects using mysql we will have thisπ;
FROM php:8.0.5
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo_mysql
WORKDIR /app
COPY . /app
RUN composer install --optimize-autoloader --no-dev
CMD php artisan serve --host=0.0.0.0 --port $PORT
EXPOSE $PORT
Top comments (0)