Ubuntu 22.04 安装docker

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="" 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   0 }. 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

你可能感兴趣的:(Ubuntu,docker,ubuntu,容器)