Creating Docker File for Laravel App

Dockerize your laravel app#

Docker is greate tool for containerizing your app. if your app have multiple services involve then managing all those service manually is very tedious works to do.

better approach is create a docker compose file with all those services with their software version predefined then you just have to run that docker compose file and your project will start running instead managing php version, importing database.

i will give you simple docker compose file which contain following services.

  • Php localhost:8000
  • Mysql
  • Phpmyadmin localhost:5000
  • Mailhog localhost:8025
  • Meilisearch localhost:7700

you can include as much service as you want. all services are connected to specific network created by docker.

This compose file is development version if you want to deploy on server you can use php-nginx or php-apache image instead only php image.

first create a main folder like laravel-app then inside that create src folder

src folder will contain our application source file. we are not creating image for our application we are just adding docker to our existing project folder and mount that folder to php image container.

In this way we can modify our source code at docker runtime.

first create Dockerfile with the follwing content.


FROM php:8.0-fpm
WORKDIR /src

RUN apt-get update && apt-get install -y  \
    libfreetype6-dev \
    libjpeg-dev \
    libpng-dev \
    libwebp-dev \
    libzip-dev \
    --no-install-recommends \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo_mysql -j$(nproc) gd zip 
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

EXPOSE 8000

this docker file take care of php installation and its extension required for laravel project to run properly. it will also install composer for dependency management.

now lets create docker-compose.yaml file. this yaml file will contain all other services like database and all those.

docker-compose.yaml


version: '3'
services:

  diverse-db:
    container_name: diverse-db
    image: mysql
    # restart: always
    environment:
      MYSQL_ROOT_PASSWORD: Admin@12345
      MYSQL_DATABASE: diverseyp-docker
      MYSQL_USER: admin
      MYSQL_PASSWORD: Admin@123
    volumes:
      - ./db/setup.sql:/docker-entrypoint-initdb.d/setup.sql
      - ./mysql:/var/lib/mysql
    #ports:
    #  - "9906:3306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - '5000:80'
    # restart: always
    environment:
      PMA_HOST: diverse-db
      UPLOAD_LIMIT: 128048K
    depends_on:
      - diverse-db
  meilisearch:
    container_name: meilisearch
    image: getmeili/meilisearch:v0.28
    ports:
      - '7700:7700'
    volumes:
      - ./meili_data:/meili_data

  app:
    build:
      context: ./
    volumes:
      - ./src:/src

    command: bash -c "sh build.sh"

    ports:
      - "8000:8000"
    depends_on:
      - diverse-db
      - meilisearch
      - mailhog
  mailhog:
    container_name: mailhog
    image: mailhog/mailhog:latest
    ports:
      - "8025:8025"

create build.sh file in src directory which will contain post commands like composer install and php artisan serve file

build.sh

composer install
mv env.copy .env
php artisan serve

your folder will look like below .

docker folder structure

Running#

Now run this command to start your project

sudo docker compose up

add -d option for detached mode

Importing your old db.sql file#

if you want that your project start with the predefined sql file then add you database backup file in db/setup.sql .

if you have any doubt you can ask in comment. i would say go through the yaml file you will get the idea how things are connected and working.

That’s it for this post.

comments powered by Disqus