一.背景
徒弟的前端工程,已经构建出了镜像,并在1Panel上面可以查看镜像文件了。可是,手动创建容器运行后,怎么也连接不上。我带他准备先从容器里面的nginx是否已经启动正常开始,从内到外排查问题。
二.排查过程
1.先进入到容器里面
平时,如果是虚拟机或者远程服务器,我们一般都是连接远程桌面进去看看。现在是docker容器,怎么进到它里面呢?网上搜索的命令大多都是:
docker exec -it mine-vue /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是不存在的。那我们怎么理解上面进入到容器的命令呢?我的理解是“mine-vue”是容器的名称,“/bin/bash”是连接通道或者软件。到这里失败了,于是我在1Panel里面找到了解决方法。
2.从1Panel先进入到容器里面
在1Panel的容器界面,每个容器都有“终端”这个选择,可以考虑直接点击打开,就应该能进入到容器里面了吧?
我这里有点懵,看到了似乎熟悉的东西“/bin/sh”,这个是啥?也是一种连接的程序?先点击“连接”看看吧。点击后,在这个界面下半部分,出现了连接的画面。我找到了"/bin"目录,发现下面确实有“ash”、“sh”,但是没有我们要的“bash”。
先还是解决我们的问题,最后再去延伸学习“bin”、“ash”、“sh”、“bash”。
3.命令查看端口占用情况
我又回到了1Panel主机下面的终端,分别用“ash”和“sh”都进入查看了端口使用情况。
[root@localhost ~]# docker exec -it mine-vue /bin/sh
/ # netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.11:45675 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
tcp 0 0 :::80 :::* LISTEN 1/nginx: master pro
udp 0 0 127.0.0.11:35984 0.0.0.0:* -
/ # exit
[root@localhost ~]# docker exec -it mine-vue /bin/ash
/ # netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.11:45675 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
tcp 0 0 :::80 :::* LISTEN 1/nginx: master pro
udp 0 0 127.0.0.11:35984 0.0.0.0:* -
/ # exit
[root@localhost ~]#
发现端口都是正常监听的。那我在容器里面试试访问端口有正常的响应吗?输入了curl http://localhost:80,结果是正常的:
/ # curl http://localhost:80
<!DOCTYPE html><html><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1"><meta name=renderer content=webkit><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=/favicon.ico><title>配矿管理系统</title><link href=/static/css/app.c5e82b92.css rel=preload as=style><link href=/static/css/chunk-libs.2db6af39.css rel=preload as=style><link href=/static/js/app.d25a9921.js rel=preload as=script><link href=/static/js/chunk-elementUI.32dd3983.js rel=preload as=script><link href=/static/js/chunk-libs.b973ba81.js rel=preload as=script><link href=/static/css/chunk-libs.2db6af39.css rel=stylesheet><link href=/static/css/app.c5e82b92.css rel=stylesheet></head><body><div id=app></div><script>(function(e){function n(n){for(var t,r,o=n[0],d=n[1],f=n[2],h=0,i=[];h<o.length;h++)r=o[h],Object.prototype.hasOwnProperty.call(u,r)&&u[r]&&i.push(u[r][0]),u[r]=0;for(t in d)Object.prototype.hasOwnProperty.call(d,t)&&(e[t]=d[t]);b&&b(n);while(i.length)i.shift()();return a.push.apply(a,f||[]),c()}function c(){for(var e,n=0;n<a.length;n++){for(var c=a[n],t=!0,r=1;r<c.length;r++){var o=c[r];0!==u[o]&&(t=!1)}t&&(a.splice(n--,1),e=d(d.s=c[0]))}return e}var t={},r={runtime:0},u={runtime:0},a=[];function o(e){return d.p+"static/js/"+({}[e]||e)+"."+{"chunk-023b2e94":"583802ca","chunk-276138ce":"fa947cce","chunk-2ca47e32":"b2413247","chunk-2d0a4dd9":"7dc13ef8","chunk-2d0ba8b6":"c9e04531","chunk-2d0e1bdb":"72bc12d7","chunk-2d230fe7":"289e51b0","chunk-2d57dc24":"94163750","chunk-3bd0470c":"f05a5938","chunk-415fa28c":"67c4a277","chunk-47a23203":"93331fd0","chunk-4c9ef285":"01bb7d2f","chunk-5ea3371f":"dc7a566f","chunk-614f19b4":"cd654dbb","chunk-75f0336e":"b8c3ffd8","chunk-77b1110b":"ccc7950e","chunk-9b92e588":"6d48181c","chunk-9eadba7c":"a6b3e287","chunk-a69d8edc":"1b8921b4","chunk-c5d7f5f4":"79818c6b","chunk-e43f328a":"d22dcb2a"}[e]+".js"}function d(n){if(t[n])return t[n].exports;var c=t[n]={i:n,l:!1,exports:{}};return e[n].call(c.exports,c,c.exports,d),c.l=!0,c.exports}d.e=function(e){var n=[],c={"chunk-023b2e94":1,"chunk-276138ce":1,"chunk-3bd0470c":1,"chunk-415fa28c":1,"chunk-47a23203":1,"chunk-614f19b4":1,"chunk-75f0336e":1,"chunk-77b1110b":1,"chunk-9b92e588":1,"chunk-9eadba7c":1,"chunk-a69d8edc":1,"chunk-c5d7f5f4":1,"chunk-e43f328a":1};r[e]?n.push(r[e]):0!==r[e]&&c[e]&&n.push(r[e]=new Promise((function(n,c){for(var t="static/css/"+({}[e]||e)+"."+{"chunk-023b2e94":"b1dd7a76","chunk-276138ce":"80d49051","chunk-2ca47e32":"31d6cfe0","chunk-2d0a4dd9":"31d6cfe0","chunk-2d0ba8b6":"31d6cfe0","chunk-2d0e1bdb":"31d6cfe0","chunk-2d230fe7":"31d6cfe0","chunk-2d57dc24":"31d6cfe0","chunk-3bd0470c":"3efb746a","chunk-415fa28c":"8a521266","chunk-47a23203":"c62f5167","chunk-4c9ef285":"31d6cfe0","chunk-5ea3371f":"31d6cfe0","chunk-614f19b4":"0fd62164","chunk-75f0336e":"25e57fce","chunk-77b1110b":"ccfd5595","chunk-9b92e588":"87ab7d3c","chunk-9eadba7c":"bd154485","chunk-a69d8edc":"3efb746a","chunk-c5d7f5f4":"13a7e89e","chunk-e43f328a":"1fc537f3"}[e]+".css",u=d.p+t,a=document.getElementsByTagName("link"),o=0;o<a.length;o++){var f=a[o],h=f.getAttribute("data-href")||f.getAttribute("href");if("stylesheet"===f.rel&&(h===t||h===u))return n()}var i=document.getElementsByTagName("style");for(o=0;o<i.length;o++){f=i[o],h=f.getAttribute("data-href");if(h===t||h===u)return n()}var b=document.createElement("link");b.rel="stylesheet",b.type="text/css",b.onload=n,b.onerror=function(n){var t=n&&n.target&&n.target.src||u,a=new Error("Loading CSS chunk "+e+" failed.\n("+t+")");a.code="CSS_CHUNK_LOAD_FAILED",a.request=t,delete r[e],b.parentNode.removeChild(b),c(a)},b.href=u;var l=document.getElementsByTagName("head")[0];l.appendChild(b)})).then((function(){r[e]=0})));var t=u[e];if(0!==t)if(t)n.push(t[2]);else{var a=new Promise((function(n,c){t=u[e]=[n,c]}));n.push(t[2]=a);var f,h=document.createElement("script");h.charset="utf-8",h.timeout=120,d.nc&&h.setAttribute("nonce",d.nc),h.src=o(e);var i=new Error;f=function(n){h.onerror=h.onload=null,clearTimeout(b);var c=u[e];if(0!==c){if(c){var t=n&&("load"===n.type?"missing":n.type),r=n&&n.target&&n.target.src;i.message="Loading chunk "+e+" failed.\n("+t+": "+r+")",i.name="ChunkLoadError",i.type=t,i.request=r,c[1](i)}u[e]=void 0}};var b=setTimeout((function(){f({type:"timeout",target:h})}),12e4);h.onerror=h.onload=f,document.head.appendChild(h)}return Promise.all(n)},d.m=e,d.c=t,d.d=function(e,n,c){d.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:c})},d.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},d.t=function(e,n){if(1&n&&(e=d(e)),8&n)return e;if(4&n&&"object"===typeof e&&e&&e.__esModule)return e;var c=Object.create(null);if(d.r(c),Object.defineProperty(c,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var t in e)d.d(c,t,function(n){return e[n]}.bind(null,t));return c},d.n=function(e){var n=e&&e.__esModule?function(){return e["default"]}:function(){return e};return d.d(n,"a",n),n},d.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},d.p="/",d.oe=function(e){throw console.error(e),e};var f=window["webpackJsonp"]=window["webpackJsonp"]||[],h=f.push.bind(f);f.push=n,f=f.slice();for(var i=0;i<f.length;i++)n(f[i]);var b=h;c()})([]);</script><script src=/static/js/chunk-elementUI.32dd3983.js></script><script src=/static/js/chunk-libs.b973ba81.js></script><script src=/static/js/app.d25a9921.js></script></body></html>/ #
4.整理问题分析思路
内部启动是正常的,服务也正常。外部访问不到,可能的问题是容器里面有防火墙,不让访问或者没有打开防火墙?docker容器跟主机的端口映射不对?测试访问时的地址有误?我能想到的就这些了。
5.发现是创建容器时端口映射错误到了8080 ![](https://i-blog.csdnimg.cn/direct/dbf1588f93ad480fabe187b2713cc4a5.png)
修改端口映射配置,容器端口从8080修改为80,后面访问就正常了。
我们在浏览器访问http://10.1.230.94:14000/,发现就正常打开页面了。
三.延伸学习
回到为啥“docker exec -it mine-vue /bin/bash”进入不了容器?从1Pannel进入容器后,发现容器里面“bin”目录下面,根本没有“bash”程序,只有“ash”和“sh”。
1.“bin”是什么?
问的AI哈,“/bin”一个非常关键的目录,它存放着系统最基本的命令的二进制可执行文件。这些命令是系统启动和进行系统管理时所需要的最基本的工具。我的理解就是windows的system目录。
2.“ash”、"sh"、"bash"、“dash”分别是什么?
也是问的AI,大概意思是都是不同的程序,他们都是命令行解释器(Shell)。
3.再次理解docker exec -it mine-vue /bin/sh
就是执行mine-vue容器里面的命令解释器,类似于运行了远程桌面,这里不过是命令形式的通道。当然你执行的话,只不过有几种命令解释器,需要我们选择一种。下次遇到没有“bash”时,可以试试“ash”和“sh”吧!