Skip to content

Commit bd0c404

Browse files
committed
Merge branch 'release/0.10.0'
2 parents ccfd50b + b5265c6 commit bd0c404

File tree

6 files changed

+106
-21
lines changed

6 files changed

+106
-21
lines changed

Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:7.4.5-fpm-alpine3.11
1+
FROM php:7.4.6-fpm-alpine3.11
22

33
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="0" \
44
PHP_OPCACHE_MAX_ACCELERATED_FILES="10000" \
@@ -90,6 +90,9 @@ COPY ./etc/nginx/ /etc/nginx/
9090
COPY ./usr/local/etc/php/ /usr/local/etc/php/
9191
COPY ./usr/local/etc/php-fpm.d/ /usr/local/etc/php-fpm.d/
9292

93+
# copy bin files
94+
COPY ./usr/local/bin/startup-commands.php /usr/local/bin/
95+
9396
# configure composer
9497
ENV COMPOSER_ALLOW_SUPERUSER=1 \
9598
COMPOSER_MEMORY_LIMIT=-1
@@ -104,7 +107,9 @@ RUN yarn config set strict-ssl false && \
104107
COPY ./home/app/ /home/app/
105108
COPY ./root/.bashrc /root/
106109
RUN find /home/app -name "run-*.sh" -exec chmod -v +x {} \;
110+
RUN chmod +x /home/app/entrypoint.sh
107111

108112
# run the application
113+
ENTRYPOINT bash /home/app/entrypoint.sh
109114
CMD /home/app/run-prod.sh
110115
EXPOSE 8080

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,37 @@ Make sure to adjust your `.env` accordingly and set `APP_URL` to `http://localho
6969

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

72+
### Startup Commands
73+
74+
Further commands can be defined via ENV variable `STARTUP_COMMANDXXX`, which are executed at container start before the actual command.
75+
76+
The commands must be numbered sequentially and start with 1 (e.g. `STARTUP_COMMAND1=command`, `STARTUP_COMMAND2=...`).
77+
78+
```yml
79+
version: '3.7'
80+
services:
81+
app:
82+
image: sourceboat/docker-laravel
83+
command: /home/app/run-prod.sh
84+
restart: unless-stopped
85+
environment:
86+
- PHP_OPCACHE_VALIDATE_TIMESTAMPS=1
87+
- PHP_MEMORY_LIMIT=1G
88+
- STARTUP_COMMAND1=php artisan migrate --force
89+
- STARTUP_COMMAND2=php artisan horizon:restart
90+
volumes:
91+
- ./:/opt/app:cached
92+
ports:
93+
- "8080:8080"
94+
depends_on:
95+
- mysql
96+
mysql:
97+
image: mysql:8.0
98+
environment:
99+
- "MYSQL_ROOT_PASSWORD=secret"
100+
- "MYSQL_DATABASE=default"
101+
```
102+
72103
### Production
73104

74105
`WIP`
@@ -80,6 +111,7 @@ Check [releases](https://github.com/sourceboat/docker-laravel/releases) for all
80111
## Credits
81112

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

85117
## License

home/app/entrypoint.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
echo "Running entrypoint script..."
3+
4+
set -e
5+
6+
# make sure Laravel can write its own files
7+
mkdir -p /opt/app/storage/logs/
8+
touch /opt/app/storage/logs/laravel.log
9+
touch /opt/app/storage/logs/worker.log
10+
chown www-data:www-data -R /opt/app/storage
11+
chown www-data:www-data -R /opt/app/bootstrap/cache
12+
13+
# create storage symlink
14+
echo "create storage symlink..."
15+
su www-data -s /bin/sh -c "php artisan storage:link -q"
16+
17+
# startup commands
18+
php /usr/local/bin/startup_commands.php | bash
19+
20+
cd /opt/app
21+
22+
"$@"

home/app/run-dev.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22
echo "Running via DEV script..."
33
cd /opt/app
44

5-
# make sure Laravel can write its own files
6-
mkdir -p /opt/app/storage/logs/
7-
touch /opt/app/storage/logs/laravel.log
8-
touch /opt/app/storage/logs/worker.log
9-
chown www-data:www-data -R /opt/app/storage
10-
chown www-data:www-data -R /opt/app/bootstrap/cache
11-
12-
# create storage symlink
13-
php artisan storage:link
14-
155
# install dependencies
166
composer install --prefer-dist --no-progress --no-interaction
177
yarn

home/app/run-prod.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
echo "Running via PROD script..."
33
cd /opt/app
44

5-
# make sure Laravel can write its own files
6-
mkdir -p /opt/app/storage/logs/
7-
touch /opt/app/storage/logs/laravel.log
8-
touch /opt/app/storage/logs/worker.log
9-
chown www-data:www-data -R /opt/app/storage
10-
chown www-data:www-data -R /opt/app/bootstrap/cache
11-
125
# migrate and setup database
136
if [ -z "$DB_HOST" ]; then
147
wait-for-it.sh $DB_HOST
@@ -21,8 +14,5 @@ php artisan config:cache
2114
php artisan route:cache
2215
php artisan view:cache
2316

24-
# restart the Horizon supervisors
25-
php artisan horizon:restart
26-
2717
# start the services
2818
exec runsvdir /etc/service

usr/local/bin/startup-commands.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* A very simple script in charge of generating the startup commands based on environment variables.
4+
* The script is run on each start of the container.
5+
*
6+
* MIT License
7+
*
8+
* Copyright (c) 2018 TheCodingMachine
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in all
18+
* copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
* SOFTWARE.
27+
*/
28+
29+
$commands = array_filter($_SERVER, function(string $key) {
30+
return strpos($key, 'STARTUP_COMMAND') === 0;
31+
}, ARRAY_FILTER_USE_KEY);
32+
33+
ksort($commands);
34+
35+
echo "set -e\n";
36+
37+
38+
// Let's run the commands as user $UID if env variable UID is set.
39+
40+
foreach ($commands as $command) {
41+
$line = $command;
42+
if (isset($_SERVER['UID'])) {
43+
$line = 'sudo -E -u \\#'.$_SERVER['UID'].' bash -c '.escapeshellarg($line);
44+
}
45+
echo $line."\n";
46+
}

0 commit comments

Comments
 (0)