问题描述
环境:
Ubuntu 18.04
Docker version 20.10.9, build c2ea9bc
最近在尝试复现 Dompteur,按照官方 readme 文档,在尝试 build 一个 GitHub - RUB-SysSec/dompteur 的 image 时,出现了报错。
username@device1:~/dompteur$ docker build -t dompteur .
Sending build context to Docker daemon 96.65MB
Step 1/15 : FROM nvidia/cuda:10.0-devel-ubuntu18.04
---> d4ff4c8943b1
Step 2/15 : RUN apt update && apt upgrade -y && apt install -y g++ zlib1g-dev make automake autoconf patch unzip wget git sox libtool subversion python2.7 python3 libatlas3-base && ln -s /usr/bin/python2.7 /usr/bin/python2 && ln -s /usr/bin/python2.7 /usr/bin/python
---> Using cache
---> 606e6c561001
Step 3/15 : RUN apt install -y python-pip python3-pip && pip install numpy scipy pathlib && pip3 install numpy scipy
---> Using cache
---> 848098bb9a61
Step 4/15 : RUN wget https://ssd.mathworks.com/supportfiles/downloads/R2019a/Release/6/deployment_files/installer/complete/glnxa64/MATLAB_Runtime_R2019a_Update_6_glnxa64.zip -O /root/matlab_runtime.zip && apt install -y libxrender1 libxt6 libxcomposite1 && unzip /root/matlab_runtime.zip -d /root/matlab_runtime && /root/matlab_runtime/install -mode silent -agreeToLicense yes && rm -rf /root/matlab_runtime*
---> Using cache
---> 0856691f3451
Step 5/15 : ADD hearing_thresholds/_compiled /root/hearing_thresholds
---> Using cache
---> 801b10515f31
Step 6/15 : ADD kaldi/tools /root/kaldi/tools
---> Using cache
---> 377893e0a9b1
Step 7/15 : WORKDIR /root/kaldi/tools
---> Using cache
---> a9a24cc171a1
Step 8/15 : RUN extras/check_dependencies.sh && make -j $(nproc) && ./extras/install_irstlm.sh
---> Running in 83a21c694851
/bin/sh: 1: extras/check_dependencies.sh: Permission denied
The command '/bin/sh -c extras/check_dependencies.sh && make -j $(nproc) && ./extras/install_irstlm.sh' returned a non-zero code: 126
PS:因为我前面的 BUILD 都顺利完成了,所以直接用了 Cache,报错显示是 Permission denied。多年的经验,第一反应直接 sudo docker build ***,结果依旧报错,然后研究了一下相关的原理。先直接上解决方案:
解决方案
1. 找到报错不能执行的 sh 文件
2. 查看 Dockerfile,找到执行该 sh 的 RUN 命令。
例如对于我的情况,使用的 Dockerfile 相关脚本如下:
# Build Kaldi tools
ADD kaldi/tools /root/kaldi/tools
WORKDIR /root/kaldi/tools
RUN extras/check_dependencies.sh &&\
make -j $(nproc) &&\
./extras/install_irstlm.sh
可以看到,在 /root/kaldi/tools/extras/check_dependencies.sh 处报错了。
3. 修改 Dockerfile 的 RUN 命令,在 sh 脚本前面加入 `chmod a+x extras/check_dependencies.sh &&\`,具体修改后的样子应该如下:
# Build Kaldi tools
ADD kaldi/tools /root/kaldi/tools
WORKDIR /root/kaldi/tools
RUN chmod a+x extras/check_dependencies.sh &&\
extras/check_dependencies.sh &&\
make -j $(nproc) &&\
./extras/install_irstlm.sh
4. 保存 Dockerfile 文件,重新 docker build xxx,即可。
原理与参考资料
Adding a file to docker image results in "permission denied" error - Stack Overflow
关于 Dockerfile 中各种命令的含义可以参见官方文档:
Dockerfile reference | Docker DocumentationDockerfiles use a simple DSL which allows you to automate the steps you would normally manually take to create an image.https://docs.docker.com/engine/reference/builder/#add基本思路就是 ADD 进去的文件没有开启可执行权限,加上可执行权限就好了。权限的报错来源与容器内部执行脚本。
`chmod a+x` 的意思就是给所有用户加上可执行权限,当然如果你的应用需要考虑安全问题,那就具体问题具体分析了。