dream/ 二月 3, 2020/ docker, linux, 嵌入式/ 1评论

问题:docker hub上部分镜像中不包含sh,导致无法进入到容器的命令行。如caddy镜像等。

报错示例:

╭─root@dts-virtual-machine ~  
╰─# docker run -p 8080:8080 -it --name http_server caddy/caddy:scratch /bin/sh
[ERROR] '/bin/sh' is not a recognized subcommand; see 'caddy help'
╭─root@dts-virtual-machine ~  
╰─# docker run -p 8080:8080 -it --name http_server caddy/caddy:scratch        
2020/02/03 01:30:06.529	INFO	using provided configuration	{"config_file": "/etc/caddy/Caddyfile", "config_adapter": "caddyfile"}
2020/02/03 01:30:06.622	INFO	admin	admin endpoint started	{"address": "localhost:2019", "enforce_origin": false, "origins": ["localhost:2019"]}
2020/02/03 01:30:06.623	INFO	tls	cleaned up storage units
2020/02/03 01:30:06.623	INFO	autosaved config	{"file": "/var/lib/caddy/.config/caddy/autosave.json"}
2020/02/03 01:30:06.623	INFO	serving initial configuration
2020/02/03 01:30:06 [INFO][cache:0xc0002aa190] Started certificate maintenance routine
^C2020/02/03 01:30:15.787	INFO	shutting down	{"signal": "SIGINT"}
2020/02/03 01:30:15 [INFO][cache:0xc0002aa190] Stopped certificate maintenance routine
2020/02/03 01:30:15.787	INFO	shutdown done	{"signal": "SIGINT"}
╭─root@dts-virtual-machine ~  
╰─# docker start http_server                                           
http_server
╭─root@dts-virtual-machine ~  
╰─# docker exec -it --user root http_server /bin/sh    
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown

处理方案:

导致该问题的原因即容器中无sh,可以从外界直接拷贝一个来处理。

首先拉取一个busybox的镜像并从中拷贝出一个busybox,然后将其放入原容器中,并运行busybox的shell,进入命令行后再修改,将常用命令的链接指向busybox,问题即可解决。

╭─root@dts-virtual-machine ~  
╰─# docker run -it --name tmp_busybox busybox /bin/sh
/ # exit
╭─root@dts-virtual-machine ~  
╰─# docker cp tmp_busybox:/bin/busybox busybox                                                                               1 ↵
╭─root@dts-virtual-machine ~  
╰─# ls busybox     
busybox
╭─root@dts-virtual-machine ~  
╰─# docker cp busybox http_server:/           
╭─root@dts-virtual-machine ~  
╰─# docker start http_server       
http_server
╭─root@dts-virtual-machine ~  
╰─# docker cp busybox http_server:/
╭─root@dts-virtual-machine ~  
╰─# docker exec -it --user root http_server /busybox sh
/ # ls
sh: ls: not found
/ # ./busybox ls
busybox  dev      etc      proc     sys      usr      var
/ # ./busybox mkdir bin
/ # ./busybox mv busybox bin/
/ # cd bin/
/bin # ln -s busybox sh
sh: ln: not found
/bin # busybox ln -s busybox sh
/bin # busybox ln -s busybox ln
/bin # busybox ln -s busybox mkdir
/bin # busybox ln -s busybox netstat
/bin # busybox ln -s busybox ls

完成后就可以使用sh进入容器的命令行了,常用的命令如果没有的话,busybox里有的可以自行做链接。

点击数:371

Share this Post

1
说点什么

avatar
300
1 评论主题
0 主题回复
0 关注
 
最热门评论
最热门主题
1 评论者
AffiliateLabz 最近的评论者
  订阅  
最新 最旧 得票最多
提醒
AffiliateLabz
游客

Great content! Super high-quality! Keep it up! 🙂