1. 程式人生 > >centos6 安裝 docker17.06

centos6 安裝 docker17.06

由於centos6預設更新的docker版本為1.7.1, 不支援mount引數。所以需要升級docker到較新的版本。

從參考1,docker的官方文件中看到,要想使用mount需要docker17.06及以上版本。

參照參考2,進行docker18.06的安裝;

步驟如下:

1.  匯入GPG金鑰
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
如果遇到 網路問題,執行如下命令:
yum -y update nss

2. 安裝ELRepo
rpm -Uvh http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm

3. 安裝新核心
yum --enablerepo=elrepo-kernel install kernel-lt -y

4. 修改grub引導檔案
vi /etc/grub.conf
將default=1修改為default=0
確保預設啟動新的核心。一般新安裝的核心會在第一個位置(從0開始),所以設定default=0
修改完成後重啟電腦。
執行 uname-r
出現如下結果表示核心升級成功
4.4.163-1.el6.elrepo.x86_64


5. 安裝docker
解除安裝原有的docker
yum remove docker-io
rm -rf /var/lib/docker

curl -O http://mirrors.ustc.edu.cn/docker-ce/linux/static/stable/x86_64/docker-17.06.2-ce.tgz
tar -xzvf docker-17.06.2-ce.tgz && cp docker/* /usr/bin && rm -rf docker
mkdir -p /etc/docker 
echo "{\"registry-mirrors\": [\"https://docker.mirrors.ustc.edu.cn\"]}" > /etc/docker/daemon.json

配置docker 服務:

#!/bin/sh
#set -x
BASE_HOME=/var/run
PID_FILE=$BASE_HOME/docker.pid

# dockerd父程序的id,如果docker.pid檔案不存在則PID值就為-1
PID=-1
if [ -f $PID_FILE ];then
  PID=`cat $PID_FILE`
fi


status(){
    if [ $PID -gt -1 ]; then
       echo "dockerd is running"        
    else
       echo "dockerd is not running"        
    fi
}

start() {
    if [ $PID -gt -1 ]; then
       echo "dockerd has started,the pid is $PID"
       return
    fi
    nohup dockerd > /var/log/dockerd.log 2>&1 &
    COUNT=0
    NUMBER=0
    echo -e "starting...\c"
    while [ $COUNT -lt 2 ]; do
      echo -e ".\c"
      sleep 0.25
      if [ -f $PID_FILE ];then
         PID=`cat $PID_FILE`
      fi
      COUNT=`ps -ef |grep $PID|grep -v grep|wc -l`
      # dockerd父程序與docker-containerd子程序都啟動
      if [ $COUNT -ge 2 ]; then
         echo -e "\ndockerd has started,the pid is $PID"
         return
      fi
      if [ $NUMBER -gt 20 ]; then
         break
      fi
      NUMBER=$NUMBER+1
    done
    
    echo -e "\ndockerd start failed"        
}

stop() {
   if [ $PID -eq -1 ]; then
    echo "dockerd is not running"
    return
   fi
   
   # PIDS得到的是dockerd父程序及docker-containerd子程序號
   # sort -r是為了先殺子程序,再殺父程序
   PIDS=`ps -ef|grep $PID|grep -v grep|awk '{print $2}'|sort -r`

   for VAR_PID in $PIDS ; do
      kill $VAR_PID > /dev/null 2>&1
   done
   
   # 因為有dockerd父程序和docker-containerd子程序,因此預先設定COUNT=2
   COUNT=2
   echo -e "stoping...\c"
   while [ $COUNT -gt 0 ]; do
      echo -e ".\c"
      sleep 0.05
      COUNT=`ps -ef |grep $PID |grep -v grep |wc -l`
      if [ $COUNT -eq 0 ]; then
        echo -e "\ndockerd has been stoped"
        break
      fi
   done
   # restart的時候會存在無法啟動dockerd程序的問題。(因為PID值還是原先停掉的dockerd程序id)
   PID=-1
}

restart() {
   stop
   start
}

case "$1" in
    'start')
        start
        ;;
    'stop')
        stop
        ;;
    'status')
        status
        ;;
    'restart')
        restart
        ;;
    *)
    echo "usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

 啟動docker 服務,檢視docker 資訊

service docker start

解決啟動時的幾個問題:

1. level=warning msg="Could not get operating system name: Error opening /usr/lib/os-release: open /usr/lib/os-release: no such file or directory" 

echo "CentOS release 6.x (Final)" | tee /usr/lib/os-release

 2. warning msg="could not change group /var/run/docker.sock to docker: group docker not found"

groupadd docker

3.kernal相關的問題:

服務啟動的warning
warning msg="containerd: low RLIMIT_NOFILE changing to max" current=1024 max=4096
warning msg="Your kernel does not support kernel memory limit"
warning msg="mountpoint for pids not found"

docker info的warning
WARNING: No kernel memory limit support
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

  待定位。

參考: