Docker简易部署
记录一下docker 简易部署的步骤
环境准备
Centos 7.9 Mini安装;
cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
关闭防火墙和selinux
systemctl stop firewalld sed -i 's/enforcing/disabled/' /etc/selinux/config setenforce 0
安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
添加源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Yum安装
yum install -y docker-ce
开启及检查
systemctl start docker
systemctl enable docker
systemctl status docker
docker version
配置镜像加速(此步骤如担心安全问题,可不执行)
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
set_mirror.sh内容
1 #!/usr/bin/env bash 2 set -e 3 4 if [ -z "$1" ] 5 then 6 echo 'Error: Registry-mirror url required.' 7 exit 1 8 fi 9 10 MIRROR_URL=$1 11 lsb_dist='' 12 command_exists() { 13 command -v "$@" > /dev/null 2>&1 14 } 15 if command_exists lsb_release; then 16 lsb_dist="$(lsb_release -si)" 17 lsb_version="$(lsb_release -rs)" 18 fi 19 if [ -z "$lsb_dist" ] && [ -r /etc/lsb-release ]; then 20 lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")" 21 lsb_version="$(. /etc/lsb-release && echo "$DISTRIB_RELEASE")" 22 fi 23 if [ -z "$lsb_dist" ] && [ -r /etc/debian_version ]; then 24 lsb_dist='debian' 25 fi 26 if [ -z "$lsb_dist" ] && [ -r /etc/fedora-release ]; then 27 lsb_dist='fedora' 28 fi 29 if [ -z "$lsb_dist" ] && [ -r /etc/os-release ]; then 30 lsb_dist="$(. /etc/os-release && echo "$ID")" 31 fi 32 if [ -z "$lsb_dist" ] && [ -r /etc/centos-release ]; then 33 lsb_dist="$(cat /etc/*-release | head -n1 | cut -d " " -f1)" 34 fi 35 if [ -z "$lsb_dist" ] && [ -r /etc/redhat-release ]; then 36 lsb_dist="$(cat /etc/*-release | head -n1 | cut -d " " -f1)" 37 fi 38 lsb_dist="$(echo $lsb_dist | cut -d " " -f1)" 39 docker_version="$(docker -v | awk '{print $3}')" 40 docker_major_version="$(echo $docker_version| cut -d "." -f1)" 41 docker_minor_version="$(echo $docker_version| cut -d "." -f2)" 42 lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')" 43 44 set_daemon_json_file(){ 45 DOCKER_DAEMON_JSON_FILE="/etc/docker/daemon.json" 46 if sudo test -f ${DOCKER_DAEMON_JSON_FILE} 47 then 48 sudo cp ${DOCKER_DAEMON_JSON_FILE} "${DOCKER_DAEMON_JSON_FILE}.bak" 49 if sudo grep -q registry-mirrors "${DOCKER_DAEMON_JSON_FILE}.bak";then 50 sudo cat "${DOCKER_DAEMON_JSON_FILE}.bak" | sed -n "1h;1"'!'"H;\${g;s|\"registry-mirrors\":\s*\[[^]]*\]|\"registry-mirrors\": [\"${MIRROR_URL}\"]|g;p;}" | sudo tee ${DOCKER_DAEMON_JSON_FILE} 51 else 52 sudo cat "${DOCKER_DAEMON_JSON_FILE}.bak" | sed -n "s|{|{\"registry-mirrors\": [\"${MIRROR_URL}\"],|g;p;" | sudo tee ${DOCKER_DAEMON_JSON_FILE} 53 fi 54 else 55 sudo mkdir -p "/etc/docker" 56 sudo echo "{\"registry-mirrors\": [\"${MIRROR_URL}\"]}" | sudo tee ${DOCKER_DAEMON_JSON_FILE} 57 fi 58 } 59 60 61 can_set_json(){ 62 if [ "$docker_major_version" -eq 1 ] && [ "$docker_minor_version" -lt 12 ] 63 then 64 echo "docker version < 1.12" 65 return 0 66 else 67 echo "docker version >= 1.12" 68 return 1 69 fi 70 } 71 72 set_mirror(){ 73 if [ "$docker_major_version" -eq 1 ] && [ "$docker_minor_version" -lt 9 ] 74 then 75 echo "please upgrade your docker to v1.9 or later" 76 exit 1 77 fi 78 79 case "$lsb_dist" in 80 centos) 81 if grep "CentOS release 6" /etc/redhat-release > /dev/null 82 then 83 DOCKER_SERVICE_FILE="/etc/sysconfig/docker" 84 sudo cp ${DOCKER_SERVICE_FILE} "${DOCKER_SERVICE_FILE}.bak" 85 sudo sed -i "s|other_args=\"|other_args=\"--registry-mirror='${MIRROR_URL}'|g" ${DOCKER_SERVICE_FILE} 86 sudo sed -i "s|OPTIONS='|OPTIONS='--registry-mirror='${MIRROR_URL}'|g" ${DOCKER_SERVICE_FILE} 87 echo "Success." 88 echo "You need to restart docker to take effect: sudo service docker restart" 89 exit 0 90 fi 91 if grep "CentOS Linux release 7" /etc/redhat-release > /dev/null 92 then 93 if can_set_json; then 94 DOCKER_SERVICE_FILE="/lib/systemd/system/docker.service" 95 sudo cp ${DOCKER_SERVICE_FILE} "${DOCKER_SERVICE_FILE}.bak" 96 sudo sed -i "s|\(ExecStart=/usr/bin/docker[^ ]* daemon\)|\1 --registry-mirror="${MIRROR_URL}"|g" ${DOCKER_SERVICE_FILE} 97 sudo systemctl daemon-reload 98 else 99 set_daemon_json_file 100 fi 101 echo "Success." 102 echo "You need to restart docker to take effect: sudo systemctl restart docker " 103 exit 0 104 else 105 echo "Error: Set mirror failed, please set registry-mirror manually please." 106 exit 1 107 fi 108 ;; 109 fedora) 110 if grep "Fedora release" /etc/fedora-release > /dev/null 111 then 112 if can_set_json; then 113 DOCKER_SERVICE_FILE="/lib/systemd/system/docker.service" 114 sudo cp ${DOCKER_SERVICE_FILE} "${DOCKER_SERVICE_FILE}.bak" 115 sudo sed -i "s|\(ExecStart=/usr/bin/docker[^ ]* daemon\)|\1 --registry-mirror="${MIRROR_URL}"|g" ${DOCKER_SERVICE_FILE} 116 sudo systemctl daemon-reload 117 else 118 set_daemon_json_file 119 fi 120 echo "Success." 121 echo "You need to restart docker to take effect: sudo systemctl restart docker" 122 exit 0 123 else 124 echo "Error: Set mirror failed, please set registry-mirror manually please." 125 exit 1 126 fi 127 ;; 128 ubuntu) 129 v1=`echo ${lsb_version} | cut -d "." -f1` 130 if [ "$v1" -ge 16 ]; then 131 if can_set_json; then 132 DOCKER_SERVICE_FILE="/lib/systemd/system/docker.service" 133 sudo cp ${DOCKER_SERVICE_FILE} "${DOCKER_SERVICE_FILE}.bak" 134 sudo sed -i "s|\(ExecStart=/usr/bin/docker[^ ]* daemon -H fd://$\)|\1 --registry-mirror="${MIRROR_URL}"|g" ${DOCKER_SERVICE_FILE} 135 sudo systemctl daemon-reload 136 else 137 set_daemon_json_file 138 fi 139 echo "Success." 140 echo "You need to restart docker to take effect: sudo systemctl restart docker.service" 141 exit 0 142 else 143 if can_set_json; then 144 DOCKER_SERVICE_FILE="/etc/default/docker" 145 sudo cp ${DOCKER_SERVICE_FILE} "${DOCKER_SERVICE_FILE}.bak" 146 if grep "registry-mirror" ${DOCKER_SERVICE_FILE} > /dev/null 147 then 148 sudo sed -i -u -E "s#--registry-mirror='?((http|https)://)?[a-zA-Z0-9.]+'?#--registry-mirror='${MIRROR_URL}'#g" ${DOCKER_SERVICE_FILE} 149 else 150 echo 'DOCKER_OPTS="$DOCKER_OPTS --registry-mirror='${MIRROR_URL}'"' >> ${DOCKER_SERVICE_FILE} 151 echo ${MIRROR_URL} 152 fi 153 else 154 set_daemon_json_file 155 fi 156 fi 157 echo "Success." 158 echo "You need to restart docker to take effect: sudo service docker restart" 159 exit 0 160 ;; 161 debian) 162 if can_set_json; then 163 DOCKER_SERVICE_FILE="/etc/default/docker" 164 sudo cp ${DOCKER_SERVICE_FILE} "${DOCKER_SERVICE_FILE}.bak" 165 if grep "registry-mirror" ${DOCKER_SERVICE_FILE} > /dev/null 166 then 167 sudo sed -i -u -E "s#--registry-mirror='?((http|https)://)?[a-zA-Z0-9.]+'?#--registry-mirror='${MIRROR_URL}'#g" ${DOCKER_SERVICE_FILE} 168 else 169 echo 'DOCKER_OPTS="$DOCKER_OPTS --registry-mirror='${MIRROR_URL}'"' >> ${DOCKER_SERVICE_FILE} 170 echo ${MIRROR_URL} 171 fi 172 else 173 set_daemon_json_file 174 fi 175 echo "Success." 176 echo "You need to restart docker to take effect: sudo service docker restart" 177 exit 0 178 ;; 179 arch) 180 if grep "Arch Linux" /etc/os-release > /dev/null 181 then 182 if can_set_json; then 183 DOCKER_SERVICE_FILE="/lib/systemd/system/docker.service" 184 sudo cp ${DOCKER_SERVICE_FILE} "${DOCKER_SERVICE_FILE}.bak" 185 sudo sed -i "s|\(ExecStart=/usr/bin/docker[^ ]* daemon -H fd://\)|\1 --registry-mirror="${MIRROR_URL}"|g" ${DOCKER_SERVICE_FILE} 186 sudo systemctl daemon-reload 187 else 188 set_daemon_json_file 189 fi 190 echo "Success." 191 echo "You need to restart docker to take effect: sudo systemctl restart docker" 192 exit 0 193 else 194 echo "Error: Set mirror failed, please set registry-mirror manually please." 195 exit 1 196 fi 197 ;; 198 suse) 199 if grep "openSUSE Leap" /etc/os-release > /dev/null 200 then 201 if can_set_json; then 202 DOCKER_SERVICE_FILE="/usr/lib/systemd/system/docker.service" 203 sudo cp ${DOCKER_SERVICE_FILE} "${DOCKER_SERVICE_FILE}.bak" 204 sudo sed -i "s|\(^ExecStart=/usr/bin/docker daemon -H fd://\)|\1 --registry-mirror="${MIRROR_URL}"|g" ${DOCKER_SERVICE_FILE} 205 sudo systemctl daemon-reload 206 else 207 set_daemon_json_file 208 fi 209 echo "Success." 210 echo "You need to restart docker to take effect: sudo systemctl restart docker" 211 exit 0 212 else 213 echo "Error: Set mirror failed, please set registry-mirror manually please." 214 exit 1 215 fi 216 esac 217 echo "Error: Unsupported OS, please set registry-mirror manually." 218 exit 1 219 } 220 set_mirror
重启Docker,生效镜像加速
systemctl restart docker
Docker 安装完毕!