10.docker exec -it /bin/bash报错解决、sh与bash区别

报错

进入容器时,报如下错误

bash 复制代码
dockeruser@dell-PowerEdge-R740:~$ docker exec -it daf2 /bin/bash
OCI runtime exec failed: exec failed: unable to start container process: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown

解决

将bin/bash换成bin/sh

bash 复制代码
dockeruser@dell-PowerEdge-R740:~$ docker exec -it daf2 /bin/sh

分析

制作镜像时使用了精简版,只装了sh命令,未安装bash。

拓展

(1)shell简介

Shell是一种应用程序,该应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。Shell 是一个用 C 语言编写的程序,是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

sh (Bourne Shell)是一个早期的重要shell,1978年由史蒂夫·伯恩编写,并同Version 7 Unix一起发布。
bash(Bourne-Again Shell)是一个为GNU计划编写的Unix shell。1987年由布莱恩·福克斯创造。主要目标是与POSIX标准保持一致,同时兼顾对sh的兼容,是各种Linux发行版标准配置的Shell,在Linux系统上/bin/sh往往是指向/bin/bash的符号链接。

(2)#!

  • #! 是一个特殊标记,说明这是一个可执行的脚本。除了第一行,其他以#开头的都不再生效,为注释。
  • #! 后面是脚本的解释器程序路径。这个程序可以是shell,程序语言或者其他通用程序,常用的是bash、sh。

(3)sh与bash区别

  • sh 跟bash的区别是bash是否开启POSIX模式。

  • sh是bash的一种特殊的模式,sh就是开启了POSIX标准的bash, /bin/sh 相当于 /bin/bash --posix。

  • 在Linux系统上/bin/sh往往是指向/bin/bash的符号链接

    ln -s /bin/bash /bin/sh

  • sh 遵循POSIX规范:"当某行代码出错时,不继续往下解释"。bash 就算出错,也会继续向下执行。

hello.sh脚本:

bash 复制代码
#!/bin/sh
source err
echo "sh hello"

hello2.sh脚本:

bash 复制代码
#!/bin/bash
source err
echo "bash hello2"

输出结果:

bash 复制代码
[root@admin]# bash hello2.sh 
hello2.sh: line 2: err: No such file or directory
hello2
[root@admin]# sh hello.sh 
hello.sh: line 2: source: err: file not found
[root@admin]# bash --posix hello2.sh 
hello2.sh: line 2: source: err: file not found
相关推荐
fetasty12 小时前
rustfs加picgo图床搭建
docker
YuMiao12 小时前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
蝎子莱莱爱打怪1 天前
GitLab CI/CD + Docker Registry + K8s 部署完整实战指南
后端·docker·kubernetes
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号2 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
小p2 天前
docker学习7:docker 容器的通信方式
docker
小p2 天前
docker学习5:提升Dockerfile水平的5个技巧
docker