Loading

docker二进制安装

docker_install.sh

#!/bin/bash


base_path=$(
    cd "$(dirname "$0")" || exit 1
    pwd
)

function download_jq() {
    wget "https://github.com/jqlang/jq/releases/download/${jq_tag}/jq-linux-amd64" -P $base_path
}

function download_docker() {
    wget https://download.docker.com/linux/static/stable/x86_64/docker-${docker_version:1}.tgz -P $base_path
    wget https://raw.githubusercontent.com/docker/cli/${docker_version}/contrib/completion/bash/docker -P $base_path
    # wget https://raw.githubusercontent.com/moby/moby/${docker_version}/contrib/init/systemd/docker.service -P $base_path
    # wget https://raw.githubusercontent.com/moby/moby/${docker_version}/contrib/init/systemd/docker.socket -P $base_path
}

function download_compose() {
    wget https://github.com/docker/compose/releases/download/${compose_version}/docker-compose-`uname -s`-`uname -m` -P $base_path
}

function install_jq() {
    cp jq-linux-amd64 /usr/local/bin/jq
    chmod +x /usr/local/bin/jq
}

function install_docker() {
    # yum install bash-completion -y
    mkdir -p /etc/docker
    mkdir -p /etc/bash_completion.d
    cp ${base_path}/docker /etc/bash_completion.d/docker
    read -p "输入docker安装目录,直接回车则为默认值/usr/bin: " docker_bin
    read -p "输入docker数据存储目录,直接回车则为默认值/var/lib/docker: " data_root
    read -p "输入docker信任私有仓库,直接回车则默认值为127.0.0.1: " insecure_registries
    cat > /usr/lib/systemd/system/docker.socket <<-EOF
[Unit]
Description=Docker Socket for the API
[Socket]
ListenStream=/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
EOF
    cat > /usr/lib/systemd/system/docker.service <<-EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket
[Service]
Type=notify
ExecStart=${docker_bin:-/usr/bin}/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP \$MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
OOMScoreAdjust=-500
[Install]
WantedBy=multi-user.target
EOF
    cat > /etc/docker/daemon.json <<-EOF
{
    "exec-opts":[
        "native.cgroupdriver=systemd"
    ],
    "insecure-registries":[
        "${insecure_registries:-127.0.0.1}"
    ],
    "max-concurrent-downloads":10,
    "default-ulimits":{
        "nofile":{
            "Name":"nofile",
            "Hard":65535,
            "Soft":65535
        }
    },
    "live-restore":true,
    "log-driver":"json-file",
    "log-level":"warn",
    "log-opts":{
        "max-size":"10m",
        "max-file":"5"
    },
    "data-root":"${data_root:-/var/lib/docker}"
}
EOF
    tar -zxvf ${base_path}/docker-${docker_version:1}.tgz -C ${docker_bin:-/usr/bin} --strip-components=1
    groupadd docker
    usermod -aG docker ${USER}
    systemctl daemon-reload
    systemctl enable --now docker.service
}

function install_compose() {
    DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
    mkdir -p $DOCKER_CONFIG/cli-plugins
    cp ${base_path}/docker-compose-`uname -s`-`uname -m` /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
}

function main() {
   jq_tag="$(curl --retry 5 -sSL https://api.github.com/repos/jqlang/jq/releases/latest | grep '  "tag_name": ' | awk -F'"' '{print $4}')"
   docker_version="$(curl --retry 5 -sSL https://api.github.com/repos/moby/moby/releases/latest | jq -r '.tag_name' || exit 1)"
   compose_version="$(curl --retry 5 -sSL https://api.github.com/repos/docker/compose/releases/latest | jq -r '.tag_name' || exit 1)"
   download_jq
   download_docker
   download_compose
   jq --version > /dev/null 2>&1 || install_jq
   echo -e "$TIMESTAMP \033[36mDEBUG\033[0m $2"
   docker version > /dev/null 2>&1 && echo -e "\e[32mINFO\e[0m docker已存在,跳过安装" || install_docker
   docker-compose version > /dev/null 2>&1 && echo -e "\e[32mINFO\e[0m docker-compose已存在,跳过安装" || install_compose
}

main $@
posted @ 2023-10-30 18:25  小维江湖  阅读(84)  评论(0编辑  收藏  举报