はじめに
本番用途じゃないんだけど、ちょっとだけ動作確認したい。でもその環境でDockerを動作させたい。とかいうとき。
わざわざVM建てる手間が惜しいので、LXCコンテナでDockerをネスト利用する方法。
LXCコンテナを作成する際に、以下のオプションを有効にしておけばOK
- ☑非特権コンテナ
- ☑ネスト
CLIでネスト対応コンテナを作成する
のまえに、CTテンプレートをダウンロードしておく。
「データセンター」→「作成したノード名」→「local」→「CTテンプレート」から「テンプレート」をクリックすると、以下のようなダイアログが出るので、必要なものを選択して「ダウンロード」をクリック
すると、実ファイルは /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 \
このCTテンプレートはSSHサービスが動作していないので pct enter 999
でコンソールログインしてDockerインストールする
(コンテナ内で)Dockerインストール
# curlが導入されていないのでインストールする
apt install -y curl
# いつもの手順
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
動作チェック
(コンテナ内で)hello-worldで動作確認
$ 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/
AplineLinuxを単騎で立ててちょっとシェルでログインしたいなー、とか、ちょっとツールの動作確認したいなー、とかのとき。
(コンテナ内で)AlpineLinuxにログイン
$ 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