Docker

Environment Docker

docker-compose on Laravel

Here is the whole example of how to run the Laravel project on the pure docker environment via docker-compose.

And try to use the docker container to run the composer, npm, and artisan commands WITHOUT installing these environments on your local environment.

Keep your local environment simple and allow everyone is able to execute these commands in different platforms (Mac, Windows, Linux) by using docker.

  • Nginx
  • PostgreSQL
  • PHP
  • Redis
  • NPM
  • Artisan

File structure

- docker-compose.yaml
- resources
    - docker
        - dockerfile
            - composer.dockerfile
            - nginx.dockerfile
            - php.dockerfile
        - nginx
            - conf.d
                - nginx.conf
- storage
    - docker
        - postgresql (ignore the whole folder)
        

docker-compose.yaml

version: '3.9'

networks:
    local-proxy-network:
        driver: bridge

services:
    nginx:
        container_name: local_nginx
        build:
            context: resources/docker/dockerfile/
            dockerfile: nginx.dockerfile
        ports:
            - 80:80
        volumes:
            - '.:/var/www/html/laravel_app'
            - ./resources/docker/nginx/conf.d/:/etc/nginx/conf.d/
        networks:
            - local-proxy-network
        restart: always
        depends_on:
            - php
            - postgresql
            - redis
    php:
        container_name: local_php
        build:
            context: resources/docker/dockerfile/
            dockerfile: php.dockerfile
        expose:
            - 9000
        volumes:
             - '.:/var/www/html/laravel_app'
        extra_hosts:
            - "timegrade.local:127.0.0.1"
        networks:
            - local-proxy-network
        restart: always
    postgresql:
        container_name: local_postgresql
        ports:
            - "5432:5432"
        image: "postgres:14.5"
        volumes:
            - ./storage/docker/postgresql/data:/var/lib/postgresql/data # persist data even if container shuts down
        environment:
            POSTGRES_USER: ${DB_USERNAME}
            POSTGRES_PASSWORD: ${DB_PASSWORD}
            POSTGRES_DB: ${DB_DATABASE}
        networks:
            - local-proxy-network
        restart: always
    redis:
        container_name: local_redis
        image: redis:6.2.7 # stable version
        ports:
            - "6379:6379"
        networks:
            - local-proxy-network
        restart: always
    artisan:
        container_name: local_laravel_artisan
        build:
            context: resources/docker/dockerfile/
            dockerfile: php.dockerfile
        volumes:
            - '.:/var/www/html/laravel_app'
        working_dir: /var/www/html/laravel_app
        networks:
            - local-proxy-network
        depends_on:
            - postgresql
        entrypoint: [ "php", "/var/www/html/laravel_app/artisan" ]
    composer:
        container_name: local_composer
        build:
            context: resources/docker/dockerfile/
            dockerfile: composer.dockerfile
        volumes:
            - '.:/var/www/html/laravel_app'
        working_dir: /var/www/html/laravel_app
        networks:
            - local-proxy-network
    npm:
        container_name: local_npm
        image: node:18.12.0-alpine
        volumes:
            - .:/var/www/html/laravel_app
        entrypoint: [ "npm" ]
        working_dir: /var/www/html/laravel_app
        networks:
            - local-proxy-network

nginx.dockerfile

FROM nginx:1.23.2-alpine

ENV NGINX_USER=laravel
ENV NGINX_GROUP=laravel

RUN sed -i "s/user = www-data/user = ${NGINX_GROUP}/g" /etc/nginx/nginx.conf
RUN adduser -g ${NGINX_GROUP} -s /bin/sh -D ${NGINX_USER}

php.dockerfile

Including library

  • postgresql
  • zip
  • curl
  • gd
  • imagick
  • png
  • jpeg
  • opcache
FROM php:8-fpm

RUN apt-get update && apt-get install -y libpq-dev libzip-dev zip unzip curl \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libmagickwand-dev \
    --no-install-recommends \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && docker-php-ext-install pgsql pdo_pgsql zip\
    && docker-php-ext-enable opcache

# Install redis
RUN pecl install -o -f redis \
  &&  rm -rf /tmp/pear \
  &&  docker-php-ext-enable redis

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

# -R: Run as root
CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]

composer.dockerfile

FROM composer:2

ENV COMPOSERUSER=laravel
ENV COMPOSERGROUP=laravel

RUN adduser -g ${COMPOSERGROUP} -s /bin/sh -D ${COMPOSERUSER}

Docker Command

Command Description
docker-compose up nginx Run the nginx container on the foreground
docker-compose up -d nginx Run the nginx container on the background
docker-compose up --build -d nginx Build and run the nginx container on the background
docker-compose run --rm composer Run composer command via docker container
docker-compose run --rm artisan Run artisan command via docker container
docker-compose run --rm npm Run npm command via docker container
docker exec -it local_php bash Go to the php container

Composer

docker-compose run --rm composer require spatie/laravel-permission
docker-compose run --rm composer dump-autoload

Artisan

docker-compose run --rm artisan migrate

NPM

docker-compose run --rm npm install
docker-compose run --rm npm run dev

Reference