Docker + Laravel

Back-End Dev Note, Uncategorized June 1, 2023

Hi, I’m Raeanne. I’ve been working for 2 years now here at Commude PH as a PHP Developer. In this article, I will give you an idea on how to use a docker container to run your existing Laravel project. 

 

So what is Docker? Docker is a tool that allows software engineers  to easily deploy their applications in containers to run on the host operating system. Also, when developing a project and your localhost doesn’t meet all the requirements to run it, downgrading or upgrading your system’s current settings might be the first solution that comes to mind. That was me at the beginning, but when I discovered Docker –  I never looked back again. 

 

Now, enough with that. Let’s get into an example on how to Dockerize a Laravel project.

 

 

The setup

 

1)  Install Docker Desktop

 

First, we have to install Docker Desktop on your system. You may find it here: https://www.docker.com/get-started/

 

 

2)  Start the app

 

 

 

3)  Install PHP, MySQL, and Apache Web Server

 

At the root directory of your existing laravel project, create the file docker-compose.yml with the following details:

 

version: '3.8'


services:
  web:
    build: ./docker/apache
    container_name: todo.apache
    ports:
      - 8083:80
    privileged: true
    volumes:
      - ./:/var/www/html
  mysql:
    platform: linux/amd64
    image: mysql:5.7
    container_name: todo.db
    environment:
      MYSQL_ROOT_PASSWORD: mysql
      MYSQL_DATABASE: todo_db
      MYSQL_USER: default
      MYSQL_PASSWORD: secret
      TZ: 'Asia/Tokyo'
    command: mysqld --character-set-server=utf8mb4 --
    collation-server=utf8mb4_unicode_ci
    ports:
      - 3307:3306

 

For simplicity, we will be using apache since it’s easier to configure compared to nginx.

Now create a folder named ‘docker’ and within the directory create another directory named ‘apache’. Inside the apache folder create a Dockerfile with details as shown:

 

(File structure: docker/apache/Dockerfile)

 

# docker/apache/Dockerfile
FROM php:8.1-apache


COPY ./conf/*.conf /etc/apache2/sites-enabled/


RUN apt-get update \
  && apt-get install -y libzip-dev zlib1g-dev 
  libpq-dev mariadb-client libmagick++-dev 
  libmagickwand-dev libpq-dev libfreetype6-dev 
  libjpeg62-turbo-dev libpng-dev libwebp-dev unzip \

  && docker-php-ext-install zip pdo_mysql mysqli exif \
  && docker-php-ext-enable mysqli


COPY --from=composer:2.4.1 /usr/bin/composer /usr/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer
ENV PATH $PATH:/composer/vendor/bin


WORKDIR /var/www/html
RUN a2enmod rewrite

 

These commands will install PHP, the necessary dependencies of your existing laravel project, and run commands to serve it to the apache server. 

 

(Note: This is not the default configuration, you will have to tweak this file according to your project’s needs. )

 

We also need to configure the Virtual Host to pass the PHP requests to the apache server  via port 80. Create a folder named ‘conf’ inside our docker folder and create a default configuration file inside as shown below:

 

(File structure: docker/conf/000-default.conf)

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/public


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

 

4)  Build and start the container

 

Looking good! Now, we need to build the images of the container. In your terminal, go to your project and run docker-compose build. This might take a while since it will install everything we had configured:

 C:\Users\User\Desktop\commude-sample\todoApp>docker-compose build
 Mysql uses an image, skipping
 Mail uses an image, skipping
 Building web
 [+] Building 224.7s (12/12) FINISHED

 

 

After the build has finished, run docker-compose up -d to finally start the images.

 

 C:\Users\User\Desktop\commude-sample\todoApp>docker-compose up -d
 [+]Running 3/3
 - Container todo.db Started
 - Container todo.apache Started
 - Container todo.mail Running

 

And voila! That’s it. Your Laravel project is now dockerized!

 

5)  Install Laravel

 

Since you’re now working inside the container and not your usual localhost, you will need to install laravel inside the appropriate image and configure the details according to the details that was set on docker-compose.yml.

 

At your project’s root directory, run the following command to install laravel and its dependencies: 

 

Running commands will have to be like this:

(Syntax: docker-exec -it <image_name> <command>)


docker exec -it todo.apache php artisan migrate

 

A. If Laravel is not yet installed, run this command:

 

docker-compose -exec <image_name> composer create-project 
-prefer-dist laravel/laravel. “10.*”

 

B. If Laravel is already installed:

 

docker-compose -exec <image_name> composer install

 

5.1)  Configure settings

 

Now, once Laravel was installed, run the usual Laravel commands to set up your project:

 

 

A. Generate keys

 

docker-compose -exec <image_name> php artisan key:generate

 

B. Edit Laravel’s .env file

 

# .env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=todo_db
DB_USERNAME=default
DB_PASSWORD=secret

 

# docket-compose.yml
  Mysql:
    platform: linux/amd64
    image: mysql:5.7
    container_name: todo.db
    
  environment:
      MYSQL_ROOT_PASSWORD: mysql
      MYSQL_DATABASE: todo_db
      MYSQL_USER: default
      MYSQL_PASSWORD: secret

 

C. Clear configuration’s cache

 

When editing Laravel configuration files such as .env, we need to run the following commands to reflect the changes:

 

docker-compose -exec <image_name> php artisan config:cache && optimize:clear

 

 

 

D. Run migrations:

 

docker-compose -exec <image_name> php artisan migrate:fresh —seed

 

6)  Install NPM

 

Latest version of Laravel comes with vite, in order to access your project, you will need to install npm first. At your project’s root directory, run npm install && npm run dev. 

These commands shall install the dependencies set on package.json and build your projects assets

 

(https://laravel.com/docs/10.x/vite)

 

 

We can now access the localhost:8083 as defined in our docker-compose.yml

 

 

If you find it confusing, you’re not alone! I get confused about it as well. Gladly, they have a documentation that you can rely on to understand more about it: https://docs.docker.com/

 

Happy Coding!

 


 

We’re developing something similar. We have established an online exhibition system and are deploying it, utilizing this type of technology, and we do everything, from planning to infrastructure design, system development, and design. Please do not hesitate to contact us at info@commude.ph.

 

“Today we think ahead as one, in making tomorrow so much fun.”