ThakurCoder

Mastering docker for laravel: from local development to production deployment

·3 min read
Laravel
Docker
PHP
MySQL
performance

Mastering Docker for Laravel: From Local Development to Production Deployment

In the world of web development, Docker has emerged as a game-changer, offering a consistent and reproducible environment for both local development and production deployment. This guide will walk you through setting up a Laravel application using Docker, covering both local development and production deployment scenarios. We'll explore how to leverage Docker to create a seamless development workflow, from coding to deploying your Laravel application on a production server like DigitalOcean.

Local Development Environment Setup

For local development, you'll want to ensure that your Docker setup is optimized for development purposes. This includes having the latest versions of PHP and MySQL to take advantage of the latest features and improvements.

Dockerfile for Local Development

Your Dockerfile for the local development environment might look something like this, using PHP 8.3 and MySQL 8:

# Build Stage
FROM composer:2.0 as build
COPY. /app/
RUN composer install --prefer-dist --no-dev --optimize-autoloader --no-interaction
Development Stage
FROM php:8.3-fpm as development
ENV APP_ENV=development
ENV APP_DEBUG=true

RUN docker-php-ext-install pdo pdo_mysql COPY --from=build /app /var/www/html COPY docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

docker-compose.yml for Local Development

Your docker-compose.yml for local development might look like this:

version: '3'
services:
  app:
    build:
      context:.
      dockerfile: Dockerfile
    container_name: laravel_app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www/html
    volumes:
      -./:/var/www/html
      -./storage:/var/www/html/storage
    networks:
      - app-network
db:
image: mysql:8
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: your_root_password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
networks:
- app-network
networks:
app-network:
driver: bridge

volumes: dbdata: driver: local

Production Environment Setup

For the production environment, you'll want to optimize your Docker setup for performance and security. This includes using PHP 8.3 and MySQL 8, as well as configuring your application and Docker environment for production use.

Dockerfile for Production

Your Dockerfile for the production environment might look something like this:

# Production Stage
FROM php:8.3-fpm as production
ENV APP_ENV=production
ENV APP_DEBUG=false

RUN docker-php-ext-install pdo pdo_mysql COPY --from=build /app /var/www/html COPY docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

docker-compose.yml for Production

Your docker-compose.yml for production might look like this:

version: '3'
services:
  app:
    build:
      context:.
      dockerfile: Dockerfile
    container_name: laravel_app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: prod
    working_dir: /var/www/html
    volumes:
      -./:/var/www/html
      -./storage:/var/www/html/storage
    networks:
      - app-network
db:
image: mysql:8
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: your_root_password
SERVICE_TAGS: prod
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
networks:
- app-network
networks:
app-network:
driver: bridge

volumes: dbdata: driver: local

Note

By using PHP 8.3 and MySQL 8 in your Docker setup, you're leveraging the latest versions of these technologies, which can offer improved performance, new features, and better security. Remember to adjust your Dockerfiles and docker-compose.yml files according to your specific application requirements and environment configurations.

Conclusion

Docker provides a powerful and flexible way to manage both local development and production deployments of Laravel applications. By following the steps outlined in this guide, you can ensure a smooth development workflow and a seamless transition to production. Whether you're developing locally or deploying to a production server, Docker offers the consistency and control needed to maintain high-quality applications.