Linux 安装docker shell脚本

  • Post author:
  • Post category:linux


#!/bin/bash
sourceFile=$1 
targetPath=$2
port=$3
dockerBase=/docker
dockerConfTag=/conf
dockerServerTag=/docker-server
dockerDataTag=/data
dockerPidTag=/pid
dockerPidFileTag=/docker.pid
dockerServiceConf=/etc/init.d/docker
dockerDaemonJsonTag=/daemon.json
dockerRuncCurrentTag=/runc-current
dockerSockFile=/var/run/docker.sock
if [ ! -f $sourceFile ]; then
  echo "docker压缩包的地址未指定"
else
  if [ ! -n "$targetPath" ]; then
    echo the second param is not exists
    targetPath=$(cd `dirname $0`;pwd)
  fi
  if [ ! -d $targetPath ]; then
    mkdir $targetPath
    chmod -755 $targetPath
  fi
  
  processParam=`ps -aux|grep docker|grep -v $0|grep -v grep|awk '{print $2}'|xargs -r kill -9`
  dockerPath=$targetPath$dockerBase
  dockerServerPath=$dockerPath$dockerServerTag
  dockerDataPath=$dockerPath$dockerDataTag
  removeDockerPath=`rm -rf $dockerPath`
  removeDockerServerPath=`rm -rf $dockerServerPath`
  removeDockerServicePath=`rm -rf $dockerServiceConf`
  removeDockerSockPath=`rm -rf $dockerSockPath`
  if [ ! -n "$port" ]; then
    port=2375
  fi
  if [ ! -d $dockerPath ]; then
    mkdir $dockerPath
    chmod -R 755 $dockerPath
  fi
  if [ ! -f $dockerServiceConf ]; then
    touch $dockerServiceConf
    chmod -R 755 $dockerServiceConf
  fi
  if [ ! -f $dockerSockFile ]; then
    touch $dockerSockFile
    chmod -R 755 $dockerSockFile
  fi
  if [ ! -d $dockerDataPath ]; then
    mkdir $dockerDataPath
    chmod -R 755 $dockerDataPath
  fi
  dockerPidPath=$dockerPath$dockerPidTag
  if [ ! -d $dockerPidPath ]; then
    mkdir $dockerPidPath
    chmod -R 755 $dockerPidPath
  fi
  dockerPidFile=$dockerPidPath$dockerPidFileTag
  if [ ! -f $dockerPidFile ]; then
    touch $dockerPidFile
    chmod -R 755 $dockerPidFile
  fi
  
  dockerConfPath=$dockerPath$dockerConfTag
  if [ ! -d $dockerConfPath ]; then
    mkdir $dockerConfPath
    chmod -R 755 $dockerConfPath
  fi
  dockerConfFile=$dockerConfPath$dockerDaemonJsonTag
  if [ ! -f $dockerConfFile ]; then
    touch $dockerConfFile
    chmod -R 755 $dockerConfFile
  fi
  dockerRuncCurrentPath=$dockerPath$dockerRuncCurrentTag
  if [ ! -d $dockerRuncCurrentPath ]; then
    mkdir $dockerRuncCurrentPath
    chmod -R 777 $dockerRuncCurrentPath
  fi
  
  tar -xzvf $sourceFile -C $dockerPath
  cd $dockerPath
  binPath=`find ./ -name '*docker*' -type d -maxdepth 1` 
  realDockerTag=`echo ${binPath#*.}`
  realDockerPath=$dockerPath$realDockerTag
  movePath=`mv $realDockerPath $dockerServerPath`
  
  sedDockerHome=`sed -i '/DOCKER_HOME=\//d' /etc/profile` 
  echoDockerHome=`echo export DOCKER_HOME=$dockerServerPath>>/etc/profile`
  sedPath=`sed -i '/PATH=\//d' /etc/profile`
  echoPath=`echo export PATH=$dockerServerPath:$PATH>>/etc/profile`
  sourceResult=`source /etc/profile`
  cd /
  dockerConfFileContent="{\n
    \"log-level\":\"warn\",\n
    \"hosts\":[\"unix:///var/run/docker.sock\",\"tcp://0.0.0.0:$port\"],\n
    \"runtimes\":{\n
      \"docker-runc\":{\n
          \"path\": \"$dockerRuncCurrentPath\"\n
      }\n
    },\n
    \"add-runtime\":\"docker-runc=$dockerRuncCurrentPath\",\n
    \"default-runtime\":\"docker-runc\",\n
    \"selinux-enabled\":false,\n
    \"data-root\":\"$dockerDataPath\",\n
    \"pidfile\":\"$dockerPidFile\",\n
    \"log-opts\":{\"max-size\":\"1g\"},\n
    \"live-restore\":true\n
 }"
 echoDockerConf=`echo -e $dockerConfFileContent>>$dockerConfFile`
dockerServiceContent="
#!/bin/sh\n#chkconfig: - 64 36\n
#description:docker\n
dockerHomeStr=\$(grep 'DOCKER_HOME=' /etc/profile)\n
dockerHome=\$(echo \${dockerHomeStr#*=})\n
PATH=\$dockerHome:\$PATH\n
case \$1 in\n
start)\n
nohup $dockerServerPath/dockerd --config-file $dockerConfFile & ;;\n
stop)\n
cat $dockerPidFile|awk '{print \$1}'|xargs kill -9;;\n
restart)\n
service docker stop\n
sleep 1\n
service docker start;;\n
*)\n
echo this command is not support!\n
esac\n
"
  echoDockerServiceConf=`echo -e $dockerServiceContent>>$dockerServiceConf`
  service docker start
  sleepOne=`sleep 1`
  chkMongod=`chkconfig docker on`
  startFireWalld=`service firewalld start`
  addPort=`firewall-cmd --permanent --add-port=$port/tcp`
  fireWallReload=`firewall-cmd --reload`
  
fi
                                      



版权声明:本文为AngryFyj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。