> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 运行 Docker 容器

Qualcomm Linux 支持 Docker 容器。要使用 Docker 容器，请确保 Qualcomm 板级支持包（BSP）已启用虚拟化功能。在您的工作区中启用 `meta-virtualization` 层，以便在 Qualcomm Linux 设备上使用容器。

## **验证 Docker 守护进程的状态**

`qcom-multimedia-image` 已启用 Docker。

对于 `qcom-multimedia-image` 镜像配方，`packagegroup-container` 通过 `meta-qcom-distro/recipes-products/images/qcom-multimedia-image.bb` 被包含。`packagegroup-container` 在 `meta-virtualization` 层的 `recipes-core/packagegroups/packagegroup-container.bb` 中定义。镜像构建并烧写后，Docker 及其所有先决条件和依赖项都将在设备上可用。

要验证 Docker 守护进程的状态，请运行以下命令并验证输出：

> |           **命令**          |          **说明**          |        **输出**       |
> | :-----------------------: | :----------------------: | :-----------------: |
> | `systemctl status docker` |  如果开机后至少运行过一次 Docker 命令  |  `Active`（running）  |
> |                           | 如果设备在开机后未运行过任何 Docker 命令 | `Inactive`（stopped） |

从 Docker 仓库拉取的适用于 `linux/arm64` 平台且经过测试可在设备上成功运行的镜像如下：

> * `busybox`
> * `python`
> * `alpine`
> * `ubuntu`
> * `postgres`
> * `mongo`
> * `nginx`
> * `redis`

在启用 Docker 的情况下，以下用例已通过验证：

* 使用 Docker load 工具从 tar 文件加载 Docker 镜像。
* 从 Docker 文件构建 Docker 镜像。
* 将拉取/构建的 Docker 镜像保存为 `.tar` 文件。
* 加载保存为 `.tar` 文件的 Docker 镜像。
* 运行多个拉取/构建镜像的多个容器实例。
* 验证设备上已加载的 Docker 镜像列表。
* 验证设备上活跃容器和全部容器的列表。
* 使用 Docker inspect 工具检查 Docker 镜像。
* 使用 Docker logs 工具获取容器的日志。
* 使用 Docker stop 和 Docker kill 工具停止和终止容器。
* 从设备中移除容器和 Docker 镜像。

## **从 Docker 访问硬件节点**

在 Docker 容器内运行的应用程序可能需要访问设备上的设备节点和文件（因为设备是 Docker 容器的宿主机）。以下是两种方法：

1. 将相应的设备节点作为选项与 run 命令一起传递给 Docker 来执行此任务：
   ```text theme={null}
   docker run -it --rm --device=<device-1> --device=<device-2> <docker-image-name>
   ```
   传递位于设备上 `/dev` 目录中的设备节点。根据用例，例如在图形场景中，使用 `--device=/dev/kgsl-3d0` 将 `/dev/kgsl-3d0` 传递给 Docker。
   在 Docker 容器内运行的应用程序可能还需要访问设备（目标宿主机）上的存储。要以绑定挂载的方式公开文件和目录，请运行以下命令：
   ```text theme={null}
   docker run -it --rm --mount type=bind,source=<source-path-on-device>,target=<destination-path-inside-container> <docker-image-name>
   ```
   使用以下命令运行 docker 容器，向容器公开多个目录、文件和设备节点：
   ```text theme={null}
   docker run -it --rm --device=<device-1> --device=<device-2> --mount type=bind,source=<source-path-on-device-1>,target=<destination-path-inside-container-1>  --mount type=bind,source=<source-path-on-device-2>,target=<destination-path-inside-container-2> <docker-image-name>
   ```
2. 使用容器设备接口（CDI）创建 CDI JSON/YAML 文件并将其放置在以下位置：
   ```text theme={null}
   /etc/cdi/ (persistent)
   /var/run/cdi/ (dynamic, auto-generated)
   ```
   使用以下命令运行带有 CDI 设备的容器：
   ```text theme={null}
   docker run -it --device <vendor.com/device-kind>=<cdi-device-name> <image> <command>
   ```

## **在 Qualcomm Linux 设备上运行 Docker Compose**

Docker Compose 是一款用于定义和运行多容器应用程序的工具。它通过在一个易于理解的 YAML 配置文件中定义服务、网络和卷，简化了应用程序堆栈的管理。默认情况下，Qualcomm Linux 版本在 `qcom-multimedia-image` 中包含 Docker Compose。

`docker-compose` 软件包在 `meta-qcom-distro` 元数据层中被添加到 `qcom-multimedia-image`。

要了解有关 Docker Compose 的更多信息，请参阅 [Docker Compose 概述](https://docs.docker.com/compose)。

**要在设备上运行 Docker Compose：**

1. 在设备上的可写路径（例如 `/var`，比如 `/var/docker-compose-yaml`）下创建或复制 Docker Compose YAML 文件，以便在设备上与 Docker Compose 一起使用。
2. 确保 Docker Compose YAML 文件中列出的 Docker 镜像在设备上可用。如果不可用，设备应连接到有效的互联网以拉取 docker 镜像。
3. 要为多个 YAML 文件运行 Docker Compose，请运行以下命令：
   ```text theme={null}
   docker compose -f <docker-compose-file-1>.yml -f <docker-compose-file-2>.yml up
   ```
   Docker Compose 会按照 Docker Compose YAML 文件中的配置运行并启动 Docker 容器。
4. Docker Compose 命令返回后，运行 `docker ps -a` 和 `docker logs <container-id>`，以验证 docker 容器是否按预期运行。

## **后续步骤**

* 有关 Kubernetes 的启用，请参阅[使用 Qualcomm Linux 设置 Kubernetes](./set-up-kubernetes-with-qualcomm-linux)。
* 有关虚拟机设置，请参阅[创建辅助虚拟机](./create-a-secondary-virtual-machine)。
* 有关 OTA 更新，请参阅[使用 capsule 和 OSTree 机制更新 Qualcomm Linux 上的固件和操作系统](./update-firmware-and-os-on-qualcomm-linux-using-capsule-and-os-tree-mechanisms)。
