Php Laravel App Dockerfile

A quick summary of Php Laravel App Dockerfile

Dockerfile for php Larael application

This is the base Dockerfile can be used to serve Laravel or similar php application. Based on Debian. Average size may vary.

Dockerfile


FROM php:7.2-fpm

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    default-mysql-client \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    jpegoptim optipng pngquant gifsicle \
    curl \
    nginx \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer

# Copy composer.lock and composer.json
COPY ./config/app.conf /etc/nginx/sites-available/default
COPY ./config/local.ini /usr/local/etc/php/conf.d/local.ini
COPY composer.lock composer.json /var/www/
COPY --chown=www-data:www-data . /var/www

RUN composer install

#Create folder for cache if file cache is used in .env file
RUN mkdir ./storage/framework/views && chown -R www-data:www-data /var/www/

# Database migrations and seeds, at this moment migrations doesn't work, must be added in entrypoint.sh
# RUN php artisan migrate && php artisan db:seed

# Expose port 9000 and start php-fpm server
EXPOSE 80
#CMD ["exec nginx -g 'daemon off;' && php-fpm"]
CMD ["sh", "-c", "nginx; php-fpm"]

Docker-compose file


version: '3'
services:

  mysql:
    image: mysql:5.7
    container_name: ${PROJECT_NAME}_mysql
    restart: unless-stopped
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - ms_data:/var/lib/mysql
    networks:
      project:
        aliases:
          - mysql

  php:
    build:
      context: .
    image: ${PROJECT_HUB}/php:${IMAGE_TAG}
    container_name: ${PROJECT_NAME}_php
    restart: unless-stopped
    tty: true
    env_file:
      - ./.env
    environment:
      - DB_CONNECTION=mysql
      - DB_HOST=${MYSQL_DB_HOST}
      - DB_DATABASE=${MYSQL_DATABASE}
      - DB_USERNAME=${MYSQL_USER}
      - DB_PASSWORD=${MYSQL_PASSWORD}
    working_dir: /var/www
    ports:
      - "8888:80"
    networks:
      project:
        aliases:
          - php

  redis:
    image: redis:alpine
    container_name: ${PROJECT_NAME}_redis
    restart: on-failure
    networks:
      project:
        aliases:
          - redis

volumes:
  ms_data:

networks:
  project:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.10.21.0/24

Environment sample file

IMAGE_TAG=latest

PROJECT_HUB=harbor/sample
PROJECT_NAME=sample

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:somebase64string
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

MYSQL_DB_HOST=mysql
MYSQL_ROOT_PASSWORD=secretp@sswd
MYSQL_DATABASE=dbname
MYSQL_USER=dbuser
MYSQL_PASSWORD=secret1

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mail.host
MAIL_PORT=587
MAIL_USERNAME=user@mail.host
MAIL_PASSWORD=PASSWORD
MAIL_ENCRYPTION=null