前言:
    有些特殊情况,服务器是在内网的无法连接外网安装Docker和docker-compose。Docker官网也是提供了离线安装模式。
    根据不同的系统有环境要求,这块可以参考Docker官网 https://docs.docker.com/engine/install/

    Docker安装
    环境要求:
    1、centos7以上(包含centos7)
    2、kernel内核:3.10及以上 查看命令 uname -r
    Centos 7 二进制离线安装 Docker - 图1

    离线安装包:
    1、准备docker安装包和安装脚本

    Docker官方下载地址:https://download.docker.com/linux/static/stable/x86_64/
    或者选择阿里云镜像站下载 https://mirrors.aliyun.com//docker-ce/linux/static/stable/x86_64/
    根据自己的需要进行下载,这块我选择最新的docker-20.10.5.tgz
    image.png
    2、Docker安装脚本
    创建install-docker.sh脚本:

    1. #!/bin/sh
    2. usage(){
    3. echo "使用方法: $0 FILE_NAME_DOCKER_CE_TAR_GZ"
    4. echo " $0 docker-20.10.5.tgz"
    5. echo "Get docker-ce binary from: https://download.docker.com/linux/static/stable/x86_64/"
    6. echo "eg: wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.5.tgz"
    7. echo ""
    8. }
    9. SYSTEMDDIR=/usr/lib/systemd/system
    10. SERVICEFILE=docker.service
    11. DOCKERDIR=/usr/bin
    12. DOCKERBIN=docker
    13. SERVICENAME=docker
    14. if [ $# -ne 1 ]; then
    15. usage
    16. exit 1
    17. else
    18. FILETARGZ="$1"
    19. fi
    20. if [ ! -f ${FILETARGZ} ]; then
    21. echo "Docker binary tgz files does not exist, please check it"
    22. echo "Get docker-ce binary from: https://download.docker.com/linux/static/stable/x86_64/"
    23. echo "eg: wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.5.tgz"
    24. exit 1
    25. fi
    26. echo "##unzip : tar xvpf ${FILETARGZ}"
    27. tar xvpf ${FILETARGZ}
    28. echo
    29. echo "##binary : ${DOCKERBIN} copy to ${DOCKERDIR}"
    30. cp -p ${DOCKERBIN}/* ${DOCKERDIR} >/dev/null 2>&1
    31. which ${DOCKERBIN}
    32. echo "##systemd service: ${SERVICEFILE}"
    33. echo "##docker.service: create docker systemd file"
    34. cat >${SYSTEMDDIR}/${SERVICEFILE} <<EOF
    35. [Unit]
    36. Description=Docker Application Container Engine
    37. Documentation=http://docs.docker.com
    38. After=network.target docker.socket
    39. [Service]
    40. Type=notify
    41. EnvironmentFile=-/run/flannel/docker
    42. WorkingDirectory=/usr/local/bin
    43. ExecStart=/usr/bin/dockerd \
    44. -H tcp://0.0.0.0:4243 \
    45. -H unix:///var/run/docker.sock \
    46. --selinux-enabled=false \
    47. --log-opt max-size=1g
    48. ExecReload=/bin/kill -s HUP $MAINPID
    49. # Having non-zero Limit*s causes performance problems due to accounting overhead
    50. # in the kernel. We recommend using cgroups to do container-local accounting.
    51. LimitNOFILE=infinity
    52. LimitNPROC=infinity
    53. LimitCORE=infinity
    54. # Uncomment TasksMax if your systemd version supports it.
    55. # Only systemd 226 and above support this version.
    56. #TasksMax=infinity
    57. TimeoutStartSec=0
    58. # set delegate yes so that systemd does not reset the cgroups of docker containers
    59. Delegate=yes
    60. # kill only the docker process, not all processes in the cgroup
    61. KillMode=process
    62. Restart=on-failure
    63. [Install]
    64. WantedBy=multi-user.target
    65. EOF
    66. echo ""
    67. systemctl daemon-reload
    68. echo "##Service status: ${SERVICENAME}"
    69. systemctl status ${SERVICENAME}
    70. echo "##Service restart: ${SERVICENAME}"
    71. systemctl restart ${SERVICENAME}
    72. echo "##Service status: ${SERVICENAME}"
    73. systemctl status ${SERVICENAME}
    74. echo "##Service enabled: ${SERVICENAME}"
    75. systemctl enable ${SERVICENAME}
    76. cat >/etc/docker/daemon.json <<EOF
    77. {
    78. "registry-mirrors": ["https://bn4ll166.mirror.aliyuncs.com"],
    79. "data-root": "/data/docker-root",
    80. "dns": ["223.5.5.5", "119.29.29.29"],
    81. "bip": "172.31.255.254/16",
    82. "storage-driver": "overlay2",
    83. "storage-opts": [
    84. "overlay2.override_kernel_check=true"
    85. ],
    86. "userland-proxy": false,
    87. "log-driver": "json-file",
    88. "log-opts": {
    89. "max-size": "50m",
    90. "max-file": "1"
    91. }
    92. }
    93. EOF
    94. swapoff -a
    95. iptables -P FORWARD ACCEPT
    96. sysctl --system
    97. systemctl daemon-reload
    98. systemctl restart docker.service
    99. echo "## docker version"
    100. docker version

    授权install-docker.sh为可以执行脚本,chmod +x install-docker.sh

    3、执行install-docker.sh脚本,进行安装docker
    Centos 7 二进制离线安装 Docker - 图3

    Centos 7 二进制离线安装 Docker - 图4
    版本号打印
    Centos 7 二进制离线安装 Docker - 图5
    安装成功
    Centos 7 二进制离线安装 Docker - 图6