Introduction

AWS で ECS on EC2 から別のコンテナを起動する、という動きをオンプレミスで模倣するコードを書いているとき、権限回りなどで苦労したのでメモしておく。

How to do?

最初は ChatGPT に /var/run/docker.sock をマウントしろだの、postCreateCommand でユーザに権限を与えろだの的を得ない回答が返ってきて時間を取られた。

最近はそんな苦労は不要で features/docker-outside-of-docker を使ってこの要望を簡単に実現できる。
権限の操作やマウントなど、煩わしい操作を全部肩代わりしてくれる。
※rootless の場合はマウント設定が必要

サポートされているのは Debian/Ubuntu ベースのディストリビューションのみ。
下記は一例だが、features に追加されている ghcr.io/devcontainers/features/docker-outside-of-docker:1 が肝。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name": "docker-outside-of-docker-devcontainer",
"build": {
"dockerfile": "Dockerfile"
},
"runArgs": [
"--name",
"${localEnv:USER}-devcontainer",
"--rm"
],
"shutdownAction": "stopContainer",
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
"version": "latest"
}
}
}

これで接続した DevContainer から

1
2
3
4
5
root@675dc072c6b8:/workspaces/Demo# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
675dc072c6b8 vsc-demo-82e9f309f250a9488ce353d5d99ecb7034a06f1f57ddd7529cb1538fb8461fd8 "/bin/sh -c 'echo Co…" 32 seconds ago Up 32 seconds t-takeuchi-devcontainer
4ad5dbdb2e5e fluent/fluent-bit:5.0.3 "/fluent-bit/bin/flu…" 6 weeks ago Exited (0) 4 weeks ago demo-fluentbit-fluentbit
8b2cdbd08748 demo-fluentbit-app "/app/Demo" 6 weeks ago Exited (0) 6 weeks ago demo-fluentbit-app1

のようにホスト側のコンテナが見える。

ただし、features/docker-outside-of-docker は Ubuntu 26.04 や debian 13 のような一部のコンテナでは下記のようなエラーになってしまう。

1
2
3
30.63 (!) The 'moby' option is not supported on ubuntu 'resolute' because 'moby-cli' and related system packages are not available in that distribution.
30.63 (!) To continue, either set the feature option '"moby": false' or use a different base image (for example: 'debian:bookworm' or 'ubuntu-24.04').
30.63 ERROR: Feature "Docker (docker-outside-of-docker)" (ghcr.io/devcontainers/features/docker-outside-of-docker) failed to install! Look at the documentation at https://github.com/devcontainers/features/tree/main/src/docker-outside-of-docker for help troubleshooting this error.

公式のコード https://github.com/devcontainers/features/blob/0c44debb78548dd9c15258b51efafede9bf53c78/src/docker-outside-of-docker/install.sh#L211 を見ると

1
2
3
4
5
6
# Prevent attempting to install Moby on Debian trixie or Ubuntu resolute (packages not available)
if [ "${USE_MOBY}" = "true" ] && ([ "${VERSION_CODENAME}" = "trixie" ] || [ "${VERSION_CODENAME}" = "resolute" ]); then
err "The 'moby' option is not supported on ${ID} '${VERSION_CODENAME}' because 'moby-cli' and related system packages are not available in that distribution."
err "To continue, either set the feature option '\"moby\": false' or use a different base image (for example: 'debian:bookworm' or 'ubuntu-24.04')."
exit 1
fi

にように、debian の trixie (debina 13)resolute (ubuntu 26.04) での moby 使用はサポート外となっている。
対処方法は書いてある通り簡単で "moby": false を追加するだけでよい。

1
2
3
4
5
6
	"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
"version": "latest",
+ "moby": false
}
}

もしくは ubuntu 24.04 や debian 12 を使えばよい。