在Proxmox的LXC容器中嵌套使用Docker(NestedDocker)

首先

本番用途じゃないんだけど、ちょっとだけ動作確認したい。でもその環境でDockerを動作させたい。とかいうとき。
わざわざVM建てる手間が惜しいので、LXCコンテナでDockerをネスト利用する方法。

如果创建LXC容器时,确保启用以下选项即可。

    • ☑非特権コンテナ

 

    ☑ネスト
image.png

使用CLI命令创建支持嵌套的容器。

image.png

于是,实际文件会被下载到 /var/lib/vz/template/cache/ 。

使用下载的模板,在下面创建一个LXC容器(这里使用的是ubuntu22.04)。

pct create 999 /var/lib/vz/template/cache/ubuntu-22.04-standard_22.04-1_amd64.tar.zst \
  --hostname lxc-cli \
  --features nesting=1 \
  --unprivileged 1 \
  --ssh-public-keys /root/.ssh/authorized_keys \
  --rootfs local-lvm:8 \
  --cores 4 \
  --memory 4096 \
  --swap 0 \
  --net0 name=eth0,bridge=vmbr0,ip=10.254.10.100/24,gw=10.254.10.254,tag=10 \
  --start 1

# DHCPの場合は以下 
#  --net0 name=eth0,bridge=vmbr0,ip=dhcp,tag=10 \

由于SSH服务未运行,因此使用pct enter 999进行控制台登录并安装Docker的CT模板。

# curlが導入されていないのでインストールする
apt install -y curl

# いつもの手順
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

动作检查。

$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

当我想要独自搭建Alpine Linux并登录到shell来做一些事情,比如验证工具的运行状态时。

$ docker run -it alpine sh

/ # cat /etc/*release*
3.18.4
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.18.4
PRETTY_NAME="Alpine Linux v3.18"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"

/ # apk add bind-tools -q
/ # dig -v
DiG 9.18.19

删除容器

pct shutdown 999 && pct destroy 999 --purge