Last active
October 16, 2022 12:12
-
-
Save jalallinux/9e08387e525bb9405b18aad5fd42646f to your computer and use it in GitHub Desktop.
Laravel Best Dockerization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
app: | |
restart: always | |
container_name: 'app_name' | |
build: | |
context: . | |
dockerfile: Dockerfile | |
extra_hosts: | |
- 'host.docker.internal:host-gateway' | |
ports: | |
- '${APP_PORT:-9000}:9000' | |
volumes: | |
- app_data:/var/app/path/storage | |
networks: | |
tick: | |
ipv4_address: 10.0.2.1 | |
# depends_on: | |
# - pgsql | |
# - redis | |
# | |
# pgsql: | |
# image: bitnami/postgresql:14 | |
# ports: | |
# - '${FORWARD_DB_PORT:-5432}:5432' | |
# environment: | |
# POSTGRESQL_POSTGRES_PASSWORD: 'postgres' | |
# POSTGRESQL_DATABASE: '${DB_DATABASE:-dbname}' | |
# POSTGRESQL_USERNAME: '${DB_USERNAME:-postgres}' | |
# POSTGRESQL_PASSWORD: '${DB_PASSWORD:-postgres}' | |
# networks: | |
# tick: | |
# ipv4_address: 10.0.1.4 | |
# volumes: | |
# - 'postgres_data:/bitnami/postgresql' | |
# healthcheck: | |
# test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"] | |
# retries: 3 | |
# timeout: 5s | |
# | |
# redis: | |
# image: bitnami/redis:7.0 | |
# ports: | |
# - '${FORWARD_REDIS_PORT:-6379}:6379' | |
# environment: | |
# ALLOW_EMPTY_PASSWORD: 'no' | |
# REDIS_PASSWORD: '${REDIS_PASSWORD:-REDIS_PASSWORD}' | |
# REDIS_DISABLE_COMMANDS: 'FLUSHDB,FLUSHALL' | |
# networks: | |
# tick: | |
# ipv4_address: 10.0.1.5 | |
# volumes: | |
# - 'redis_data:/bitnami/redis/data' | |
# healthcheck: | |
# test: ["CMD", "redis-cli", "ping"] | |
# retries: 3 | |
# timeout: 5s | |
# | |
networks: | |
tick: | |
external: | |
name: docker_tick | |
volumes: | |
app_data: | |
driver: local | |
# postgres_data: | |
# driver: local | |
# redis_data: | |
# driver: local |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM jalallinux/laravel-9:php-80 | |
LABEL maintainer="JalalLinuX" | |
ENV TZ=Asia/Tehran | |
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone | |
COPY . /app/path | |
WORKDIR /app/path | |
COPY start-container /usr/local/bin/start-container | |
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf | |
RUN chmod +x /usr/local/bin/start-container | |
COPY .env.docker .env | |
RUN composer install | |
#RUN php -d variables_order=EGPCS artisan setup --migrate-fresh-seed --pm2-startup --pm2-link --symlinks | |
EXPOSE 9000 | |
ENTRYPOINT ["start-container"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class Setup extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'setup {--key-generate : Generating application key} | |
{--migrate-fresh-seed : Fresh migrations and run seeders} | |
{--migrate : Migrate new tables} | |
{--pm2-startup : Start, startup and save pm2 process} | |
{--pm2-link : Link pm2 to https://app.pm2.io/} | |
{--symlinks : Linking filesystem links}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Initialize Setup'; | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
if ( | |
!$this->option('key-generate') && !$this->option('migrate-fresh-seed') | |
&& !$this->option('pm2-startup') && !$this->option('pm2-link') | |
&& !$this->option('symlinks') && !$this->option('help') | |
) { | |
$this->error('No option passes. run `php artisan setup --help` to see options.'); | |
return 0; | |
} | |
if ($this->option('key-generate')) { | |
$this->call('key:generate', ['--force' => 1]); | |
} | |
$this->callSilent('optimize:clear'); | |
if ($this->option('migrate-fresh-seed')) { | |
$this->call('db:wipe', [ | |
'--force' => 1, | |
'--database' => 'pgsql', | |
]); | |
$this->call('db:wipe', [ | |
'--force' => 1, | |
'--database' => 'pgsql_log', | |
]); | |
$this->call('migrate', [ | |
'--force' => 1, | |
'--seed' => 1, | |
]); | |
} | |
if ($this->option('pm2-startup')) { | |
shell_exec("pm2 stop all"); | |
shell_exec("pm2 del all"); | |
shell_exec("pm2 start " . base_path('ecosystem.config.js')); | |
shell_exec("pm2 startup"); | |
shell_exec("'pm2 save --force'"); | |
} | |
if ($this->option('pm2-link')) { | |
$pm2Keys = config('app.pm2'); | |
if (empty($pm2Keys['public_key']) || empty($pm2Keys['secret_key'])) { | |
$this->error("Pm2 public_key or secret_key is not set."); | |
} else { | |
shell_exec("pm2 link delete"); | |
shell_exec("pm2 link {$pm2Keys['secret_key']} {$pm2Keys['public_key']}"); | |
} | |
} | |
if ($this->option('symlinks')) { | |
if (file_exists(public_path('storage'))) { | |
shell_exec("rm " . public_path('storage')); | |
} | |
$this->call('storage:link'); | |
} | |
$this->call('optimize:clear'); | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [ ! -z "$WWWUSER" ]; then | |
usermod -u $WWWUSER root | |
fi | |
if [ ! -d /.composer ]; then | |
mkdir /.composer | |
fi | |
chmod -R ugo+rw /.composer | |
if [ $# -gt 0 ]; then | |
exec gosu $WWWUSER "$@" | |
else | |
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[supervisord] | |
nodaemon=true | |
user=root | |
logfile=/var/log/supervisor/supervisord.log | |
pidfile=/var/run/supervisord.pid | |
[program:php] | |
command=/usr/bin/php -d variables_order=EGPCS /var/app/path/artisan serve --host=0.0.0.0 --port=9000 | |
#command=/usr/bin/php -d variables_order=EGPCS /var/app/path/artisan serve --host=0.0.0.0 --port=9000 | |
user=root | |
stdout_logfile=/dev/stdout | |
stdout_logfile_maxbytes=0 | |
stderr_logfile=/dev/stderr | |
stderr_logfile_maxbytes=0 | |
[program:pm2] | |
command=/usr/bin/pm2 start | |
user=root | |
autorestart=false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup.php
file in a console command must be copy toapp/Console/Commands