Ubuntu中docker

适应Ubuntu版本如下:
要安装 Docker Engine,您需要以下 Ubuntu 之一的 64 位版本 版本:

  • Ubuntu Noble 24.04 (LTS)
  • Ubuntu Jammy 22.04 (LTS)
  • Ubuntu Focal 20.04 (LTS)
    Docker Engine for Ubuntu 与 x86_64(或 amd64)、armhf、arm64、 S390x 和 PPC64LE (PPC64EL) 架构。

警告

根据现在网络安全状态,使用某些步骤的时候注意安全,建议使用别的办法替换;
该网页内容不承担任何法律责任,如果不符合当地法律,请关闭网页

更新软件包

1
2
sudo apt update
sudo apt upgrade

安装docker

运行以下命令卸载所有冲突的包:

1
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

apt-get可能会报告您没有安装这些包。

更新软件包索引:

1
sudo apt update

安装必要的软件包,允许apt通过HTTPS使用仓库:

1
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y

如果没有就创建 keyrings文件

1
sudo install -m 0755 -d /usr/share/keyrings/

如果遇到错误,请点击如下选项

ubuntu中sudo install -m 0755 -d /usr/share/keyrings/

这个命令是在Ubuntu中创建一个目录并设置权限,但是它有一个错误。正确的命令应该是sudo install -m 0755 -d /usr/share/keyrings/

这个命令的含义是:

  • sudo:以超级用户的身份运行命令。
  • install:是一个在源文件和目标文件之间传输数据的程序,它建立目录并设置权限。
  • -m 0755:设置文件的权限为0755,这意味着文件所有者可以读、写和执行,而组用户和其他用户可以读和执行。
  • -d:表示我们正在创建一个目录。
  • /usr/share/keyrings/:是我们想要创建的目录的路径。

如果你在Ubuntu中运行这个命令,它会创建一个名为keyrings的目录在/etc/apt/下,并设置权限为755。

但是,如果你在Ubuntu中运行这个命令,你可能会遇到错误,因为install命令通常用于复制文件,而不是仅创建目录。在这种情况下,你可以使用mkdir命令来创建目录,并使用chmod命令来设置权限。

例如:

1
2
sudo mkdir /usr/share/keyrings/
sudo chmod 0755 /usr/share/keyrings/

这两个命令分别创建了一个名为keyrings的目录在/etc/apt/下,并设置了权限为755。

将 Docker APT 软件源添加到你的系统

1
2
sudo touch /usr/share/keyrings/docker.asc
sudo chmod a+r /usr/share/keyrings/docker.asc

导入源仓库的 GPG key

1
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /usr/share/keyrings/docker.asc
1
2
sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg |gpg --dearmor -o /usr/share/keyrings/docker.asc
sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

添加Docker 阿里稳定软件源

1
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

更新软件包

1
2
sudo apt update
sudo apt upgrade

docker的安装程序

1
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

使用二进制方式安装

  1. 进入/tmp/
  2. 创建docker目录,进入docker目录
  3. 下载最新docker
    1
    wget -c https://download.docker.com/linux/static/stable/x86_64/docker-27.3.1.tgz
  4. 解压
  • root用户操作:
    1
    tar -zxvf docker-27.3.1.tgz
  • 非root用户操作:
    1
    sudo tar -zxvf docker-27.3.1.tgz
    1
    chown root:root docker/*
  1. 复制二进制文件
    将解压后的二进制文件复制到目录/usr/bin下:
    1
    cp -p docker/* /usr/bin/ 

    说明:参数-p保持原来的属性。

  2. 创建用户组
    执行以下命令创建用户组:
    1
    groupadd docker 
  3. 检查命令是否生效
    执行以下命令查看版本:
    1
    docker version 
    如果可以执行该命令说明docker可以使用了
  4. 配置相关服务文件
    参考文件:docker.socketcontainerd.servicedocker.service
  • 创建文件/usr/lib/systemd/system/docker.socket
    1
    touch /usr/lib/systemd/system/docker.socket
    文件内容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [Unit]
    Description=Docker Socket for the API

    [Socket]
    # If /var/run is not implemented as a symlink to /run, you may need to
    # specify ListenStream=/var/run/docker.sock instead.
    ListenStream=/run/docker.sock
    SocketMode=0660
    SocketUser=root
    SocketGroup=docker

    [Install]
    WantedBy=sockets.target
  • 创建文件/usr/lib/systemd/system/containerd.service
    1
    touch /usr/lib/systemd/system/containerd.service
    文件内容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    # Copyright The containerd Authors.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    # http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.

    [Unit]
    Description=containerd container runtime
    Documentation=https://containerd.io
    After=network.target local-fs.target

    [Service]
    #uncomment to enable the experimental sbservice (sandboxed) version of containerd/cri integration
    #Environment="ENABLE_CRI_SANDBOXES=sandboxed"
    ExecStartPre=-/sbin/modprobe overlay
    ExecStart=/usr/bin/containerd

    Type=notify
    Delegate=yes
    KillMode=process
    Restart=always
    RestartSec=5
    # Having non-zero Limit*s causes performance problems due to accounting overhead
    # in the kernel. We recommend using cgroups to do container-local accounting.
    LimitNPROC=infinity
    LimitCORE=infinity
    LimitNOFILE=infinity
    # Comment TasksMax if your systemd version does not supports it.
    # Only systemd 226 and above support this version.
    TasksMax=infinity
    OOMScoreAdjust=-999

    [Install]
    WantedBy=multi-user.target
  • 创建文件/usr/lib/systemd/system/docker.service
    1
    touch /usr/lib/systemd/system/docker.service
    文件内容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    [Unit]
    Description=Docker Application Container Engine
    Documentation=https://docs.docker.com
    After=network-online.target docker.socket firewalld.service containerd.service time-set.target
    Wants=network-online.target containerd.service
    Requires=docker.socket

    [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/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
    ExecReload=/bin/kill -s HUP $MAINPID
    TimeoutStartSec=0
    RestartSec=2
    Restart=always

    # Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
    # Both the old, and new location are accepted by systemd 229 and up, so using the old location
    # to make them work for either version of systemd.
    StartLimitBurst=3

    # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
    # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
    # this option work for either version of systemd.
    StartLimitInterval=60s

    # Having non-zero Limit*s causes performance problems due to accounting overhead
    # in the kernel. We recommend using cgroups to do container-local 4accounting.
    LimitNPROC=infinity
    LimitCORE=infinity

    # Comment TasksMax if your systemd version does not support it.
    # Only systemd 226 and above support this option.
    TasksMax=infinity

    # 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
    OOMScoreAdjust=-500

    [Install]
    WantedBy=multi-user.target
  1. 启用 dockerd 服务:
  • 启动 dockerd 服务进程。
    1
    systemctl enable docker.service 
  • 查看状态:
    1
    systemctl is-enabled docker.socket docker.service containerd.service 
    分别是:disabled、enabled、disabled
  • 启动:
    1
    systemctl start docker.service 
  • 查看状态:
    1
    systemctl status docker.socket docker.service containerd.service 
  • 一条命令搞定启用和运行:
    1
    systemctl enable --now docker.service 
  1. 检验
  • 查看进程:
    1
    ps aux | grep docker 

安装 Docker Compose

以非 root 用户身份管理 Docker

创建docker群组。

1
sudo groupadd docker

将您的用户添加到docker组中。

1
sudo usermod -aG docker $USER

可以自己创建用户组,然后加入userdocker

  1. 创建一个用户userdocker
1
adduser userdocker
  1. 将您的用户添加到docker组中。
1
sudo usermod -aG docker userdocker

立即生效指令

1
newgrp docker

加速域名

修改文件/etc/docker/daemon.json,参考点击此处,一切以安全为主;

如果没有该路径和该文件,直接创建:
1
sudo vim /etc/docker/daemon.json
  • 在该文件中添加如下代码
1
2
3
4
5
{
"registry-mirrors": [
"https://docker.m.daocloud.io"
]
}
  • IPv6网络需要在文件中添加如下内容
1
2
3
4
5
6
{
"ipv6": true,
"fixed-cidr-v6": "2001:db8:1::/64",
"experimental": true,
"ip6tables": true
}

上面两个文件合并,并保存文件

  • 重新启动 Docker 守护进程以使更改生效。
1
sudo systemctl restart docker

卸载docker

卸载 Docker 时,存储在中的图像、容器、卷和网络/var/lib/docker/不会自动删除。如果您想从全新安装开始,并且希望清理所有现有数据,请阅读 卸载 Docker Engine部分。

删除docker及安装时自动安装的所有包

1
apt-get autoremove docker docker-ce docker-engine  docker.io  containerd runc

卸载 Docker Engine、CLI、containerd 和 Docker Compose 软件包:

1
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

查看docker是否卸载干净

1
dpkg -l | grep docker
1
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P # 删除无用的相关的配置文件

删除没有删除的相关插件

1
apt-get autoremove docker-*

删除docker的相关配置&目录

主机上的映像、容器、卷或自定义配置文件 不会自动删除。要删除所有镜像、容器和卷,请执行以下操作:

1
rm -rf /etc/systemd/system/docker.service.d
1
2
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

确定docker卸载完毕

1
docker --version

Docker应用程序

  1. 查看日志 name 是容器库
1
docker logs --follow name

—END—