问题描述

docker演示中,我是用tomcat镜像来演示docker的一些操作但同样的操作有的TX是ok的,有的TX就会遇到如下错误

核心信息 Exited(1)

Cannot find /usr/local/tomcat/bin/setclasspath.sh

This file is needed to run this program

一开始也挺费解的,我虽然不上这个课,但也比较好奇,自己始终无法复现,但不断有学员问,我看到就回复,在docker run命令后加一个–privileged即可 但为何呢,不能说的很清楚,因为–privileged这个参数就是让你容器内的root用户具备拥有真正的root权限。否则容器内的root只是外部的一个普通用户权限。

线索一:容器退出码

从上面的提示可以看到容器退出了,ExitCode是11的意思是:

程序错误,或者Dockerfile中引用不存在的文件,如 entrypoint中引用了错误的包程序错误可以很简单,例如“除以0”,也可以很复杂,比如空引用或者其他程序 crash ExitCode1: Indicates failure due to application errorIndicates that the container stopped due to either an application error or an incorrect reference in Dockerfile to a file that is not present in the container.An application error can be as simple as “divide by 0” or as complex as “Reference to a bean name that conflicts with existing, non-compatible bean definition of same name and class.”An incorrect reference in Dockerfile to a file not present in the container can be as simple as a typo (the example below has sample.ja instead of sample.jar)

知道了这个似乎帮助不大,不过有的容器退出码是非常能指向原因的,比如ExitCode 0

线索二:无法找到文件

这个线索就非常重要了 那为何会无法找到,真的有吗?有的 比如在我这个正常的容器中 [root@hecs-67651 ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

59463bed0fd7 tomcat "catalina.sh run" 35 minutes ago Up 35 minutes 8080/tcp mytomcat5

[root@hecs-67651 ~]# docker exec -it 594 ls /usr/local/tomcat/bin/setclasspath.sh

/usr/local/tomcat/bin/setclasspath.sh

那遇到问题的学员为何找不到呢? 我们的这个tomcat镜像在启动的时候会执行一个脚本 [root@hecs-67651 ~]# docker inspect -f '{{.Config.Cmd}}' tomcat:latest

[catalina.sh run]

来看看catalina.sh做了啥

顺藤摸瓜:catalina.sh

这个shell脚本比较大646行,我就摘录关键部分 你看懂需要懂一些shell 第一部分:报错在哪里 if $os400; then

# -r will Only work on the os400 if the files are:

# 1. owned by the user

# 2. owned by the PRIMARY group of the user

# this will not work if the user belongs in secondary groups

. "$CATALINA_HOME"/bin/setclasspath.sh

else

if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then

. "$CATALINA_HOME"/bin/setclasspath.sh

else

echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh"

echo "This file is needed to run this program"

exit 1

fi

fi

可以看到我们的报错就在这里 执行的时候[ -r “$CATALINA_HOME”/bin/setclasspath.sh ]这个分支为假就走到了我们的报错中,exit 1 这句话的意思是看 “$CATALINA_HOME”/bin/setclasspath.sh文件是否有read权限 root@59463bed0fd7:/usr/local/tomcat/bin# ll setclasspath.sh

-rwxr-xr-x 1 root root 3342 Mar 6 23:33 setclasspath.sh*

在我这个OK的环境中的权限如上,read是有的 那可能的问题就是在CATALINA_HOME这个变量是否存在 而再往前看我们走到第一个else是因为$os400为假 第二部分:os400(仅供学习,对本问题没有作用,无需分析) cygwin=false

darwin=false

os400=false

hpux=false

case "`uname`" in

CYGWIN*) cygwin=true;;

Darwin*) darwin=true;;

OS400*) os400=true;;

HP-UX*) hpux=true;;

esac

从这里可以看到os400初始值为false,只有你的uname是OS400的时候才为true 而我们这个环境的uname的值是 [root@59463bed0fd7 ~]# uname

Linux

第三部分:[ -r “$CATALINA_HOME”/bin/setclasspath.sh ]

等价于 test -r “$CATALINA_HOME”/bin/setclasspath.sh 我这个OK的环境执行效果 root@59463bed0fd7:/usr/local/tomcat/bin# [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]

root@59463bed0fd7:/usr/local/tomcat/bin# echo $?

0

可以看到,是为0的返回值,那自然就不会报错,报错的环境肯定是非0 的 问题的焦点似乎就集中到了$CATALINA_HOME上 第四部分:$CATALINA_HOME怎么来的 # 下面的意思是如果没有CATALINA_HOME这个变量就设置为cd "$PRGDIR/.." >/dev/null; pwd 这个pwd的结果

[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`

# 而PRGDIR是这么来的

PRGDIR=`dirname "$PRG"`

# PRG来自

PRG="$0" # 就是catalina.sh所在目录

# 下面的我也有点看不懂了,大致就是获取目录

while [ -h "$PRG" ]; do

ls=`ls -ld "$PRG"`

link=`expr "$ls" : '.*-> \(.*\)$'`

if expr "$link" : '/.*' > /dev/null; then

PRG="$link"

else

PRG=`dirname "$PRG"`/"$link"

fi

done

找了半天找了个寂寞?好像是的。那问题到底在哪里呢?我也没法复现。捋一捋 线索:[ -r “$CATALINA_HOME”/bin/setclasspath.sh ] 执行为非0是肯定的 如果文件存在,变量存在,那问题就只能是-r了,权限问题!

解决方法

在docker run命令后加一个–privileged即可 –privileged这个参数就是让你容器内的root用户具备拥有真正的root权限。否则容器内的root只是外部的一个普通用户权限。 往上有个文档说是:与faccessat2系统调用有关,由于 runc 中的 bug,如果您的内核不支持 faccessat2,它将失败。这有点难了~看不懂

https://syntaxbug.com/6d684d2afe/

好文推荐

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。