赞
踩
环境:centos7.9 docker version 20.10.9
本文讲解如何联网环境下和离线环境下安装docker,在旧版本中, docker 被称为docker或docker-engine,但在新版本中,docker 引擎包现在称为docker-ce。
官方文档:https://docs.docker.com/engine/install/centos/
开启内核流量转发,可以根据自己情况来配置,不是必须项。
cat >>/etc/sysctl.d/docker.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0
net.ipv4.ip_forward = 1
EOF
modprobe br_netfilter && sysctl -p /etc/sysctl.d/docker.conf
#先删除旧的版本 #安装yum-utils,主要提供yum-config-manager命令 #下载并安装docker的仓库 #查看可获取的docker版本 #直接安装最新的docker版本 #安装指定版本 yum remove docker* \ docker-ce \ docker-ce-cli \ docker-ce-rootless-extras \ docker-scan-plugin \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine yum install -y yum-utils yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yum list docker-ce --showduplicates | sort -r # yum install docker-ce docker-ce-cli containerd.io -y yum install docker-ce-20.10.9 docker-ce-cli-20.10.9 containerd.io -y
#启动docker
#查看运行状态
#设置开机自启
systemctl start docker && systemctl enable docker
systemctl status docker | grep -i running
docker默认会从外国网站获取镜像,这样在网络不好的情况下镜像拉去得特别慢,所以设置docker从内网的镜像源拉取镜像。
mkdir /etc/docker/ && touch /etc/docker/daemon.json cat > /etc/docker/daemon.json <<'EOF' { "registry-mirrors": [ "https://b9pmyelo.mirror.aliyuncs.com", "https://docker.mirrors.ustc.edu.cn" ], "exec-opts": ["native.cgroupdriver=systemd"], "insecure-registries": ["192.168.118.143:1443"], "log-opts": { "max-size": "300m", "max-file":"5" }, "data-root": "/var/lib/docker", "max-concurrent-downloads": 3, "max-concurrent-uploads": 5, "live-restore": true } EOF 参数说明: "registry-mirrors" # 镜像下载地址,这个就不用多说了,可以配置多个镜像下载地址 "exec-opts": ["native.cgroupdriver=systemd"] #将cgroupdriver设置为systemd "insecure-registries": #这个定义的是私有镜像仓库harbor的仓库地址 "max-concurrent-downloads": 3 #docker拉取镜像并发下载的线程数 "max-concurrent-uploads": 5 #docker并发上传镜像的线程数 "data-root": "/var/lib/docker" #docker的主目录,默认是/var/lib/docker,在生产环境中建议设置ssd硬盘单独挂一个lvm逻辑卷 "live-restore": true #Docker 容器的自动重启是由 Docker 守护进程完成的。在较老版本 Docker 中,如果 docker 守护进程重启,容器会全 部挂掉。新版本 Docker 中,允许设置,当 docker 守护进程重启,容器不受影响。该场景比较多见,例如修改了 docker 的配置而需要重新加载 docker 守护进程,如果 docker 容器重启,业务会短暂中断,尤其是在生产环境这是不可接受的。所以这个设置很有必要。 具体设置方法有两种:第一种,编辑 /etc/docker/daemon.json,添加 "live-restore": true ,第二种,命令启用,dockerd --live-restore systemd。 "log-opts": { "max-size": "300m", "max-file":"5" } #设置docker最大的日志限制,在/var/lib/docker/containers/2a201024b135073bd9d0037227501e09ce0cddeedd523f15f2651ab5ed436670有个log文件,这个文件就是容器 里面的默认控制台输出文件,容器没有很长时间没有重启过,也不进行切割该文件,则该文件会越来越来大, "max-size"表示这个文件最大多少,到达 指定大小会自动切割,"max-file"表示最多保留多少个文件。 #重启docker #检查加速器配置是否成功 systemctl restart docker docker info |tail -5
#查看docker命令路径
#查看docker版本
which docker
docker -v
docker info | tail
WARNING: bridge-nf-call-iptables is disabled #有告警信息
WARNING: bridge-nf-call-ip6tables is disabled
cat >> /etc/sysctl.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl -p
下载离线安装包:https://download.docker.com/linux/static/stable/x86_64/docker-20.10.17.tgz
,这里就下载了最新稳定版,如果你的操作系统不同,那么可以进入到 https://download.docker.com/linux/static/stable
选择合适自己的版本。
开启内核流量转发,可以根据自己情况来配置,不是必须项。
cat >>/etc/sysctl.d/docker.conf <<'EOF'
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0
net.ipv4.ip_forward = 1
EOF
modprobe br_netfilter && sysctl -p /etc/sysctl.d/docker.conf
上传我们下载好的 docker-20.10.17.tgz 到服务器上,开始安装docker,如下所示:
tar -xf docker-20.10.17.tgz
cd docker && cp * /usr/bin/
#创建一个docker.service文件,并放到/usr/lib/systemd/system/目录下 #注意,这个docker。server文件是从官方复制过来的,但是删除了一些东西,如下 #Wants=network-online.target #注意:这里删除了Requires=docker.socket containerd.service 这一条,不然会启动失败 #ExecStart=/usr/bin/dockerd #注意:这里删除了-H fd:// --containerd=/run/containerd/containerd.sock这行不然会启动失败 cat > /usr/lib/systemd/system/docker.service <<'EOF' [Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com After=network-online.target firewalld.service containerd.service Wants=network-online.target [Service] Type=notify # the default is not to use systemd for cgroups because the delegate issues still # exists and systemd currently does not support the cgroup feature set required # for containers run by docker ExecStart=/usr/bin/dockerd ExecReload=/bin/kill -s HUP $MAINPID TimeoutSec=0 RestartSec=2 Restart=always # Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229. # Both the old, and new location are accepted by systemd 229 and up, so using the old location # to make them work for either version of systemd. StartLimitBurst=3 # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230. # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make # this option work for either version of systemd. StartLimitInterval=60s # Having non-zero Limit*s causes performance problems due to accounting overhead # in the kernel. We recommend using cgroups to do container-local accounting. LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity # Comment TasksMax if your systemd version does not support it. # Only systemd 226 and above support this option. TasksMax=infinity # set delegate yes so that systemd does not reset the cgroups of docker containers Delegate=yes # kill only the docker process, not all processes in the cgroup KillMode=process OOMScoreAdjust=-500 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl start docker systemctl status docker systemctl enable docker #至此。docker离线安装已经完成,可以参照上面的步骤设置镜像加速器。
#设置docker内核流量转发,配置内核参数 cat >>/etc/sysctl.d/docker.conf <<EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.all.rp_filter = 0 net.ipv4.ip_forward = 1 EOF #在线安装 #保证虚拟机能连外网,因为要从网络下载docker的yum仓库 #先删除旧版本的docker #安装yum-utils,主要提供yum-config-manager命令 #下载并安装docker的镜像仓库 #查看可获取的docker版本 #可直接安装最新的docker版本 #或者安装指定版本 #设置镜像加速器 yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine yum install -y yum-utils yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yum list docker-ce --showduplicates | sort -r yum install docker-ce docker-ce-cli containerd.io -y yum install docker-ce-20.10.9 docker-ce-cli-20.10.9 containerd.io -y systemctl start docker systemctl status docker | grep running systemctl enable docker docker -v mkdir /etc/docker cat >> /etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"] } EOF systemctl restart docker #离线安装 #下载 https://download.docker.com/linux/static/stable/x86_64/docker-20.10.17.tgz 离线安装包 #将docker目录的可执行文件复制到/usr/bin/下 #创建一个docker.service文件,并放到/usr/lib/systemd/system/目录下 #设置镜像加速器 tar -xf docker-20.10.17.tgz && cp docker/* /usr/bin/ vim /usr/lib/systemd/system/docker.service systemctl daemon-reload systemctl start docker systemctl status docker mkdir /etc/docker cat >>/etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"] } EOF systemctl restart docker
docker-compose 是用于定义和运行多容器 Docker 应用程序的一个工具。通过 Compose,您可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。可以理解为docker-compose就是容器编排工具。当然,目前最流行的容器编排工具是k8s。
#docker-compose的安装很简单,直接下载二进制可执行文件即可 #在线下载v2.12.2 curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose curl -SL https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose # 因为Docker Compose存放在GitHub,可能不太稳定。可以通过DaoCloud加速下载 curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose #其他网友分享的下载 wget https://github.worker.liangyuanpeng.com/docker/compose/releases/download/v2.22.0/docker-compose-linux-x86_64 #授与可执行权限 chmod a+x /usr/local/bin/docker-compose docker-compose -v #GitHub官网下载不了的,也可以直接联网yum安装,yum安装的版本可能不是最新的 yum -y install epel-release yum install docker-compose -y
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。