Linux离线安装docker

Posted 我想吃晚饭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux离线安装docker相关的知识,希望对你有一定的参考价值。

2019/10/24,docker

摘要:CentOS 7.6中离线安装docker 18.06.3以及docker-compose 1.24.1
在线安装可参照 文档
Ubuntu18.04离线安装docker可以参考博客Ubuntu18.04通过deb包离线安装docker-ce

所需环境

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

建立安装脚本

将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

运行安装脚本

脚本新建好后应该如下图所示

接下来运行脚本安装:

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

安装会自动启用docker服务,自启动等。最后输出docker版本即表示已经安装成功

如果是非root账号,此时使用docker run等命令总是需要sudo权限,需要给当前用户添加docker权限

添加docker权限

建立docker组:

sudo groupadd docker

将当前用户加入 docker 组:

sudo usermod -aG docker $USER

重启 docker 服务:

sudo service docker restart 

此时断开ssh重新连接(或者退出当前终端并重新登录)
运行docker run不再提示没有权限

安装docker-compose

以下命令会联网下载docker-compose到 /usr/local/bin/目录下:

sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

#或使用github的地址(速度可能会慢一点)
#sudo curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

经过我自己的验证,非root账户使用上面两个指令是报错的:
-bash: /usr/local/bin/docker-compose: Permission denied,提示权限拒绝
下载到当前文件夹,之后在copy到/usr/local/bin/就好了
如果不能联网,可以在别的系统下下载好,拷贝到/usr/local/bin/目录下也是可以的:

sudo cp -r docker-compose /usr/local/bin/docker-compose


接下来对二进制包赋予权限:
```bash
sudo chmod +x /usr/local/bin/docker-compose

软链接到/usr/bin/下,防止sudo docker-compose出现command not found:

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

完成后,docker-compose即可使用:

docker-compose --version

以上是关于Linux离线安装docker的主要内容,如果未能解决你的问题,请参考以下文章

Linux 环境 Docker 离线安装

Linux 环境 Docker 离线安装

Linux离线安装docker

redhat7.5 离线安装docker

windows server 离线安装docker

离线安装docker时报错