-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker-php
More file actions
executable file
·128 lines (109 loc) · 3.95 KB
/
Copy pathdocker-php
File metadata and controls
executable file
·128 lines (109 loc) · 3.95 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash
source docker-build-env
args=("$@")
build=""
cmd="php"
composer_dir="-v ${COMPOSER_DIR}:/home/app/.composer"
directory="$(pwd)"
extra_hosts="${EXTRA_HOSTS}"
hosts=""
tty="-t"
name="php-docker-project-cli"
network=""
no_cache=""
ssh_dir=""
tag="latest"
user="${USER_ID}:${GROUP_ID}"
work_dir="/app"
if [[ "$1" = "--help" ]] || [[ "$1" = "-h" ]]; then
echo "A command line interface for PHP, Composer and Git.
Usage: docker-php [options] [cmd] [args...]
Runs an interactive shell when no arguments exist.
Commands:
composer
git
phar
* php
phpdbg
phpize
phpunit
php-config
Options:
--add-host list Add custom host-to-IP mapping, e.g example.com:192.168.10.10.
--build Build image from Dockerfile.
--network NAME Name of network to connect the container to, e.g. traefik_webgateway.
--no-cache Do not use cache when building the image.
--no-tty Do not allocate a pseudo-TTY.
--project-directory PATH Alternative working directory, default is current directory.
--ssh-keys Mount user ssh directory."
exit
fi
for ((i=0; i < "$#"; ++i)); do
case "${args[i]}" in
--add-host ) hosts="${hosts} --add-host ${args[i+1]}"
unset "args[i]" "args[i+1]"
i=$(expr ${i} + 1)
;;
--build ) build="--build"
unset "args[i]"
;;
--project-directory ) directory="${args[i+1]}"
unset "args[i]" "args[i+1]"
i=$(expr ${i} + 1)
;;
--network ) network="--network ${args[i+1]}"
unset "args[i]" "args[i+1]"
;;
--no-cache ) no_cache="--no-cache"
unset "args[i]"
;;
--ssh-keys ) ssh_dir="-v ${HOME}/.ssh:/home/app/.ssh"
unset "args[i]"
;;
--no-tty ) tty=""
unset "args[i]"
;;
esac
done
args=("${args[@]}")
if [[ -z "${args[0]}" ]]; then
args=("-a")
else
case "${args[0]}" in
composer|git|phar|php|phpdbg|phpize|phpunit|php-config ) cmd="${args[0]}"
unset "args[0]"
;;
esac
fi
image="${name}:${tag}"
if [[ -n "$build" ]] || [[ "$(docker images -q ${image} 2> /dev/null)" == "" ]]; then
php_docker_dir="$( cd "$(dirname "$0")/.." ; pwd -P )"
dockerfile="${php_docker_dir}/lib/php/Dockerfile"
if [[ -f "${php_docker_dir}/.build-cli.env" ]]; then
build_env_file="${php_docker_dir}/.build-cli.env"
else
build_env_file="${php_docker_dir}/.build.env"
fi
build_arg=""
while read line ; do
if [[ -n "${line}" ]] && [[ ! "$line" =~ "#" ]]; then
build_arg="${build_arg} --build-arg ${line/export /}"
fi
done < "${build_env_file}"
echo "Building image
Args: ${build_env_file}
Dockerfile: ${dockerfile}"
docker build ${no_cache} ${build_arg} -t "${image}" -f "${dockerfile}" "${php_docker_dir}"
if [[ $? != 0 ]]; then
echo "An error occurred."
exit 1
fi
fi
volume="${directory}:${work_dir}"
if [[ -z "${hosts}" ]] && [[ -n "${extra_hosts}" ]]; then
add=($(echo ${extra_hosts} | tr ',' "\n"))
for ((i=0; i < "${#add[@]}"; ++i)); do
hosts="${hosts} --add-host ${add[i]}"
done
fi
docker run -i ${tty} --rm --name "${name}" ${hosts} -v "${volume}" ${network} ${ssh_dir} ${composer_dir} -w "${work_dir}" -u "${user}" "${image}" ${cmd} "${args[@]}"