Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 无法为docker容器执行脚本?_Linux_Bash_Shell_Docker - Fatal编程技术网

Linux 无法为docker容器执行脚本?

Linux 无法为docker容器执行脚本?,linux,bash,shell,docker,Linux,Bash,Shell,Docker,我不知道为什么,但我似乎无法从主机系统在docker容器内运行此脚本 在主机上,我在shell脚本中执行此代码 #!/bin/bash Docker_wordpress_status_check () { mysql_docker_status=$(docker container ls | grep -E 'docker_image_name|dockerwordpressmaster_wp-fpm_1') if [[ "$mysql_docker_status" ]]; then echo

我不知道为什么,但我似乎无法从主机系统在docker容器内运行此脚本

在主机上,我在shell脚本中执行此代码

#!/bin/bash

Docker_wordpress_status_check () {
mysql_docker_status=$(docker container ls | grep -E 'docker_image_name|dockerwordpressmaster_wp-fpm_1')
if [[ "$mysql_docker_status" ]]; then
echo "checking to see if wordpress exists"
wget https://s3/wordpress_staging_to_production_image_fixer.sh
chmod +x wordpress_staging_to_production_image_fixer.sh
docker cp wordpress_staging_to_production_image_fixer.sh dockerwordpressmaster_wp-fpm_1:/var/www/html/wordpress_staging_to_production_image_fixer.sh
docker exec -it dockerwordpressmaster_wp-fpm_1 sh -c "./wordpress_staging_to_production_image_fixer.sh"
fi
}

Docker_wordpress_status_check
这个脚本在大多数情况下运行良好,我甚至可以在当前的正确目录中看到该文件

/var/www/html/
我可以清楚地看到文件存在于容器中

docker exec -it dockerwordpressmaster_wp-fpm_1 sh
/var/www/html # ls -l | grep wordpress_staging_to_production_image_fixer.sh
-rwxr-xr-x    1 500      500            898 Dec 26 17:31 
wordpress_staging_to_production_image_fixer.sh
docker exec dockerwordpressmaster_wp-fpm_1 sh ./var/www/html/wordpress_staging_to_production_image_fixer.sh
但是,当我尝试从容器中执行脚本时

docker exec -it dockerwordpressmaster_wp-fpm_1 sh
/var/www/html # ls -l | grep wordpress_staging_to_production_image_fixer.sh
-rwxr-xr-x    1 500      500            898 Dec 26 17:31 
wordpress_staging_to_production_image_fixer.sh
docker exec dockerwordpressmaster_wp-fpm_1 sh ./var/www/html/wordpress_staging_to_production_image_fixer.sh
sh:无法打开“./var/www/html/wordpress\u staging\u to\u production\u image\u fixer.sh”

sh:/var/www/html/wordpress\u staging\u to\u production\u image\u fixer.sh:未找到

sh:wordpress\u staging\u to\u production\u image\u fixer.sh:未找到

bash:./var/www/html/wordpress\u staging\u to\u production\u image\u fixer.sh:没有这样的文件或目录

我不太确定我做错了什么,因为文件没有执行 我意识到所有者可能不正确,因此我可能无法运行脚本

docker exec dockerwordpressmaster_wp-fpm_1 sh chown root:root /var/www/html/wordpress_staging_to_production_image_fixer.sh
sh:无法打开“chown”


如果您能帮助解决此问题,我们将不胜感激。

以下陈述可能因两个原因对您无效

docker exec dockerwordpressmaster_wp-fpm_1 "sh" -c "/var/www/html/wordpress_staging_to_production_image_fixer.sh"
一个是访问问题,另一个是shebang
#/shell脚本开头缺少bin/sh
。您的错误是“sh:/var/www/html/wordpress\u staging\u to\u production\u image\u fixer.sh:未找到”。这肯定表明访问问题。因此,您需要转到
root:root
或使用id为500的用户登录

docker exec sh-c“pwd&chown root:root wordpress\u staging\u to\u production\u image\u fixer.sh‌​ && sh wordpress_staging_to_production_image_fixer.sh“


sh-c'chown…'?尝试
sh/var/www/html/wordpress\u staging\u to_production\u image\u fixer.sh
docker exec dockerwordpressmaster\u wp-fpm\u 1 sh chown'root:root/var/www/html/wordpress\u staging\u to_production\u image\u fixer.sh'似乎已经与sh/var/www/html/wordpress\u staging\u to_production\u image\u fixer.sh结合使用了我正在寻找的输出,谢谢大家也许可以更明确地指出
sh/path/to/file
sh-c'命令之间的区别;命令'
以及绝对路径和相对路径之间的差异。@tripleee有什么区别?OP似乎不知道
/var
/var
之间的区别,如果这是您的意思的话。当您在根目录中时,它们是相同的,但是您是吗?运行
sh-c
可以传递任意复杂的命令序列,而没有
-c
只能提供现有脚本的名称。但是相反,
sh-c existingscript
要求
existingscript
文件是可执行的,而
sh existingscript
只要求对脚本文件具有读取权限。(当然,如果你希望执行
/bin/chown
,那么
sh chown
只是一个异想天开的想法。在大多数地方,它根本不是
sh
脚本。OP显然也需要改进。)
docker exec dockerwordpressmaster_wp-fpm_1 "sh" -c "/var/www/html/wordpress_staging_to_production_image_fixer.sh"