Centos离线安装docker教程版

  • 44

Centos离线安装docker

所需环境

  1. Centos7.0以上版本,内核版本需要大于3.10(使用uname -r查看内核版本)
  2. docker安装包18.06.3(19版本我在使用中报了oci runtime的错误,但是18版本没问题)下载安装包 密码nyq7

建立安装脚本

将docker-18.06.3-ce.tgz传输到CentOS系统后,同目录下新建脚本install-docker.sh,输入以下内容:

#!/bin/sh

usage(){
  echo "使用方法: $0 FILE_NAME_DOCKER_CE_TAR_GZ"
  echo "          $0 docker-18.06.3-ce.tgz"
  echo "Get docker-ce binary from: https://download.docker.com/linux/static/stable/x86_64/"
  echo "eg: wget https://download.docker.com/linux/static/stable/x86_64/docker-18.06.3-ce.tgz"
  echo ""
}
SYSTEMDDIR=/usr/lib/systemd/system
SERVICEFILE=docker.service
DOCKERDIR=/usr/bin
DOCKERBIN=docker
SERVICENAME=docker

if [ $# -ne 1 ]; then
  usage
  exit 1
else
  FILETARGZ="$1"
fi

if [ ! -f ${FILETARGZ} ]; then
  echo "Docker binary tgz files does not exist, please check it"
  echo "Get docker-ce binary from: https://download.docker.com/linux/static/stable/x86_64/"
  echo "eg: wget https://download.docker.com/linux/static/stable/x86_64/docker-18.06.3-ce.tgz"
  exit 1
fi

echo "##unzip : tar xvpf ${FILETARGZ}"
tar xvpf ${FILETARGZ}
echo

echo "##binary : ${DOCKERBIN} copy to ${DOCKERDIR}"
cp -p ${DOCKERBIN}/* ${DOCKERDIR} >/dev/null 2>&1
which ${DOCKERBIN}

echo "##systemd service: ${SERVICEFILE}"
echo "##docker.service: create docker systemd file"
cat >${SYSTEMDDIR}/${SERVICEFILE} <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target docker.socket
[Service]
Type=notify
EnvironmentFile=-/run/flannel/docker
WorkingDirectory=/usr/local/bin
ExecStart=/usr/bin/dockerd \
                -H tcp://0.0.0.0:4243 \
                -H unix:///var/run/docker.sock \
                --selinux-enabled=false \
                --log-opt max-size=1g
ExecReload=/bin/kill -s HUP $MAINPID
# 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
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# 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
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

echo ""

systemctl daemon-reload
echo "##Service status: ${SERVICENAME}"
systemctl status ${SERVICENAME}
echo "##Service restart: ${SERVICENAME}"
systemctl restart ${SERVICENAME}
echo "##Service status: ${SERVICENAME}"
systemctl status ${SERVICENAME}

echo "##Service enabled: ${SERVICENAME}"
systemctl enable ${SERVICENAME}
cat >/etc/docker/daemon.json <<EOF
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF
swapoff -a
iptables -P FORWARD ACCEPT
sysctl --system
systemctl daemon-reload
systemctl restart docker.service

echo "## docker version"
docker version

执行离线安装脚本

准备工作做完后,需执行安装脚本,把install-docker.sh设置为可执行文件,通过 chmod +x install-docker.sh 脚本即可。然后执行以下命令

sudo sh intall-docker.sh docker-18.06.3-ce.tgz

执行完毕后,查看安装日志,如下:

##unzip : tar xvpf docker-18.06.3-ce.tgz
docker/
docker/docker-containerd
docker/docker-proxy
docker/docker
docker/docker-runc
docker/dockerd
docker/docker-containerd-ctr
docker/docker-containerd-shim
docker/docker-init

##binary : docker copy to /usr/bin
/bin/docker
##systemd service: docker.service
##docker.service: create docker systemd file

##Service status: docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: http://docs.docker.com
##Service restart: docker
##Service status: docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-11-26 15:06:56 CST; 20ms ago
     Docs: http://docs.docker.com
 Main PID: 1576 (dockerd)
   Memory: 19.7M
   CGroup: /system.slice/docker.service
           ├─1576 /usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock --selinux-enabled=false --log-opt max-size=1g
           └─1582 docker-containerd --config /var/run/docker/containerd/containerd.toml

Nov 26 15:06:55 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:55.129484798+08:00" level=info msg="Loading containers: start."
Nov 26 15:06:55 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:55.314061432+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon opt...d IP address"
Nov 26 15:06:55 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:55.426438212+08:00" level=info msg="Loading containers: done."
Nov 26 15:06:55 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:55.428005252+08:00" level=warning msg="Not using native diff for overlay2, this may cause degraded performance for bu...iver=overlay2
Nov 26 15:06:55 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:55.443011892+08:00" level=info msg="Docker daemon" commit=d7080c1 graphdriver(s)=overlay2 version=18.06.3-ce
Nov 26 15:06:55 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:55.443130693+08:00" level=info msg="Daemon has completed initialization"
Nov 26 15:06:56 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:56.073425584+08:00" level=warning msg="Could not register builder git source: failed to find git binary: exec: \"git\...und in $PATH"
Nov 26 15:06:56 iz8vb85pir06geyi1qr6koz systemd[1]: Started Docker Application Container Engine.
Nov 26 15:06:56 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:56.083618471+08:00" level=info msg="API listen on /var/run/docker.sock"
Nov 26 15:06:56 iz8vb85pir06geyi1qr6koz dockerd[1576]: time="2019-11-26T15:06:56.089479751+08:00" level=info msg="API listen on [::]:4243"
Hint: Some lines were ellipsized, use -l to show in full.
##Service enabled: docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
* Applying /usr/lib/sysctl.d/00-system.conf ...
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /etc/sysctl.d/99-sysctl.conf ...
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
kernel.sysrq = 1
* Applying /etc/sysctl.conf ...
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
kernel.sysrq = 1
## docker version
Client:
 Version:           18.06.3-ce
 API version:       1.38
 Go version:        go1.10.4
 Git commit:        d7080c1
 Built:             Wed Feb 20 02:24:22 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.3-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       d7080c1
  Built:            Wed Feb 20 02:25:33 2019
  OS/Arch:          linux/amd64
  Experimental:     false

如果最后出现docker的版本信息就表示离线安装docker成功了。

方法二

通过wget下载docker版本,这里使用的是19.0+版本

 wget https://download.docker.com/linux/static/stable/x86_64/docker-19.03.0.tgz

下载完成后,解压docker包

tar -zxvf docker-19.03.0.tgz
docker/
docker/ctr
docker/runc
docker/dockerd
docker/docker
docker/containerd
docker/docker-init
docker/containerd-shim

复制docker包下的内容之/usr/bin/目录

cp docker/* /usr/bin/

进入/etc/systemd/system/目录下,创建docker.service文件。

cd /etc/systemd/system/
vi docker.service

复制如下内容至docker.service中

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.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 --selinux-enabled=false --insecure-registry=127.0.0.1
ExecReload=/bin/kill -s HUP $MAINPID
# 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
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# 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
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

此处的--insecure-registry=127.0.0.1(此处改成你私服ip)设置是针对有搭建了自己私服Harbor时允许docker进行不安全的访问,否则访问将会被拒绝。

docker.service设置执行权限
chmod +x /etc/systemd/system/docker.service

重新加载配置文件(每次有修改docker.service文件时都要重新加载下)

systemctl daemon-reload

启动docker

systemctl start docker
//设置docker开机启动
systemctl enable docker.service

查看docker状态

systemctl status docker

Centos离线安装docker

📚文章说明:
1、本文章链接失效后,请通过右下角企鹅QQ告知管理员,24小时内补链接,谢谢。点击下载说明了解。
2、天天精品分享的资源均通过网络公开合法渠道获取的,仅阅读交流测试使用,请在下载后24小时内删除。
3、版权归作者或出版社方所有,本站不对涉及的版权问题负法律责任。点击免责申明了解详情。
4、若版权方认为天天精品侵权,请联系客服QQ或发送邮件myttjp@163.com处理。
5、会员和精品豆系对搜集搬运、整理及网站运营做的友情赞助,非购买文件费用,敬请谅解。点击关于本站了解本站。
6、每位访客应尊重版权方的知识产权,支持版权方和出版社。