Ubuntu 22.04 安装docker

本文详细介绍了在Ubuntu22.04上安装Docker的步骤,包括删除旧版本、更新资源库、安装证书、设置国内阿里云镜像加速、安装Docker及相关组件,并解决安装完成后可能出现的权限错误和启动失败问题。通过切换iptables到iptables-legacy来解决启动错误。
摘要由CSDN通过智能技术生成

Ubuntu 22.04安装docker

如果之前有安装过旧版本,则通过此命令删除旧版本

$ sudo apt-get remove docker docker-engine docker.io containerd runc
# 更新资源库
$ sudo apt-get update
# 安装证书
$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
# 安装官方GPG key
$ sudo mkdir -p /etc/apt/keyrings
################  这段可以替换为国内阿里云镜像地址 开始
### 将2块 `https://download.docker.com/linux/ubuntu` 替换为 `https://mirrors.aliyun.com/docker-ce/linux/ubuntu`
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 建立docker资源库
$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
################### 这段可以替换为国内阿里云镜像地址 结束
# 再次更新资源库
$ sudo apt-get update
# 开始安装
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 验证 是否安装成功
$ sudo docker -v
Docker version 20.10.17, build 100c701
$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
# 增加当前用户入docker组中
$ sudo groupadd docker
groupadd: group 'docker' already exists
$ sudo gpasswd -a $USER docker
Adding user tester to group docker
$ newgrp docker
# 再次验证
$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete

Windows 10 子系统Ubuntu 22.04 按以上步骤安装完docker后,启动 docker 可能会报错

$ docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
$ sudo service docker start
$ cat /var/log/docker.log
time="2022-10-20T13:46:33.420454500+08:00" level=warning msg="Your kernel does not support cgroup blkio throttle.write_iops_device"
time="2022-10-20T13:46:33.420568500+08:00" level=info msg="Loading containers: start."
time="2022-10-20T13:46:33.442094800+08:00" level=info msg="stopping event stream following graceful shutdown" error="<nil>" module=libcontainerd namespace=moby
time="2022-10-20T13:46:33.442302600+08:00" level=info msg="stopping event stream following graceful shutdown" error="context canceled" module=libcontainerd namespace=plugins.moby
time="2022-10-20T13:46:33.442329000+08:00" level=info msg="stopping healthcheck following graceful shutdown" module=libcontainerd
time="2022-10-20T13:46:34.443028000+08:00" level=warning msg="grpc: addrConn.createTransport failed to connect to {unix:///var/run/docker/containerd/containerd.sock  <nil> 0 <nil>}. Err :connection error: desc = \"transport: Error while dialing dial unix:///var/run/docker/containerd/containerd.sock: timeout\". Reconnecting..." module=grpc
failed to start daemon: Error initializing network controller: error obtaining controller instance: unable to add return rule in DOCKER-ISOLATION-STAGE-1 chain:  (iptables failed: iptables --wait -A DOCKER-ISOLATION-STAGE-1 -j RETURN: iptables v1.8.7 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain DOCKER-ISOLATION-STAGE-1
 (exit status 4))

以上可以通过将 iptables 切回 iptables-legacy

$ sudo update-alternatives --config iptables
There are 2 choices for the alternative iptables (providing /usr/sbin/iptables).

  Selection    Path                       Priority   Status
------------------------------------------------------------
* 0            /usr/sbin/iptables-nft      20        auto mode
  1            /usr/sbin/iptables-legacy   10        manual mode
  2            /usr/sbin/iptables-nft      20        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/sbin/iptables-legacy to provide /usr/sbin/iptables (iptables) in manual mode

$ sudo service docker start
 * Starting Docker: docker                                                                                       [ OK ]
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值