如何在 Incus 容器中运行 Docker
Incus 和 Docker 都使用 Linux 内核功能来容器化应用程序。当您需要像传统 VM 一样的系统级容器并提供持久化开发体验时,Incus 是最佳选择。另一方面,Docker 容器是临时的,即本质上是短暂的。当 Docker 容器停止或删除时,在 Docker 容器内创建的所有文件都会丢失,除非您使用卷将它们存储在 Docker 之外的不同目录中。Docker 被创建为一次性应用程序部署系统。Incus 容器通常不作为一次性容器创建,数据在停止时保留在内部。由于 Linux 内核支持嵌套功能,您可以在 Incus 内部运行 Docker。本页介绍如何在 Incus 容器中运行 Docker。
教程详情
使用传统(旧)方法在 Incus 中设置 Docker
在此方法中,您将学习:
步骤 1-使用 Docker 和 btrfs 文件系统创建 Incus 容器
我只使用 BTRFS 文件系统进行了测试。ZFS 不支持 Docker,并且不建议在生产中使用 zfs Docker 存储驱动程序,除非您对 Linux 上的 ZFS 有丰富的经验。因此,最好坚持使用 BTRFS 作为后端驱动程序。使用以下命令列出您的存储驱动程序:
输出:
 +---------+--------+-------------------------------------+---------+---------+|  NAME   | DRIVER |             DESCRIPTION             | USED BY |  STATE  |+---------+--------+-------------------------------------+---------+---------+| default | btrfs  | nixCraft VM & Containers Production | 1       | CREATED |+---------+--------+-------------------------------------+---------+---------+
   复制代码
 
如果存储驱动程序不是 btrfs,请按以下方式创建:
 $ incus storage create docker btrfs$ incus storage list
   复制代码
 
输出:
 +---------+--------+-------------------------------------+---------+---------+|  NAME   | DRIVER |             DESCRIPTION             | USED BY |  STATE  |+---------+--------+-------------------------------------+---------+---------+| default | btrfs  | nixCraft VM & Containers Production | 7       | CREATED |+---------+--------+-------------------------------------+---------+---------+| docker  | btrfs  |                                     | 0       | CREATED |+---------+--------+-------------------------------------+---------+---------+
   复制代码
 
现在是时候创建一个新的 incus 实例并将其命名为"docker-www-1"。以下是基于 Debian 12 的 Incus 实例的命令:
 $ incus launch images:debian/12/cloud docker-www-1
   复制代码
 
想要 Ubuntu Linux 24.04 Incus 实例?尝试:
 $ incus launch images:ubuntu/noble/cloud docker-www-1
   复制代码
 
如何运行 Amazon Linux、Rocky Linux 或 Alpine Linux incus 实例来运行 Docker?这是可能的:
 $ incus launch images:alpine/3.21/cloud docker-www-1
   复制代码
 
或
 $ incus launch images:rockylinux/9/cloud docker-www-1
   复制代码
 
或
 $ incus launch images:amazonlinux/2023 docker-www-1
   复制代码
 使用 AWS/GCP/Azure 实例类型
您可以使用与 AWS 实例相同的大小创建和启动容器,以便在本地开发机器上测试和开发应用程序。例如,t2.xlarge(4 vCPU,16GiB RAM)如下,以在本地开发机器上模拟您的 EC2 基础设施(也支持 GCP 或 Azure 类型):
 $ incus launch images:{distro_name}/{version} {container_name} -t aws:t2.xlarge$ incus launch images:debian/12/cloud docker-www-1 -t aws:t2.xlarge 
   复制代码
 
对于 GCP/GCE(Google Cloud):
 $ incus launch images:{distro_name}/{version} {container_name} -t gce:f1-micro
   复制代码
 
对于 MS-azure 云:
 $ incus launch images:{distro_name}/{version} {container_name} -t azure:A5
   复制代码
 
除了 AWS/GCP/Azure 云提供商的大小,您可以根据需要启动容器并限制其资源。例如,以下是如何将限制设置为两个 vCPU 和 4092 MiB RAM 的命令:
 $ incus launch images:ubuntu/24.04 test \--config limits.cpu=2 \--config limits.memory=4092MiB
   复制代码
 
基于云提供商服务器或自定义规范轻松分配 CPU 和 RAM 限制的能力使 Incus 成为在本地开发机器上测试和开发应用程序的完美工具。这种灵活性有助于节省资金并促进更轻松的功能测试。让我们回到手头的示例,这里我坚持使用 Debian Linux 12 云镜像,以下是验证方法:
输出:
 +--------------+---------+-----------------------+------+-----------+-----------+|     NAME     |  STATE  |         IPV4          | IPV6 |   TYPE    | SNAPSHOTS |+--------------+---------+-----------------------+------+-----------+-----------+| docker-www-1 | RUNNING | 10.147.164.177 (eth0) |      | CONTAINER | 0         |+--------------+---------+-----------------------+------+-----------+-----------+
   复制代码
 
请记住,我默认使用 btrfs 作为存储驱动程序。但是,如果您不使用 btrfs 存储驱动程序,则按以下方式分配。首先,在名为 docker 的存储下创建一个名为 demovol 的新卷:
 $ incus storage volume create docker demovol$ incus storage volume list docker
   复制代码
 
以下输出显示'docker btrfs 存储的 custom/demovol':
 +--------+---------+-------------+--------------+---------+|  TYPE  |  NAME   | DESCRIPTION | CONTENT-TYPE | USED BY |+--------+---------+-------------+--------------+---------+| custom | demovol |             | filesystem   | 1       |+--------+---------+-------------+--------------+---------+
   复制代码
 
按以下方式附加:
 $ incus config device add docker-www-1 docker disk pool=docker source=demovol path=/var/lib/docker
   复制代码
 
换句话说,您有一个需要 btrfs 存储空间的容器。此命令类似于将外部存储附加到您的容器。您告诉 Incus 从名为"docker"的 btrfs 存储中获取名为"demovol"的存储块,并将其在容器内的特定位置/var/lib/docker 处提供给容器("docker-www-1")。按以下方式验证:
 $ incus config show docker-www-1
   复制代码
 
查找'devices'部分:
 architecture: x86_64config:  image.architecture: amd64......devices:  docker:    path: /var/lib/docker    pool: docker    source: demovol    type: diskephemeral: false.....stateful: falsedescription: ""
   复制代码
 
有关更多信息,请参阅如何在 LXD(Linux 容器)页面中添加或挂载目录。
最后,将 security.nesting 设置为 true 和其他属性,这允许在 Incus 容器实例中成功运行 Docker:
 $ incus config set docker-www-1 security.nesting=true \security.syscalls.intercept.mknod=true \security.syscalls.intercept.setxattr=true
   复制代码
 
设置如下:
- security.nesting=true:允许在 Incus 实例中运行 Docker 容器。 
- security.syscalls.intercept.mknod=true:允许 mknod 和 mknodat Linux 系统调用在 Incus 内安全地为 Docker 创建各种特殊文件。 
- security.syscalls.intercept.setxattr=true:允许 setxattr Linux 系统调用在 Incus 内安全地为 Docker 设置文件的扩展属性。 
请注意,Incus 容器无法加载 Linux 内核模块,因此根据您的 Docker 配置,您可能需要主机加载额外的 Linux 内核模块(设备驱动程序)。您可以设置容器所需的内核模块的逗号分隔列表。语法是:
 $ incus config set {incus_container_name} linux.kernel_modules {module}$ incus config set {incus_container_name} linux.kernel_modules {module1,module2,...}
   复制代码
 
您必须重新启动实例,输入:
 $ incus restart docker-www-1
   复制代码
 步骤 2 - 在 Incus 容器中安装 Docker
首先,登录实例:
 $ incus exec docker-www-1 bash
   复制代码
 
或
 $ incus exec docker-www-1 sh
   复制代码
 
现在,您需要根据实例操作系统遵循常规的 Docker 安装说明。对于 Debian Linux 12:
 $ for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done$ sudo apt-get update$ sudo apt-get install ca-certificates curl$ sudo install -m 0755 -d /etc/apt/keyrings$ sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc$ sudo chmod a+r /etc/apt/keyrings/docker.asc$ echo \  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \  $(. /etc/os-release && echo "$VERSION_CODENAME") 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-buildx-plugin docker-compose-plugin
   复制代码
 
您使用的是 Amazon Linux 版本 2 吗?有关更多信息,请参阅如何在 Amazon Linux 2 上安装 Docker。以下是验证 Docker 安装的方法:
输出:
 Docker version 27.4.0, build bde2b89
   复制代码
 步骤 3 - 部署容器进行测试
让我们创建一个 Apache Web 服务器来提供静态网页 index.html:
添加以下内容:
 <!DOCTYPE html><html><head>  <title>nixCraft's - Apache Docker Container Test</title></head><body>  <h1>Hello from Apache Docker Server!</h1>  <p>I'm running inside Incus instance.</p></body></html>
   复制代码
 
接下来创建 Dockerfile:
添加以下配置:
 FROM httpd:latest
# Copy the index.html file to the Apache document rootCOPY index.html /usr/local/apache2/htdocs/
# Expose port 80 to the outside worldEXPOSE 80
   复制代码
 
构建 docker 镜像:
 # docker build -t test-apache-container .
   复制代码
 
以下命令从 test-apache-container 镜像运行容器,并将主机(Incus 实例)上的 TCP 端口 8080 映射到容器中的端口 80:
 # docker run -d -p 8080:80 test-apache-container
   复制代码
 
使用 curl 命令测试:
 $ curl http://localhost:8080/
   复制代码
 
完成后,您可以停止容器。首先,列出所有正在运行的容器:
输出:
 CONTAINER ID   IMAGE                   COMMAND              CREATED          STATUS          PORTS                                     NAMESea67ff7cb032   test-apache-container   "httpd-foreground"   10 minutes ago   Up 10 minutes   0.0.0.0:8080->80/tcp, [::]:8080->80/tcp   bold_fermi
   复制代码
 
您现在可以使用"CONTAINER ID"(如"ea67ff7cb032")或"NAME"(如"bold_fermi")来停止和删除它。例如:
 # docker stop ea67ff7cb032
   复制代码
 
删除容器:
删除镜像:
 # docker rmi test-apache-container
   复制代码
 使用新方法在 Incus 中设置 Docker
最新版本的 Incus 支持直接运行 Docker 容器镜像。您至少需要 Incus 客户端/服务器版本 6.3 或更高版本:
输出:
 Client version: 6.8Server version: 6.8
   复制代码
 
此功能仍然是新的,有时可能无法按预期工作。因此,如前所述,我建议坚持使用传统方法。
首先,设置使用 OCI 镜像的服务器应用程序容器注册表,使用以下命令:
 $ incus remote add docker https://docker.io --protocol=oci$ incus remote
   复制代码
 
现在您可以从其镜像之一启动容器:
 $ incus launch docker:hello-world --ephemeral --console
   复制代码
 
如何使用 OCI Docker 镜像运行 MySQL 服务器?尝试:
 $ incus launch docker:mysql mysql-db \-c environment.MYSQL_DATABASE=db_name_here \-c environment.MYSQL_USER=db_user_name_here \-c environment.MYSQL_PASSWORD=db_password_here \-c environment.MYSQL_RANDOM_ROOT_PASSWORD=mysql_root_password_here
   复制代码
 
验证:
现在就是全部内容。
总结
我更喜欢旧方法,因为自从 LXD 时代以来我一直在使用它,并且我知道如何使用它。新方法更容易,但有一些限制。我仍在探索这些。更多精彩内容 请关注我的个人公众号 公众号(办公 AI 智能小助手)对网络安全、黑客技术感兴趣的朋友可以关注我的安全公众号(网络安全技术点滴分享)
公众号二维码
 
 办公AI智能小助手
公众号二维码
 
 网络安全技术点滴分享
评论