项目场景:
记录docker运行centos出现的问题及解决方法
1.问题描述:
进入centos终端执行exit后,docker容器停止运行
原因分析:
对于所创建的bash容器,当用户使用exit命令退出bash进程之后,容器也会自动退出。这是因为对于容器来说,当其中的应用退出后,容器的使命完成,也就没有继续运行的必要了
解决方案:
让docker容器在后台以守护态(Daemonized)形式运行。此时,可以通过添加-d参数来实现。
例如:
docker run -d -it centos /bin/sh
1
2.问题描述:
Docker下构建centos7容器无法使用systemctl命令
原因分析:
1号进程不是 init ,而是其他例如 /bin/bash ,所以导致缺少相关文件无法运行。(System has not been booted with systemd as init system (PID 1). Can’t operat)
解决方案:
解决方法如下:
docker run --privileged -it -d centos /usr/sbin/init #注意2点:1. --privileged 参数必须加,2. /usr/sbin/init作为容器的启动命令
1
验证方法如下:
docker exec -it 容器ID bash #进入容器的bash命令行
1
systemctl #验证systemctl 命令是否可用
1
3.问题描述:
docker centos使用sudo命令提示sudo command not found
查看镜像id
docker images
第一次通过镜像创建容器的时候需要加上
docker run -d -it --name=定义容器名称 镜像id /bin/sh
找到容器id
docker ps -a
docker exec -it 容器id/bin/bash
重启主机后进入
docker start 容器id
docker exec -it 容器id/bin/bash