laravel-docker-compose-with-pgsql-and-runner

Directory Structure

The project directory is structured as follows:

|-- ./
|   |-- .env
|   |-- docker-bash.sh*
|   |-- docker-compose.yml
|   |-- entrypoint.sh*
|   |-- etc/
|   |   +-- ...
|   |-- postgres_data/
|   |   +-- ...
|   |-- postgres_lib/
|   |   +-- ...
|   +-- www/
|       +-- ...

  1. To run laravel application inside the docker copy this 'docker-compose.yml' file to your project file
# docker-compose file to start up Laravel sales nest pharma app in https://sales.nestpharmaceuticals.in
# to start the container run:
#    docker-compose up -d
#
# Note: Bind mounts for laravel
#    /srv/sales.nestpharmaceuticals.in/www:/var/www -- Laravel app base folder
#    /srv/sales.nestpharmaceuticals.in/www/public:/var/www/html -- Apache DocumentRoot is mapped to Laravel public folder
# befroe that copy docker cp postgres.nestpharmaceuticals.in:/usr/lib/postgresql postgres_lib/
#    /srv/sales.nestpharmaceuticals.in/postgres_lib/postgresql/15/bin/pg_dump:/usr/bin/pg_dump -- to run postgres backup inside Laravel
#
# Note2: docker container name is sourced from the .env file in this directory.
#
# Note3: containrrr/watchtower service will auto update this container


version: "2.4"

services:
  postgres:
    image: postgres:latest
    container_name: postgres.nestpharmaceuticals.in
    restart: always
    ports:
      - 5432:5432
    volumes:
      - ./postgres_data:/var/lib/postgresql
      - ./postgres_data:/var/lib/postgresql/data

    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

  nest_app:
    image: rsubr/php-apache-ubuntu:jammy
    container_name: sales.nestpharmaceuticals.in
    restart: always

    volumes:
      - ./www:/var/www
      - ./www/public:/var/www/html
      - ./etc/apache2/mods-enabled/mpm_prefork.conf:/etc/apache2/mods-enabled/mpm_prefork.conf:ro
      - ./etc/php/8.1/apache2/conf.d/99-local.ini:/etc/php/8.1/apache2/conf.d/99-local.ini:ro
      - ./postgres_lib/postgresql/15/bin/pg_dump:/usr/bin/pg_dump

    mem_limit: 4G

    labels:
      - com.centurylinklabs.watchtower.enable=true
      - traefik.enable=true
      - traefik.http.routers.nest.rule=Host(`sales.nestpharmaceuticals.in`)
      - traefik.http.routers.nest.tls=true
      - traefik.http.routers.nest.tls.certresolver=lets-encrypt

    depends_on:
      - postgres

  nest_runner:
    image: rsubr/php-apache-ubuntu:jammy
    container_name: sales.nestpharmaceuticals.in_runner
    restart: always

    mem_limit: 1G

    entrypoint: [ "/entrypoint.sh" ]

    volumes:
      - ./www:/var/www
      - ./www/public:/var/www/html
      - ./etc/apache2/mods-enabled/mpm_prefork.conf:/etc/apache2/mods-enabled/mpm_prefork.conf:ro
      - ./etc/php/8.1/apache2/conf.d/99-local.ini:/etc/php/8.1/apache2/conf.d/99-local.ini:ro
      - ./entrypoint.sh:/entrypoint.sh
      - ./postgres_lib/postgresql/15/bin/pg_dump:/usr/bin/pg_dump
  1. Add entrypoin.sh file to run cron jobs for the laravel container
#!/bin/bash

# Run bip scheduled jobs periodically

# Sleep interval in seconds
INTERVAL=300


# Run first iteration after 10 seconds for postgres accept the connection, then sleep and loop
sleep 10
php /var/www/artisan backup:run

while sleep ${INTERVAL} ; do
    php /var/www/artisan backup:run
done