centos7.9在线离线安装docker

yum在线安装

# 卸载旧版本
[root@localhost ~]# yum remove docker docker-client docker-client-latest docker-common docker-latest \
docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
 
# 安装所需的软件包。yum-utils 提供了 yum-config-manager
[root@localhost ~]# yum install -y yum-utils
 
# 添加 yum 软件源(三选一)
# -----------------------------------------------------------------------------------------------------------------------------
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  # 阿里源
 
yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo  # 清华大学源
 
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo   # 官方源(较慢)
# -----------------------------------------------------------------------------------------------------------------------------
 
# 列出存储库中可用的版本。按版本号(从高到低)
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirrors.aliyun.com
docker-ce.x86_64            3:26.1.4-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.1.3-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.1.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.1.1-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.1.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.0.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.0.1-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.0.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.5-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.4-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.3-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.1-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.9-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.8-1.el7                      docker-ce-stable
 
# 方式一:选择安装最新版本
[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io
 
#实际执行时遇到的问题

#上述命令执行后会报错:
错误:软件包:3:docker-ce-26.1.4-1.el7.x86_64 (docker-ce-stable)
          需要:container-selinux >= 2:2.74
错误:软件包:docker-ce-rootless-extras-26.1.4-1.el7.x86_64 (docker-ce-stable)
          需要:slirp4netns >= 0.4
错误:软件包:docker-ce-rootless-extras-26.1.4-1.el7.x86_64 (docker-ce-stable)
          需要:fuse-overlayfs >= 0.7
错误:软件包:containerd.io-1.6.33-3.1.el7.x86_64 (docker-ce-stable)
          需要:container-selinux >= 2:2.74
这是下载源没有找到对应的安装包,更换下载源即可,如下操作:
文章地址:https://blog.csdn.net/qq_58159763/article/details/143830116
 直接到阿里巴巴开源镜像站下载Centos-7.repo yum源
 wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
更换yum源后记得执行:yum clean all && yum repolist
再次执行yum install docker-ce docker-ce-cli containerd.io 即可。

 
# 或方式二:选择安装指定版本 例如docker-ce-24.0.9
yum install docker-ce- docker-ce-cli- containerd.io
 
# 若选择测试版本的 Docker 请执行以下命令
yum-config-manager --enable docker-ce-test
  
# 验证
[root@localhost ~]# docker -v
 
[root@localhost ~]# docker version
Client:
 Version:           23.0.6
 API version:       1.42
 Go version:        go1.19.9
 Git commit:        ef23cbc
 Built:             Fri May  5 21:13:15 2023
 OS/Arch:           linux/amd64
 Context:           default
 
Server: Docker Engine - Community
 Engine:
  Version:          23.0.6
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.9
  Git commit:       9dbdbd4
  Built:            Fri May  5 21:14:22 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.6.21
  GitCommit:        3dce8eb055cbb6872793272b4f20ed16117344f8
 runc:
  Version:          1.1.7
  GitCommit:        v1.1.7-0-g860f061
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
 
# 添加国内镜像加速(每次修改需重启 docker)
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://dockerpull.com",
        "https://docker.anyhub.us.kg",
        "https://dockerhub.jobcher.com",
        "https://dockerhub.icu",
        "https://docker.awsl9527.cn",
        "https://dockerproxy.com",
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com",
        "https://ccr.ccs.tencentyun.com"
    ]
}
EOF
 
# 重载
[root@localhost ~]# systemctl daemon-reload && systemctl restart docker
 
# 启动|停止|查看|开机自启
[root@localhost ~]# systemctl start|stop|status|enable docker
 
# 测试 Docker 是否安装正确
[root@localhost ~]# docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d195baed67eb: Pull complete
Digest: sha256:be06e3c4ce8780c0f87fbf66ec9b34623ba2fd14caa5559be5b593653821b814
Status: Downloaded newer image for hello-world:latest
 
Hello from Docker!
This message shows that your installation appears to be working correctly.
 
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
 
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
 
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
 
For more examples and ideas, visit:
 https://docs.docker.com/get-started/
 
若能正常输出以上信息,则说明安装成功。

离线安装-未亲自验证

下载地址:https://download.docker.com/linux/static/stable/x86_64/
阿里下载地址:https://mirrors.aliyun.com/docker-ce/linux/static/stable/x86_64/docker-23.0.6.tgz

清华:https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/docker-23.0.6.tgz

# 下载 Docker docker-23.0.6.tgz 为例 (当前最新版本 docker-27.2.0.tgz)
[root@localhost ~]# wget https://mirrors.aliyun.com/docker-ce/linux/static/stable/x86_64/docker-23.0.6.tgz
[root@localhost ~]# tar -xf docker-23.0.6.tgz
[root@localhost ~]# cp docker/* /usr/local/bin/
 
[root@localhost ~]# docker -v
Docker version 23.0.6, build ef23cbc
[root@localhost ~]# docker -h
 
# 将docker注册为 Systemd 的 service
# /usr/lib/systemd/system/docker.service 或 /etc/systemd/system/docker.service
[root@localhost ~]# cat > /usr/lib/systemd/system/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/local/bin/dockerd -H unix:///var/run/docker.sock --selinux-enabled=false --default-ulimit nofile=65536:65536
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
 
EOF
 
 
# 如果需要开启远程服务ExecStart属性修改为以下命令:
ExecStart=/usr/local/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --selinux-enabled=false --default-ulimit nofile=65536:65536
# -H tcp://0.0.0.0:2375 开启远程连接命令
# 测试远程连接  docker -H DockerHost_ip version
 
# 添加文件可执行权限
[root@localhost ~]# chmod +x /usr/lib/systemd/system/docker.service
 
# 添加国内镜像加速(每次修改需重启 docker)
[root@localhost ~]# mkdir /etc/docker
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://dockerpull.com",
        "https://docker.anyhub.us.kg",
        "https://dockerhub.jobcher.com",
        "https://dockerhub.icu",
        "https://docker.awsl9527.cn"
    ]
}
EOF
 
# 重载
[root@localhost ~]# systemctl daemon-reload && systemctl restart docker
 
# 启动|停止|查看|开机自启
[root@localhost ~]# systemctl start|stop|status|enable docker
 
# 测试 Docker 是否安装正确
[root@localhost ~]# docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d195baed67eb: Pull complete
Digest: sha256:be06e3c4ce8780c0f87fbf66ec9b34623ba2fd14caa5559be5b593653821b814
Status: Downloaded newer image for hello-world:latest
 
Hello from Docker!
This message shows that your installation appears to be working correctly.
 
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
 
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
 
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
 
For more examples and ideas, visit:
 https://docs.docker.com/get-started/
 
若能正常输出以上信息,则说明安装成功。

你可能感兴趣的:(环境搭建,docker,容器,运维)