Docker无法正常启动的原因及解决办法

一、Docker启动异常表现:

1.状态反复restaring,用命令查看

$docker ps -a
CONTAINER ID    IMAGE  COMMAND       CREATED      STATUS       PORTS    NAMES
21c09be88c11     docker.xxxx.cn:5000/xxx-tes/xxx_tes:1.0.6   "/usr/local/tomcat..."   9 days ago          Restarting (1) Less than a second ago                      xxx10

2.Docker日志有明显问题:

       $docker logs [容器名/容器ID]

二、Docker启动异常的可能原因:

2.1.内存不够
Docker 启动至少需要2G内存,首先执行free -mh命令查看剩余内存是否足够

直接查看内存

$free -mh
            total        used        free      shared  buff/cache   available
Mem:         15G         14G          627M        195M        636M     726M
Swap:         0B          0B          0B

分析日志

  有时候一瞬间内存过载溢出,导致部分进程被杀死,看起来内存也是够用的,事实上docker还是会反复重启,就需要通过docker日志和系统日志信的息来进一步分析:
分析docker日志

查看docker日志看到内存溢出的信息,要仔细翻阅才能找到信息,并不是在最下面

$docker logs [容器名/容器ID]|less 
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000769990000, 1449590784, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 1449590784 bytes for committing reserved memory.
# An error report file with more information is saved as:
# //hs_err_pid1.log
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000769990000, 1449590784, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 1449590784 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /tmp/hs_err_pid1.log
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000769990000, 1449590784, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 1449590784 bytes for committing reserved memory.
# Can not save log file, dump to screen..
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 1449590784 bytes for committing reserved memory.
# Possible reasons:
#   The system is out of physical RAM or swap space
#   In 32 bit mode, the process size limit was hit
# Possible solutions:
#   Reduce memory load on the system
#   Increase physical memory or swap space
#   Check if swap backing store is full
#   Use 64 bit Java on a 64 bit OS
#   Decrease Java heap size (-Xmx/-Xms)
#   Decrease number of Java threads
#   Decrease Java thread stack sizes (-Xss)
#   Set larger code cache with -XX:ReservedCodeCacheSize=
# This output file may be truncated or incomplete.
#
#  Out of Memory Error (os_linux.cpp:2756), pid=1, tid=140325689620224
#
# JRE version:  (7.0_79-b15) (build )
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode linux-amd64 compressed oops)
# Core dump written. Default location: //core or core.1
#
分析系统日志

查看系统日志,发现有大量由于内存溢出,进程被杀死的记录

$grep -i 'Out of Memory' /var/log/messages
Apr  7 10:04:02 centos106 kernel: Out of memory: Kill process 1192 (java) score 54 or sacrifice child
Apr  7 10:08:00 centos106 kernel: Out of memory: Kill process 2301 (java) score 54 or sacrifice child
Apr  7 10:09:59 centos106 kernel: Out of memory: Kill process 28145 (java) score 52 or sacrifice child
Apr  7 10:20:40 centos106 kernel: Out of memory: Kill process 2976 (java) score 54 or sacrifice child
Apr  7 10:21:08 centos106 kernel: Out of memory: Kill process 3577 (java) score 47 or sacrifice child
Apr  7 10:21:08 centos106 kernel: Out of memory: Kill process 3631 (java) score 47 or sacrifice child
Apr  7 10:21:08 centos106 kernel: Out of memory: Kill process 3634 (java) score 47 or sacrifice child
Apr  7 10:21:08 centos106 kernel: Out of memory: Kill process 3640 (java) score 47 or sacrifice child
Apr  7 10:21:08 centos106 kernel: Out of memory: Kill process 3654 (java) score 47 or sacrifice child
Apr  7 10:27:27 centos106 kernel: Out of memory: Kill process 6998 (java) score 51 or sacrifice child
Apr  7 10:27:28 centos106 kernel: Out of memory: Kill process 7027 (java) score 52 or sacrifice child
Apr  7 10:28:10 centos106 kernel: Out of memory: Kill process 7571 (java) score 42 or sacrifice child
Apr  7 10:28:10 centos106 kernel: Out of memory: Kill process 7586 (java) score 42 or sacrifice child

2.2.端口冲突

该docker监听端口已经被其他进程占用,一般此种问题容易出现在新部署的服务,或在原有机器上部署新的后台服务,所以在部署之前应该执行命令检查端口是否已经被占用,如果上线后发现占有则应改为可用端口再重启之。

检查命令:    $netstat -nltp|grep [规划的端口号]

三、对策

3.1.内存不够的对策:

对策1:
    3.1.1 saltstack的minion在运行过久之后,可能占用大量内存,需要将其重启。重启命令可能有时并不起作用。主要检查运行状态,如果未成功停止,则重新重启;
对策2:
    3.2.2 ELK日志收集程序或者其他java进程占用过高,用top和ps命令排查,谨慎确定进程的作用,在确保不影响业务的情况下,停止相关进程;
对策3:
   释放被占用的内存(buff/cache):
   $sync  #将内存数据写入磁盘
   $echo 3 > /proc/sys/vm/drop_caches  #释放被占用的内存
对策4:
    有时候并不是buff/cache过高导致内存不够用,确实是被很多必要的进程消耗掉了内存,那就需要从机器资源分配使用的层面去考虑和解决了。

3.2 端口冲突的对策

对策1:
 一般此种问题容易出现在新部署的服务,或在原有机器上部署新的后台服务,所以在部署之前应该执行命令检查端口是否已经被占用,如果上线后发现占有则应改为可用端口再重启之。
检查命令:    $netstat -nltp|grep [规划的端口号]
### Docker无法启动原因及解决方案 Docker无法启动可能是由多个因素引起的,以下是常见的原因以及对应的解决方案: #### 1. **Docker守护进程未运行** Docker守护进程(Docker daemon)是Docker的核心组件之一。如果该进程未能成功启动,则所有的Docker操作都无法执行[^1]。 - 解决方案:尝试手动启动Docker服务并检查日志。 ```bash sudo systemctl start docker sudo journalctl -u docker.service ``` #### 2. **镜像文件问题** 当使用的Docker镜像损坏或者根本不存在时,可能导致容器无法正常启动。这通常表现为拉取镜像失败或加载过程中出现问题[^2]。 - 解决方法:重新下载所需的镜像版本。 ```bash docker pull <image_name>:<tag> ``` #### 3. **配置文件错误** Docker的配置文件如`daemon.json`可能存在语法或其他类型的错误,从而阻止了Docker的成功初始化。 - 处理方式:验证并修正配置文件中的参数设定。 ```json { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" } } ``` #### 4. **系统资源不足** 主机上的可用CPU核心数、内存大小不足以支持所请求的容器规格也会引发启动失败的情况。 - 应对策略:优化现有资源配置或将部分负载迁移至其他节点释放空间。 ```bash free -h # 查看当前系统的空闲内存情况 top # 实时监控CPU占用率高的进程 ``` #### 5. **网络连接异常** 由于某些特定环境下的防火墙规则限制或是DNS解析失败等原因造成网络不可达同样会影响Docker的行为表现[^1]。 - 调整措施:调整相关网络选项来适配实际应用场景的需求。 ```bash --net=bridge|--network="host"|-p <宿主机端口>:<容器内部端口> ... ``` #### 6. **GPU驱动兼容性问题** 对于涉及图形处理单元(GPU)加速的应用场景来说,如果没有正确安装NVIDIA CUDA Toolkit及相关驱动程序的话也会影响到基于此硬件特性的容器实例创建过程[^4]。 - 推荐做法:按照官方文档指导完成必要的软硬件准备工作之后再试一次。 ```bash distribution=$(. /etc/os-release;echo $ID$VERSION_ID) curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | \ sudo apt-key add - curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \ sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list sudo apt-get update && sudo apt-get install -y nvidia-docker2 sudo systemctl restart docker ``` --- ### 总结 通过上述分析可以看出,针对不同层面产生的障碍采取相应的纠正行动能够有效提升解决问题效率。同时也要注意定期维护整个生态系统健康状态以减少潜在风险发生几率。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值