Skip to content

Commit

Permalink
Merge branch 'release/0.12.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
krns committed Sep 7, 2020
2 parents 935c7ec + 6ef4fd4 commit a3316ca
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 72 deletions.
34 changes: 16 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
FROM php:7.3.8-fpm-alpine3.10
FROM php:7.4.10-fpm-alpine3.11

ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="0" \
PHP_OPCACHE_MAX_ACCELERATED_FILES="10000" \
PHP_OPCACHE_MEMORY_CONSUMPTION="192" \
PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10" \
PHP_MEMORY_LIMIT="512M"
PHP_MEMORY_LIMIT="512M" \
PHP_MAX_EXECUTION_TIME="60" \
PHP_FPM_MAX_CHILDREN="100" \
PHP_FPM_MAX_REQUESTS="1000" \
PHP_FPM_PM="ondemand" \
PHP_FPM_PROCESS_IDLE_TIMEOUT="10s"

RUN apk info \
&& echo @edge http://nl.alpinelinux.org/alpine/edge/community >> /etc/apk/repositories \
&& echo @edge http://nl.alpinelinux.org/alpine/edge/main >> /etc/apk/repositories \
&& echo @3.9 http://dl-cdn.alpinelinux.org/alpine/v3.9/main >> /etc/apk/repositories \
&& apk update \
&& apk upgrade \
&& apk add --no-cache --virtual .build-deps \
Expand All @@ -36,10 +40,7 @@ RUN apk info \
php7-dom \
mysql-client \
yarn@edge \
xvfb \
[email protected] \
[email protected] \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-png-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
gd \
pdo_mysql \
Expand All @@ -49,7 +50,6 @@ RUN apk info \
intl \
opcache \
pcntl \
mbstring \
iconv \
&& pecl install \
redis \
Expand All @@ -66,11 +66,6 @@ ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
# change default shell
SHELL ["/bin/bash", "-c"]

# create app user
RUN addgroup -S app && \
adduser -S app -G app && \
echo "app ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

# create working dir
RUN mkdir /opt/app
WORKDIR /opt/app
Expand All @@ -90,6 +85,9 @@ COPY ./etc/nginx/ /etc/nginx/
COPY ./usr/local/etc/php/ /usr/local/etc/php/
COPY ./usr/local/etc/php-fpm.d/ /usr/local/etc/php-fpm.d/

# copy bin files
COPY ./usr/local/bin/startup-commands.php /usr/local/bin/

# configure composer
ENV COMPOSER_ALLOW_SUPERUSER=1 \
COMPOSER_MEMORY_LIMIT=-1
Expand All @@ -100,11 +98,11 @@ RUN composer global require hirak/prestissimo
RUN yarn config set strict-ssl false && \
yarn global add cross-env

# copy home folder and make run scripts executable
COPY ./home/app/ /home/app/
COPY ./root/.bashrc /root/
RUN find /home/app -name "run-*.sh" -exec chmod -v +x {} \;
# copy root folder and make run scripts executable
COPY ./root/ /root/
RUN find /root -name "*.sh" -exec chmod -v +x {} \;

# run the application
CMD /home/app/run-dev.sh
ENTRYPOINT ["/root/entrypoint.sh"]
CMD ["runsvdir", "/etc/service"]
EXPOSE 8080
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ services:
environment:
- PHP_OPCACHE_VALIDATE_TIMESTAMPS=1
- PHP_MEMORY_LIMIT=1G
- PHP_MAX_EXECUTION_TIME=30
- STARTUP_COMMAND1=/root/modules/dev.sh
- STARTUP_COMMAND2=php artisan migrate --force
- STARTUP_COMMAND3=php artisan setup
volumes:
- ./:/opt/app:cached
ports:
Expand All @@ -68,6 +72,39 @@ Make sure to adjust your `.env` accordingly and set `APP_URL` to `http://localho

Run `docker-compose up` to start the services.

### Startup Commands

Further commands can be defined via ENV variable `STARTUP_COMMANDXXX`, which are executed at container start before the actual command.

The commands must be numbered sequentially and start with 1 (e.g. `STARTUP_COMMAND1=command`, `STARTUP_COMMAND2=...`).

```yml
version: '3.7'
services:
app:
image: sourceboat/docker-laravel
restart: unless-stopped
environment:
- PHP_OPCACHE_VALIDATE_TIMESTAMPS=1
- PHP_MEMORY_LIMIT=1G
- PHP_MAX_EXECUTION_TIME=30
- STARTUP_COMMAND1=/root/modules/storage.sh
- STARTUP_COMMAND2=/root/modules/cache.sh
- STARTUP_COMMAND3=php artisan migrate --force
- STARTUP_COMMAND4=php artisan horizon:restart
volumes:
- ./:/opt/app:cached
ports:
- "8080:8080"
depends_on:
- mysql
mysql:
image: mysql:8.0
environment:
- "MYSQL_ROOT_PASSWORD=secret"
- "MYSQL_DATABASE=default"
```

### Production

`WIP`
Expand All @@ -79,6 +116,7 @@ Check [releases](https://github.com/sourceboat/docker-laravel/releases) for all
## Credits

- [Phil-Bastian Berndt](https://github.com/pehbehbeh)
- [Philipp Kübler](https://github.com/PKuebler)
- [All Contributors](https://github.com/sourceboat/docker-laravel/graphs/contributors)

## License
Expand Down
1 change: 1 addition & 0 deletions etc/nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ server {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS $is_https;
fastcgi_hide_header X-Powered-By;
}
}
16 changes: 9 additions & 7 deletions etc/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
user nginx;
worker_processes 4;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
error_log /dev/stdout warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
Expand All @@ -11,18 +11,20 @@ events {
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/mime.types;
default_type application/octet-stream;

server_tokens off;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;
access_log /dev/null main;

sendfile on;
sendfile on;

keepalive_timeout 65;
keepalive_timeout 65;

include /etc/nginx/conf.d/*.conf;
}
17 changes: 0 additions & 17 deletions home/app/.bashrc

This file was deleted.

24 changes: 0 additions & 24 deletions home/app/run-dev.sh

This file was deleted.

18 changes: 17 additions & 1 deletion root/.bashrc
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
source /home/app/.bashrc
# useful aliases
alias ll='ls -la'
alias artisan="php artisan"

# prompt colors and short user display
force_color_prompt=yes
PS1='\[\033[1;36m\]\u\[\033[1;31m\]\[\033[1;32m\]:\[\033[1;35m\]\w\[\033[1;31m\]\$\[\033[0m\] '

# artisan autocompletion
_artisan()
{
COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
COMMANDS=`artisan --raw --no-ansi list | sed "s/[[:space:]].*//g"`
COMPREPLY=(`compgen -W "$COMMANDS" -- "${COMP_WORDS[COMP_CWORD]}"`)
return 0
}
complete -F _artisan artisan
21 changes: 21 additions & 0 deletions root/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
echo "Running entrypoint script..."

set -e

# make sure Laravel can write its own files
mkdir -p /opt/app/storage/logs/
touch /opt/app/storage/logs/laravel.log
touch /opt/app/storage/logs/worker.log
chown www-data:www-data -R /opt/app/storage
chown www-data:www-data -R /opt/app/bootstrap/cache

# startup commands
echo "Running startup commands..."
php /usr/local/bin/startup-commands.php | bash

if [[ -n "$DB_HOST" || -n "$DB_PORT" ]]; then
wait-for-it.sh --host=$DB_HOST --port=$DB_PORT
fi

exec "$@"
9 changes: 9 additions & 0 deletions root/modules/cache.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

cd /opt/app

# build caches
echo "Build caches..."
php artisan config:cache
php artisan route:cache
php artisan view:cache
6 changes: 6 additions & 0 deletions root/modules/dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
cd /opt/app

echo "Build development..."
composer install --prefer-dist --no-progress --no-interaction
yarn
5 changes: 5 additions & 0 deletions root/modules/storage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
cd /opt/app

echo "Create storage symlink..."
su www-data -s /bin/sh -c "php artisan storage:link -q"
46 changes: 46 additions & 0 deletions usr/local/bin/startup-commands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* A very simple script in charge of generating the startup commands based on environment variables.
* The script is run on each start of the container.
*
* MIT License
*
* Copyright (c) 2018 TheCodingMachine
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

$commands = array_filter($_SERVER, function(string $key) {
return strpos($key, 'STARTUP_COMMAND') === 0;
}, ARRAY_FILTER_USE_KEY);

ksort($commands);

echo "set -e\n";


// Let's run the commands as user $UID if env variable UID is set.

foreach ($commands as $command) {
$line = $command;
if (isset($_SERVER['UID'])) {
$line = 'sudo -E -u \\#'.$_SERVER['UID'].' bash -c '.escapeshellarg($line);
}
echo $line."\n";
}
11 changes: 7 additions & 4 deletions usr/local/etc/php-fpm.d/zz-fpm.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[www]
; Ondemand process manager
pm = ondemand
pm = ${PHP_FPM_PM}

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
Expand All @@ -11,21 +11,24 @@ pm = ondemand
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 100
pm.max_children = ${PHP_FPM_MAX_CHILDREN}

; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s
pm.process_idle_timeout = 10s;
pm.process_idle_timeout = ${PHP_FPM_PROCESS_IDLE_TIMEOUT};

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 1000
pm.max_requests = ${PHP_FPM_MAX_REQUESTS}

; Make sure the FPM workers can reach the environment variables for configuration
clear_env = no

; Catch output from PHP
catch_workers_output = yes

; No Access Log
access.log=/dev/null
9 changes: 8 additions & 1 deletion usr/local/etc/php/php.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 60
max_execution_time = ${PHP_MAX_EXECUTION_TIME}
max_input_time = 60
memory_limit = ${PHP_MEMORY_LIMIT}

Expand Down Expand Up @@ -56,3 +56,10 @@ opcache.memory_consumption=${PHP_OPCACHE_MEMORY_CONSUMPTION}
opcache.max_wasted_percentage=${PHP_OPCACHE_MAX_WASTED_PERCENTAGE}
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1

;;;;;;;;;;;;;;;;;
; Miscellaneous ;
;;;;;;;;;;;;;;;;;

; http://www.php.net/manual/en/ini.core.php#ini.expose-php
expose_php = Off

0 comments on commit a3316ca

Please sign in to comment.