Okay, so today I have a task that I need to run a Laravel App. I decided to set up a Docker for it. So I could share the development environment for my teammate. At this point, I was thinking about a Docker image which helps me handle it. So I researched around and got some Docker Images. After I tried them, I felt they are challenging to use at least for me. So I decided to build a Docker Image to set up the environment for Laravel App. Here we go.

1. First Step – Prepare your Laravel resource

You should follow this article to know how to install Laravel 5. I used Laravel Installer. To check if it installed already by the command `which laravel`. If you got the message like this `.composer/vendor/bin/laravel`, congrats Laravel CLI ready to use. Create your app folder which you will build Docker environment. In my case is `docker-laravel-5` `mkdir docker-laravel-5` > Note: I used OSX Then cd to `docker-laravel-5` folder and type this line in terminal `laravel new app`. This command will pull the initial source and put them all inside the app folder.

2. Second Step – Prepare to Build Docker environment

In `docker-laravel-5` folder, you create a `docker-compose.yml` file. That file contains our Docker configuration. To find out more refer Docker Compose Documentation. `> docker-compose.yml` I also create a `build` and `config`, `data` folders to use later. > The current version of Docker Compose is 3.7. But in my app, I used version 3.0. We will need PHP and Mysql to run a Laravel App. We don’t need a full web server here. Laravel builds a Local Development Server. We only need to boot it. At this time, our `docker-compose.yml` looks like this
version: '3'

services:

  // we will define out services here
We will define 2 services for PHP and Mysql soon.

2.1 PHP-FPM Service

Create a folder named `build`. That folder contains our own Dockerfile. Refer the Dockerfile Document. Then I created a file name `php.dockerfile` to config my PHP service.
FROM php:7.2-fpm

RUN apt-get update && apt-get install -y \
mysql-client libmagickwand-dev --no-install-recommends \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo pdo_mysql

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& chmod +x composer.phar \
&& mv composer.phar /usr/local/bin/composer

WORKDIR /var/www/html

ENTRYPOINT ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8181"]

EXPOSE 8181
  Add this to `docker-compose.yml` file.
  phpfpm:
    build:
      context: ./build
      dockerfile: php.dockerfile
    volumes:
      - ./app:/var/www/html
      - ./config/php.ini:/usr/local/etc/php/php.ini
    ports:
      - 8088:8181
    depends_on:
      - mysql
line 3: Define context for build parameter is `build` folder. line 4: Specific name of Dockerfile. line 6: We mounted `app` folder in the host machine to `/var/www/html` folder in the container. line 7: We mounted `php.ini` config file to `/usr/local/etc/php/php.ini` line 8-9: We set the port to listen on `8088` on the host machine to map with `8181` the port in the container. line 9-10: I set PHP service depends on MySQL service which I define later.

2.2 Mysql Service

We will need Mysql to store Laravel App data. So we define Mysql Service in `docker-compose.yml` likes this
  mysql:
    image: mysql:latest
    ports:
      - 3306:3306
    volumes:
      - ./data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_USER=root
      - MYSQL_DATABASE=spark
      - MYSQL_PASSWORD=root
line 2: We used office image on Docker Store for MySQL. line 3-4: We set the port for Mysql Service is on `3306`. You can change it. line 5-6: We mounted `data` folder in the host machine to `/var/lib/mysql` folder in the container. line 7-11: We define Mysql environment variables for Mysql Image. Ok, that’s it. Now, all we need to do is combine them together.
version: '3'

services:

  phpfpm:
    build:
      context: ./build
      dockerfile: php.dockerfile
    volumes:
      - ./app:/var/www/html
      # - ./config/php.ini:/usr/local/etc/php/php.ini
    ports:
      - 8088:8181
    depends_on:
      - mysql

  mysql:
    image: mysql:latest
    ports:
      - 3306:3306
    volumes:
      - ./data:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_USER=root
      - MYSQL_DATABASE=spark
      - MYSQL_PASSWORD=root
Save and run `docker-compose up –build` to build Laravel App container. Then access `http://localhost:8088` to see your result. Hope this help. Thanks for reading!

Leave a comment