├── docker ├── postgres │ └── .gitkeep ├── image │ ├── app.png │ ├── adminer.png │ ├── pgadmin.png │ └── laravel+docker.png └── nginx │ └── default.conf ├── .gitignore ├── .env.example ├── .editorconfig ├── docker-compose.yml ├── Dockerfile ├── README.md └── Makefile /docker/postgres/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docker/image/app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/agungprsty/laravel-postgres-with-docker/HEAD/docker/image/app.png -------------------------------------------------------------------------------- /docker/image/adminer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/agungprsty/laravel-postgres-with-docker/HEAD/docker/image/adminer.png -------------------------------------------------------------------------------- /docker/image/pgadmin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/agungprsty/laravel-postgres-with-docker/HEAD/docker/image/pgadmin.png -------------------------------------------------------------------------------- /docker/image/laravel+docker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/agungprsty/laravel-postgres-with-docker/HEAD/docker/image/laravel+docker.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Editor 2 | .vscode 3 | 4 | # Container file 5 | /docker/postgres/data/* 6 | !/docker/postgres/data/.gitkeep 7 | 8 | # Working directory 9 | src/* 10 | .env 11 | -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | POSTGRES_DB="" 2 | POSTGRES_USER="" 3 | POSTGRES_PASSWORD="" 4 | PGADMIN_EMAIL="" 5 | PGADMIN_PASSWORD="" 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | indent_size = 4 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.{yml,yaml}] 15 | indent_size = 2 16 | 17 | [Makefile] 18 | indent_style = tab -------------------------------------------------------------------------------- /docker/nginx/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | index index.php index.html; 4 | error_log /var/log/nginx/error.log; 5 | access_log /var/log/nginx/access.log; 6 | root /var/www/html/public; 7 | 8 | add_header X-Frame-Options "SAMEORIGIN"; 9 | add_header X-XSS-Protection "1; mode=block"; 10 | add_header X-Content-Type-Options "nosniff"; 11 | 12 | charset utf-8; 13 | 14 | location = /favicon.ico { access_log off; log_not_found off; } 15 | location = /robots.txt { access_log off; log_not_found off; } 16 | 17 | location ~ \.php$ { 18 | try_files $uri =404; 19 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 20 | fastcgi_pass app:9000; 21 | fastcgi_index index.php; 22 | include fastcgi_params; 23 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 24 | fastcgi_param PATH_INFO $fastcgi_path_info; 25 | } 26 | 27 | location / { 28 | try_files $uri $uri/ /index.php?$query_string; 29 | gzip_static on; 30 | } 31 | 32 | location ~ /\.(?!well-known).* { 33 | deny all; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.9" 2 | 3 | networks: 4 | aselole: 5 | name: aselole 6 | 7 | services: 8 | app: 9 | container_name: aselole-app 10 | build: 11 | context: . 12 | dockerfile: Dockerfile 13 | volumes: 14 | - ./src:/var/www/html 15 | depends_on: 16 | - postgres 17 | networks: 18 | - aselole 19 | 20 | postgres: 21 | container_name: aselole-db 22 | image: postgres:15 23 | restart: always 24 | volumes: 25 | - ./docker/postgres/data:/var/lib/postgres/data 26 | environment: 27 | - POSTGRES_DB=${POSTGRES_DB} 28 | - POSTGRES_USER=${POSTGRES_USER} 29 | - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} 30 | ports: 31 | - "5432:5432" 32 | networks: 33 | - aselole 34 | 35 | web: 36 | container_name: aselole-web 37 | image: nginx:stable-alpine 38 | restart: always 39 | ports: 40 | - "85:80" 41 | volumes: 42 | - ./src:/var/www/html 43 | - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf 44 | networks: 45 | - aselole 46 | 47 | # Database management with pgAdmin 48 | pgadmin: 49 | image: dpage/pgadmin4 50 | container_name: aselole-pgAdmin 51 | environment: 52 | - PGADMIN_DEFAULT_EMAIL=${PGADMIN_EMAIL} 53 | - PGADMIN_DEFAULT_PASSWORD=${PGADMIN_PASSWORD} 54 | ports: 55 | - "5050:80" 56 | depends_on: 57 | - postgres 58 | networks: 59 | - aselole 60 | 61 | # Database management with Adminer 62 | adminer: 63 | container_name: aselole-adminer 64 | image: adminer 65 | restart: always 66 | ports: 67 | - "8080:8080" 68 | depends_on: 69 | - postgres 70 | networks: 71 | - aselole 72 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # PHP-FPM is a FastCGI implementation for PHP. 2 | # Read more here: https://hub.docker.com/_/php 3 | FROM php:8.2-fpm 4 | 5 | 6 | RUN apt-get update 7 | 8 | # Install useful tools 9 | RUN apt-get -y install apt-utils nano wget dialog vim 10 | 11 | # Install system dependencies 12 | RUN apt-get -y install --fix-missing \ 13 | apt-utils \ 14 | build-essential \ 15 | git \ 16 | curl \ 17 | libcurl4 \ 18 | libcurl4-openssl-dev \ 19 | zlib1g-dev \ 20 | libzip-dev \ 21 | zip \ 22 | libbz2-dev \ 23 | locales \ 24 | libmcrypt-dev \ 25 | libicu-dev \ 26 | libonig-dev \ 27 | libxml2-dev 28 | 29 | RUN docker-php-ext-install \ 30 | exif \ 31 | pcntl \ 32 | bcmath \ 33 | ctype \ 34 | curl \ 35 | pcntl \ 36 | zip 37 | 38 | # Install Postgre PDO 39 | RUN apt-get install -y libpq-dev \ 40 | && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ 41 | && docker-php-ext-install pdo pdo_pgsql pgsql 42 | 43 | # Install NPM 44 | RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - 45 | RUN apt-get install -y nodejs 46 | 47 | # Clear cache 48 | RUN apt-get clean && rm -rf /var/lib/apt/lists/* 49 | 50 | # Install Composer 51 | COPY --from=composer:2.3 /usr/bin/composer /usr/bin/composer 52 | 53 | # Set working directory 54 | WORKDIR /var/www/html 55 | 56 | # Add user for laravel application 57 | RUN groupadd -g 1000 www 58 | RUN useradd -u 1000 -ms /bin/bash -g www www 59 | 60 | # Copy existing application directory contents 61 | COPY ./src /var/www/html 62 | 63 | # Copy existing application directory permissions 64 | COPY --chown=www:www ./src /var/www/html 65 | 66 | # Change current user to www 67 | USER www 68 | 69 | # Set port for application 70 | EXPOSE 8000 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel using PostgreSQL in Docker 2 | 3 |

4 | docker+laravel 5 |

6 | 7 | ## Introduction 8 | 9 | Build a simple laravel application development environment with docker compose. 10 | 11 | 12 | ## Requirement 13 | 14 | - Docker ^19.* 15 | 16 | 17 | ## Installation 18 | 19 | 1. Git clone & move to working directory 20 | 2. Settings your credentials, copy `.env.example` to `.env` 21 | 3. Execute the following command for create application 22 | 23 | ```bash 24 | $ make create-project 25 | ``` 26 | 27 | 4. Next, set environment DB for app laravel in `src/.env` variable : 28 | ``` 29 | DB_CONNECTION=pgsql 30 | DB_HOST=postgres 31 | DB_PORT=5432 32 | DB_DATABASE= // same in root .env variable POSTGRES_DB 33 | DB_USERNAME= // same in root .env variable POSTGRES_USER 34 | DB_PASSWORD= // same in root .env variable POSTGRES_PASSWORD 35 | ``` 36 | 37 | 5. show application in [http://localhost:85](http://localhost:85) 38 | 39 | app+laravel 40 | 41 | 1. show adminer in [http://localhost:8080](http://localhost:8080) 42 | 43 | adminer 44 | 45 | 7. show pgadmin in [http://localhost:5050](http://localhost:5050) 46 | 47 | pgadmin 48 | 49 | 8. list execute command in [Makefile](Makefile). 50 | 51 | ## Container details : 52 | - ``app`` use image: 53 | - [php](https://hub.docker.com/_/php):8.2-fpm 54 | - [composer](https://hub.docker.com/_/composer):2.3 55 | - [npm](https://deb.nodesource.com/setup_lts.x):latest 56 | - ``web`` use image: 57 | - [nginx](https://hub.docker.com/_/nginx):stable-alpine 58 | - ``db`` use image: 59 | - [postgres](https://hub.docker.com/_/postgres):15 60 | - ``adminer`` use image: 61 | - [adminer](https://hub.docker.com/_/adminer):latest 62 | 63 | *Optional* 64 | - ``pgadmin`` use image: 65 | - [pgadmin](https://hub.docker.com/_/pgadmin):latest 66 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | up: 2 | docker compose up -d 3 | build: 4 | docker compose build 5 | laravel-install: 6 | docker compose exec app composer create-project --prefer-dist laravel/laravel . 7 | create-project: 8 | mkdir -p src 9 | @make build 10 | @make up 11 | @make laravel-install 12 | docker compose exec app php artisan key:generate 13 | docker compose exec app php artisan storage:link 14 | docker compose exec app chmod -R 777 storage bootstrap/cache 15 | docker compose exec app npm install 16 | init: 17 | docker compose up -d --build 18 | docker compose exec app composer install 19 | docker compose exec app cp .env.example .env 20 | docker compose exec app php artisan key:generate 21 | docker compose exec app php artisan storage:link 22 | docker compose exec app chmod -R 777 storage bootstrap/cache 23 | docker compose exec app npm install 24 | @make fresh 25 | remake: 26 | @make destroy 27 | @make init 28 | stop: 29 | docker compose stop 30 | down: 31 | docker compose down --remove-orphans 32 | down-v: 33 | docker compose down --remove-orphans --volumes 34 | restart: 35 | @make down 36 | @make up 37 | destroy: 38 | docker compose down --rmi all --volumes --remove-orphans 39 | ps: 40 | docker compose ps 41 | logs: 42 | docker compose logs 43 | logs-watch: 44 | docker compose logs --follow 45 | log-web: 46 | docker compose logs web 47 | log-web-watch: 48 | docker compose logs --follow web 49 | log-app: 50 | docker compose logs app 51 | log-app-watch: 52 | docker compose logs --follow app 53 | log-db: 54 | docker compose logs postgres 55 | log-db-watch: 56 | docker compose logs --follow postgres 57 | web: 58 | docker compose exec web bash 59 | app: 60 | docker compose exec app bash 61 | migrate: 62 | docker compose exec app php artisan migrate 63 | fresh: 64 | docker compose exec app php artisan migrate:fresh --seed 65 | seed: 66 | docker compose exec app php artisan db:seed 67 | dacapo: 68 | docker compose exec app php artisan dacapo 69 | rollback-test: 70 | docker compose exec app php artisan migrate:fresh 71 | docker compose exec app php artisan migrate:refresh 72 | tinker: 73 | docker compose exec app php artisan tinker 74 | test: 75 | docker compose exec app php artisan test 76 | optimize: 77 | docker compose exec app php artisan optimize 78 | optimize-clear: 79 | docker compose exec app php artisan optimize:clear 80 | cache: 81 | docker compose exec app composer dump-autoload -o 82 | @make optimize 83 | docker compose exec app php artisan event:cache 84 | docker compose exec app php artisan view:cache 85 | cache-clear: 86 | docker compose exec app composer clear-cache 87 | @make optimize-clear 88 | docker compose exec app php artisan event:clear 89 | dump-autoload: 90 | docker compose exec app composer dump-autoload 91 | ide-helper: 92 | docker compose exec app php artisan clear-compiled 93 | docker compose exec app php artisan ide-helper:generate 94 | docker compose exec app php artisan ide-helper:meta 95 | docker compose exec app php artisan ide-helper:models --nowrite 96 | --------------------------------------------------------------------------------