-
Notifications
You must be signed in to change notification settings - Fork 0
/
php
executable file
·80 lines (64 loc) · 1.84 KB
/
php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
#
# @package php/alpine/fpm
# @author Yannoff <https://github.com/yannoff>
# @license MIT
#
image=yannoff/php-fpm
called=$(basename $0)
version=${called//php/}
if [ -z "${version}" ]
then
version=${PHP_VERSION:-8.2}
fi
_is_command() {
docker run --rm --tty ${image}:${version} which "$1" 2>&1 >/dev/null
return $?
}
_exec() {
[ -n "${DEBUG}" ] && echo "$*"
"$@"
}
if [ "$#" -gt "0" ]
then
# When the script is just passed an option, prefix it with "php"
# This allow calling the script in the form: bin/php -a
if [ "${1#-}" != "$1" ]
then
set -- php "$@"
else
# On the other hand, existing commands must be invoked as-is
basecmd=$(basename ${1})
if _is_command "${basecmd}"
then
# Counter bash parameters expansion behavior: if the first arg
# is an existing command, it will resolve to its full-path on
# the host machine
shift 1
set -- "${basecmd}" "$@"
else
set -- php "$@"
fi
fi
else
# If invoked without args, assume we want a bash session
set -- bash
fi
args=()
# Remove container after run
args+=( --rm )
# Run container in interactive mode
args+=( --interactive )
# Allocate a pseudo TTY, only if no piped input
[ -p /dev/stdin ] || args+=( --tty )
# Run as standard, low-priviledged user
args+=( -u $(id -u):$(id -g) )
# Mount user/group accounts as read-only
args+=( -v /etc/group:/etc/group:ro )
args+=( -v /etc/passwd:/etc/passwd:ro )
args+=( -v /etc/shadow:/etc/shadow:ro )
# Mount user's composer home if it exists
[ -d $HOME/.composer ] && args+=( -v $HOME/.composer:/.composer )
# Mount user's ssh directory if it exists
[ -d $HOME/.ssh ] && args+=( -v $HOME/.ssh:$HOME/.ssh )
_exec docker run -v ${PWD}:/app -w /app "${args[@]}" ${image}:${version:-8.2} "$@"