首页 > 系统相关 >Ubuntu 22.04 LTS 在线/离线安装 Docker

Ubuntu 22.04 LTS 在线/离线安装 Docker

时间:2024-10-28 08:47:15浏览次数:5  
标签:LTS 0.0 离线 22.04 ubuntu2204 docker 32 root Docker

Docker 安装方式

准备环境

root@ubuntu2204:~# hostnamectl
 Static hostname: ubuntu2204
       Icon name: computer-vm
         Chassis: vm
      Machine ID: 06204406df2343f89f6ba6550a356854
         Boot ID: 3142ea12a9e64adbab7b8939935e7698
  Virtualization: vmware
Operating System: Ubuntu 22.04.5 LTS
          Kernel: Linux 5.15.0-43-generic
    Architecture: x86-64
 Hardware Vendor: VMware, Inc.
  Hardware Model: VMware Virtual Platform

# 关闭 swap
root@ubuntu2204:~# swapoff -a
root@ubuntu2204:~# sed -ri.bak 's/.*swap.*/#&/' /etc/fstab

# 更换阿里源
root@ubuntu2204:~# cp /etc/apt/sources.list /etc/apt/sources.list_bak
root@ubuntu2204:~# cat /etc/apt/sources.list
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

root@ubuntu2204:~# apt update
root@ubuntu2204:~# apt upgrade -y

在线 APT 安装

# 卸载 docker 旧版本
root@ubuntu2204:~# for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
root@ubuntu2204:~# rm /var/lib/docker /var/lib/containerd -rf

# 安装必要的依赖包
root@ubuntu2204:~# apt install apt-transport-https ca-certificates curl gnupg lsb-release -y

# 添加软件源的GPG密钥及docker源(三选一)
# ------------------------------------------------------------------------------------------------------------
# 一:阿里
root@ubuntu2204:~# curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
root@ubuntu2204:~# add-apt-repository "deb [arch=$(dpkg --print-architecture)] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

# 二:清华
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

# 三:docker官方
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# ------------------------------------------------------------------------------------------------------------

# 方式一:选择安装最新稳定版
root@ubuntu2204:~# apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

# 或方式二:选择安装指定稳定版, 例如 5:24.0.9-1~ubuntu.22.04~jammy
root@ubuntu2204:~# apt-cache madison docker-ce | awk '{ print $3 }' | less
5:27.3.1-1~ubuntu.22.04~jammy
5:27.3.0-1~ubuntu.22.04~jammy
......
5:25.0.3-1~ubuntu.22.04~jammy
5:25.0.2-1~ubuntu.22.04~jammy
5:25.0.1-1~ubuntu.22.04~jammy
5:25.0.0-1~ubuntu.22.04~jammy
5:24.0.9-1~ubuntu.22.04~jammy
5:24.0.8-1~ubuntu.22.04~jammy
......

root@ubuntu2204:~# VERSION_STRING=5:24.0.9-1~ubuntu.22.04~jammy
root@ubuntu2204:~# apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin -y

# 查看docker信息
root@ubuntu2204:~# docker -v
Docker version 24.0.9, build 2936816

root@ubuntu2204:~# docker version
Client: Docker Engine - Community
 Version:           24.0.9
 API version:       1.43
 Go version:        go1.20.13
 Git commit:        2936816
 Built:             Thu Feb  1 00:48:39 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.9
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.13
  Git commit:       fca702d
  Built:            Thu Feb  1 00:48:39 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.22
  GitCommit:        7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc:
  Version:          1.1.14
  GitCommit:        v1.1.14-0-g2c9f560
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0


root@ubuntu2204:~# ps -ef | grep docker
root        2032       1  0 14:51 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root        2214     940  0 14:51 pts/0    00:00:00 grep --color=auto docker

# 启动|停止|查看|开机自启
root@ubuntu2204:~# systemctl start|stop|status|enable docker

# 测试 docker 是否安装正确
root@ubuntu2204:~# docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:d211f485f2dd1dee407a80973c8f129f00d54604d2c90732e8e320e5038a0348
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/

若能正常输出以上信息,则说明安装成功。

二进制离线安装

时间:2024-10 下载 Docker docker-24.0.9.tgz 为例

下载 Docker 地址

# 下载 Docker docker-24.0.9.tgz 为例 (当前最新版本 docker-27.3.1.tgz)
root@ubuntu2204:~# wget https://mirrors.aliyun.com/docker-ce/linux/static/stable/x86_64/docker-24.0.9.tgz
root@ubuntu2204:~# tar -xf docker-24.0.9.tgz
root@ubuntu2204:~# cp -a docker/* /usr/local/bin/
root@ubuntu2204:~# docker -v
Docker version 24.0.9, build 2936816
root@ubuntu2204:~# docker -h

# 将docker注册为 Systemd 的 service
# /usr/lib/systemd/system/docker.service 或 /etc/systemd/system/docker.service
[root@ubuntu2204 ~]# cat > /usr/lib/systemd/system/docker.service <<EOF
[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 --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 --default-ulimit nofile=65536:65536
# -H tcp://0.0.0.0:2375 开启远程连接命令
# 测试远程连接  docker -H DockerHost_ip version

# 添加文件可执行权限
[root@ubuntu2204 ~]# chmod +x /usr/lib/systemd/system/docker.service

# 启动
[root@ubuntu2204 ~]# systemctl daemon-reload && systemctl start docker

# 启动|停止|查看|开机自启
root@ubuntu2204:~# systemctl start|stop|status|enable docker

此时可能会无法启动报错信息如下

root@ubuntu2204:~# systemctl daemon-reload && systemctl start docker
Job for docker.service failed because the control process exited with error code.
See "systemctl status docker.service" and "journalctl -xeu docker.service" for details.

root@ubuntu2204:~# systemctl status docker.service
......
Oct 21 17:24:36 ubuntu2204 systemd[1]: docker.service: Scheduled restart job, restart counter is at 3.
Oct 21 17:24:36 ubuntu2204 systemd[1]: Stopped Docker Application Container Engine.
Oct 21 17:24:36 ubuntu2204 systemd[1]: docker.service: Start request repeated too quickly.
Oct 21 17:24:36 ubuntu2204 systemd[1]: docker.service: Failed with result 'exit-code'.
Oct 21 17:24:36 ubuntu2204 systemd[1]: Failed to start Docker Application Container Engine.

root@ubuntu2204:~# journalctl -xeu docker.service
Oct 21 17:32:56 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:56.838180448+08:00" level=info msg="Starting up"
Oct 21 17:32:56 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:56.838180448+08:00" level=info msg="Starting up"
Oct 21 17:32:56 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:56.846181372+08:00" level=warning msg="could not change group /var/run/docker.sock to docker: group docker not found"
Oct 21 17:32:56 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:56.846351136+08:00" level=info msg="containerd not running, starting managed containerd"
Oct 21 17:32:56 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:56.847171970+08:00" level=info msg="started new containerd process" address=/var/run/docker/containerd/containerd.sock module=libcontainerd pid=1011
Oct 21 17:32:56 ubuntu2204 dockerd[1011]: time="2024-10-21T17:32:56.861897616+08:00" level=info msg="starting containerd" revision=7c3aca7a610df76212171d200ca3811ff6096eb8 version=v1.7.13
Oct 21 17:32:56 ubuntu2204 dockerd[1011]: time="2024-10-21T17:32:56.879453787+08:00" level=info msg="loading plugin \"io.containerd.warning.v1.deprecations\"..." type=io.containerd.warning.v1
......
Oct 21 17:32:56 ubuntu2204 dockerd[1011]: time="2024-10-21T17:32:56.915675066+08:00" level=info msg=serving... address=/var/run/docker/containerd/containerd.sock
Oct 21 17:32:56 ubuntu2204 dockerd[1011]: time="2024-10-21T17:32:56.915690393+08:00" level=info msg="containerd successfully booted in 0.054759s"
Oct 21 17:32:56 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:56.922245751+08:00" level=info msg="detected 127.0.0.53 nameserver, assuming systemd-resolved, so using resolv.conf: /run/systemd/resolve/resolv.conf"
Oct 21 17:32:57 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:57.069029579+08:00" level=info msg="Loading containers: start."
Oct 21 17:32:57 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:57.131593492+08:00" level=warning msg="failed to find iptables" error="exec: \"iptables\": executable file not found in $PATH"
Oct 21 17:32:57 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:57.134033772+08:00" level=info msg="stopping event stream following graceful shutdown" error="<nil>" module=libcontainerd namespace=moby
Oct 21 17:32:57 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:57.134833160+08:00" level=info msg="stopping event stream following graceful shutdown" error="context canceled" module=libcontainerd namespace=plugins.moby
Oct 21 17:32:57 ubuntu2204 dockerd[1001]: time="2024-10-21T17:32:57.134935282+08:00" level=info msg="stopping healthcheck following graceful shutdown" module=libcontainerd
Oct 21 17:32:58 ubuntu2204 dockerd[1001]: failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: Iptables not found
Oct 21 17:32:58 ubuntu2204 systemd[1]: docker.service: Main process exited, code=exited, status=1/FAILUR
.....

原因:这些错误表明 Docker 无法找到 iptables 命令,它是用于配置网络规则和转发的重要工具。缺少 iptables 可能会导致 Docker 无法初始化网络控制器,从而无法启动。
解决方法

# 检查 iptables 是否已安装
root@ubuntu2204:~# which iptables
root@ubuntu2204:~# apt-get install iptables  # 或重新安装:apt-get install --reinstall iptables

# 再次启动 docker
root@ubuntu2204:~# systemctl start docker
root@ubuntu2204:~# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  0.0.0.0/0            0.0.0.0/0
DOCKER-ISOLATION-STAGE-1  all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  0.0.0.0/0            0.0.0.0/0
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  0.0.0.0/0            0.0.0.0/0
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

# 测试 Docker 是否安装正确
root@ubuntu2204:~# docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:d211f485f2dd1dee407a80973c8f129f00d54604d2c90732e8e320e5038a0348
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 镜像加速

# 每次修改 daemon.json 后,必须重载配置及重启 docker
root@ubuntu2204:~# mkdir -p /etc/docker
root@ubuntu2204:~# 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@ubuntu2204:~# systemctl daemon-reload && sudo systemctl restart docker
root@ubuntu2204:~# docker info

标签:LTS,0.0,离线,22.04,ubuntu2204,docker,32,root,Docker
From: https://www.cnblogs.com/zhangwencheng/p/18472429

相关文章

  • windows安装MySQL8.4.3LTS
    windows安装MySQL8.4.3LTS第一步:下载压缩文件https://dev.mysql.com/downloads/mysql/第二步:解压压缩包以及编写配置my.ini配置[mysqld]#skip_grant_tables#设置3306端口port=3306#设置mysql的安装目录basedir=D:\\Software\\mysql-8.4.3-winx64#设置mysql数据库......
  • Windows 11 version 24H2 & LTSC 2024 中文版、英文版 (x64、ARM64) 下载 (updated Oc
    Windows11version24H2&LTSC2024中文版、英文版(x64、ARM64)下载(updatedOct2024)Windows11,version24H2,企业版arm64x64请访问原文链接:https://sysin.org/blog/windows-11/查看最新版。原创作品,转载请保留出处。作者主页:sysin.org全新推出Windows11全新......
  • 媒体播放器应用程序的解码器如下:使用 DISM 集成 HEVC 视频扩展应用;要使用 DISM 集成 H
    WMPLegacyandtheMediaPlayer Appare 100%differentsoftware'sandshareexactlyzerocodebetweenthem.MycodecsoftwaresupportsWMPbecauseitcanuseDirectshowcodecsbuttheMediaPlayerAppusesMediaFoundationCodecswhichare distributed......
  • 【2024潇湘夜雨】WIN 11_IoT_Ent_LTSC_2024_24H2.26100.2161软件选装纯净特别版10.26
    【系统简介】=============================================================1.本次更新母盘来自WIN11_IoT_Ent_LTSC_2024_24H2.26100.2161.进桌面后稍等片刻,等待后续部分优化完成。2.全程离线精简、无人值守调用优化处理制作。部分优化适配系统可能要重启几次,即使显示适配失败也......
  • ubuntu 22.04及以上版本永久关闭SWAP方法
    背景:同事说ubuntu22.04通过常规的方式方法关闭swap,开机后还是会自动挂在swap因为之前写过在ubuntu上ansible自动化部署kubernetes就发现了这个问题,当时就在playbook文件里面写了关闭方法,这里在拿出来细分下通过测试是完全支持ubuntu20版本及以上版本永久关闭swap查看系统......
  • centos7.6离线安装Redmine 5.1.1教程
    centos7.6离线安装Redmine5.1.1教程网络上离线安装redmine资料少,有帮助大家请点赞总体思路:先申请一台云主机(天翼云、阿里、腾讯),安装相同版本的centos操作系统,有两个好处:一是用云主机下载Redmine的RPM包以及所有依赖,再复制到离线centos服务器上;二是可以先在云主机上安......
  • 两句话讲清楚离线安装screen
    两句话讲清楚离线安装screen目录两句话讲清楚离线安装screen写在前面解决方案问题screen使用写在前面摘要众所周知,如果linux系统没有安装screen,那么服务器将会很难搞(我编的)。即使关闭了连接服务器的窗口,也想随时重新回到工作区,而不是只能通过本地保存的日志,或者kill进程。......
  • centos7离线安装nvidia-docker2(附安装包)
    说实话全网没有找到一个安装包,如果你刚好从我这里找到,给我留言我就能看到发给你。或者你按照我的教程使用yum离线下载也可以在自己的docker中拉取一个centos:7镜像,创建一个容器在容器中安装docker-ce#设置docker镜像源yum-config-manager\--add-repo\https://mirro......
  • Visual Studio 离线部署
    前言最近在在做一个移植项目,把代码从Unix移植到Windows上面,考虑两个编译链:(1)MINGW64-CRT(交叉编译)(2)VisualStudio。而VisualStudio社区版需保持在线使用,不然试用将存在过期时间。由于办公环境在内网内,所以考虑使用VisualStudio的生成工具配合CMake管理......
  • 开源模型应用落地-Qwen2.5-7B-Instruct与vllm实现离线推理-CPU版本
    一、前言   离线推理能够在模型训练完成后,特别是在处理大规模数据时,利用预先准备好的输入数据进行批量推理,从而显著提高计算效率和响应速度。通过离线推理,可以在不依赖实时计算的情况下,快速生成预测结果,从而优化决策流程和提升用户体验。此外,离线推理还可以降低云计算成本......