以下脚本是我的 Dockerfile 的 ENTRYPOINT
:
/config/bootstrap.sh
#!/bin/bash
set -e
set -u
# Supervisord default params
SUPERVISOR_PARAMS='-c /etc/supervisord.conf'
mkdir -p /data/conf /data/run /data/logs
chmod 711 /data/conf /data/run /data/logs
if [ "$(ls /config/init/)" ]; then
for init in /config/init/*.sh; do
. $init
done
fi
# We have TTY, so probably an interactive container...
if test -t 0; then
# Run supervisord detached...
supervisord $SUPERVISOR_PARAMS
# Some command(s) has been passed to container? Execute them and exit.
# No commands provided? Run bash.
if [[ $@ ]]; then
eval $@
else
export PS1='[\u@\h : \w]\$ '
/bin/bash
fi
else
# If some extra params were passed, execute them before.
if [[ $@ ]]; then
eval $@
fi
supervisord -n $SUPERVISOR_PARAMS
fi
如您所见,我正在从 /config/init
中获取所有脚本并运行它们,对吧?这些脚本之一是这个:
#!/bin/sh
set -e
DATA_DIR="/data/www"
WEB_DIR="$DATA_DIR/web"
VAR_DIR="$DATA_DIR/var"
usermod -u 1000 apache && groupmod -g 1000 apache && \
chown -R apache:root $DATA_DIR
if [ "$(ls -la $WEB_DIR)" ]; then
find "$WEB_DIR" -type d -print0 -exec chmod 777 {} \;
fi
if [ "$(ls -la $VAR_DIR)" ]; then
find "$VAR_DIR" -type d -print0 -exec chmod 777 {} \;
fi
if [ "$(ls -a $DATA_DIR)" ]; then
touch "$DATA_DIR"/index.php
echo "<?php phpinfo(); ?>" > "$DATA_DIR"/index.php
fi
exec "$@"
每次我尝试在成功构建后运行容器时,我都会收到以下错误:
$ docker run -it reynierpm/php-fpm -e INSTALL_COMPOSER=true
ls: cannot access /data/www/web: No such file or directory
ls: cannot access /data/www/var: No such file or directory
/config/init/20-permissions.sh: line 26: exec: -e: invalid option
exec: usage: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
更新
上面的脚本已经更新如下:
#!/bin/bash
set -e
data_dir="/data/www"
web_dir="$data_dir/web"
var_cache_dir="$data_dir/var/cache"
var_logs_dir="$data_dir/var/logs"
# This script will be placed in /config/init/ and run when container starts.
usermod -u 1000 apache && groupmod -g 1000 apache && \
chown -R apache:root "$data_dir"
# Setup web directory permissions and ownership
if [ -d "$web_dir" ]; then
chgrp -R apache "$web_dir"
chmod -R g+w "$web_dir"
find "$web_dir" -type d -exec chmod 2775 {} +
find "$web_dir" -type f -exec chmod ug+rw {} +
fi
# Setup cache directory permissions and ownership
if [ -d "$var_cache_dir" ]; then
chgrp -R apache "$var_cache_dir"
chmod -R g+w "$var_cache_dir"
find "$var_cache_dir" -type d -exec chmod 2775 {} +
find "$var_cache_dir" -type f -exec chmod ug+rw {} +
fi
# Setup cache directory permissions and ownership
if [ -d "$var_logs_dir" ]; then
chgrp -R apache "$var_logs_dir"
chmod -R g+w "$var_logs_dir"
find "$var_logs_dir" -type d -exec chmod 2775 {} +
find "$var_logs_dir" -type f -exec chmod ug+rw {} +
fi
# Create index.php file if $data_dir is empty
if files=("$data_dir"/*); [[ -e "${files[0]}" ]]; then
echo '<?php phpinfo(); ?>' > "$data_dir/index.php";
fi
exec "$@"
但还是不行....
$ docker run -it reynierpm/php-fpm -e INSTALL_COMPOSER="true"
/config/init/21-permissions.sh: line 43: exec: -e: invalid option
exec: usage: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
我在这里缺少什么?怎么了?
最佳答案
在你的辅助脚本中,你在末尾有这一行:
exec "$@"
位置参数很可能(好吧,几乎可以肯定)包含 -e
作为论据。你可能会问,那是从哪里来的?
您的辅助脚本来源于主脚本中的这一行:
. $init
来源脚本在父级的上下文中执行,因此不会替换位置参数(除非您在来源文件的名称后指定参数)。如果$1
在您的父脚本中包含 -e
,您的源脚本将会看到。您的主脚本可能正在接收参数,或者您的辅助脚本之一更改了参数。在同一上下文中执行所有这些脚本有点问题,可能会泄漏变量和位置参数。
我发现了另一个问题:您的辅助脚本以 exec
结尾命令,它用参数中提供的命令替换当前进程(除了在 exec
用于操作文件描述符的特定情况下)。因此,我希望这个次要脚本永远不会返回到主要脚本,并且由于次要脚本的来源,您的主要脚本实际上永远不会完成。
关于linux - 启动脚本失败并出现错误 "-e: invalid option",缺少什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42095374/