Dockerサブスクリプションに含まれる docker debug 機能

Docker Hub にはサブスクリプションが存在します。個人でプランに加入するケースは少ないと思いますが、会社で使うために組織で加入しているケースがあるかと思います。

この Docker Core のサブスクリプションに最近、 docker debug が含まれました。この機能は Docker Desktop 4.33 に含まれるようです。

www.publickey1.jp

docs.docker.com

この機能を最近使ったところすごく便利だったので紹介します。

docker コンテナにデバックに必要なツールをインストールしつつ入れる

この機能を簡単に説明するとこれです。 使い方は非常に簡単で docker debug のあとにコンテナ名を指定するだけです。

docker debug [container name]

Dockerfile を作る際に成果物が正しく含まれているか、不要なものが入ってしまっていないかを確認することがよくあります。その際に、docker exec -it [container name] bash などとして入ることがあると思います。しかし、成果物のコンテナは軽量化のイメージを使っておりそもそも bash が入っていなかったり内部でファイルの内容を確認したいときに vim が入っていないので Dockerfile を一部書き換えて vim をインストールするみたいな経験もあるのではいでしょうか?

このような課題を docker debug はすべて解決できます。このコマンドを使用するとbashがコンテナに入っていなくてもコンテナ内に入ることができ、さらに vimcurl などのツールも自動でインストールしてくれるようです。

検証してみる

実際にどのようなツール群が入るのか試してみようと思います。使用するイメージは hello-world を使用します。どうやらこのイメージは bash も sh も入っていないようです

docker pull hello-world

docker exec -it hello-world bash
# Error response from daemon: No such container: hello-world

docker exec -it hello-world sh
# Error response from daemon: No such container: hello-world

これに対して docker debug を使ってみます。

docker debug hello-world
         ▄
     ▄ ▄ ▄  ▀▄▀
   ▄ ▄ ▄ ▄ ▄▇▀  █▀▄ █▀█ █▀▀ █▄▀ █▀▀ █▀█
  ▀████████▀    █▄▀ █▄█ █▄▄ █ █ ██▄ █▀▄
   ▀█████▀                        DEBUG

Builtin commands:
- install [tool1] [tool2] ...    Add Nix packages from: https://search.nixos.org/packages
- uninstall [tool1] [tool2] ...  Uninstall NixOS package(s).
- entrypoint                     Print/lint/run the entrypoint.
- builtins                       Show builtin commands.

Checks:
✓ distro:            unknown/scratch
✓ entrypoint linter: no errors (run 'entrypoint' for details)

Note: This is a sandbox shell. All changes will not affect the actual image.
                                                                                                                                                                                                     Version: 0.0.34
root@ee301c921b8a / [hello-world:latest]
docker > ls
dev  etc  hello  nix  proc  sys
root@ee301c921b8a / [hello-world:latest]
docker >

なんと、1発で入れます。NixOSを追加しているみたい?ですね
さて、色々検証してみようと思います。

ちなみに、上のコピペでは分かりづらいですが、実際のターミナルだといい感じのAAが出てきます。

$SHELL

docker > which $SHELL
/nix/var/nix/profiles/default/bin/zsh

なんと zsh が入っている!ドキュメントによると 起動時に --shell で選択でき、デフォルトは auto らしいです。自分は通常 zsh を使っているのでそれと同じshellを入れてくれるのだろうか?*1

ツール・言語・その他

vimcurlnano はドキュメントに書いてあるとおり入っていそうですね。Python や Perl はなさそうでした。それはそうという感じ

root@ee301c921b8a / [hello-world:latest]
docker > vim --version
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Jan 01 1980 00:00:00)
Included patches: 1-2116
Compiled by nixbld
...

root@ee301c921b8a / [hello-world:latest]
docker > curl --version
curl 8.4.0 (aarch64-unknown-linux-gnu) libcurl/8.4.0 OpenSSL/3.0.13 zlib/1.3 brotli/1.1.0 zstd/1.5.5 libidn2/2.3.4 libssh2/1.11.0 nghttp2/1.57.0
Release-Date: 2023-10-11
Protocols: dict file ftp ftps gopher gophers http https imap imaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM SPNEGO SSL threadsafe TLS-SRP UnixSockets zstd

root@ee301c921b8a / [hello-world:latest]
docker > nano --version
 GNU nano, version 7.2
 (C) 2023 the Free Software Foundation and various contributors
 Compiled options: --enable-utf8

root@ee301c921b8a / [hello-world:latest]
docker > git --version
git version 2.42.0

root@ee301c921b8a / [hello-world:latest]
docker > python --version
zsh: command not found: python

root@ee301c921b8a / [hello-world:latest]
docker > perl --version
zsh: command not found: perl

どうやら、バイナリは /nix/var/nix/profiles/default/bin に入っているのでこちらを見たほうがはやそうです。NixOSを追加するらしいので Nix にあればあるという感じなんでしょうか。

root@ee301c921b8a /nix/var/nix/profiles/default/bin [hello-world:latest]
docker > ls /nix/var/nix/profiles/default/bin
'['           chcpu        dir           fish_key_reader        grep          ld.bfd      lslocks       nice                  pcprofiledump   rev            sha512sum          swaplabel     uname26      whatis
 accessdb     chfn         dircolors     flock                  groups        ld.gold     lslogins      nix                   pgrep           rfkill         shred              swapoff       uncompress   whereis
 addpart      chgrp        dirname       fmt                    gunzip        ld.so       lsmem         nix-build             pidof           rm             shuf               swapon        unexpand     which
 addr2line    chmem        dmesg         fold                   gzexe         ldattach    lsns          nix-channel           ping            rmdir          size               switch_root   uniq         who
 agetty       chmod        du            free                   gzip          ldconfig    makedb        nix-collect-garbage   pinky           rnano          slabtop            sync          unlink       whoami
 apropos      choom        dwp           fsck                   hardlink      ldd         man           nix-copy-closure      pipesz          rtcwake        sleep              sysctl        unshare      wipefs
 ar           chown        echo          fsck.cramfs            head          less        man-recode    nix-daemon            pivot_root      runcon         sln                tac           uptime       write
 arping       chroot       egrep         fsck.minix             hexdump       lessecho    mandb         nix-env               pkill           runuser        sort               tail          users        xargs
 as           chrt         eject         fsfreeze               hostid        lesskey     manpath       nix-hash              pldd            rview          sotruss            tar           utmpdump     xtrace
 b2sum        chsh         elfedit       fstrim                 hostname      lexgrog     mcookie       nix-instantiate       pmap            rvim           split              taskset       uuidd        xxd
 base32       cksum        env           gcore                  htop          link        md5sum        nix-prefetch-url      pr              scalar         sprof              tee           uuidgen      yes
 base64       clockdiff    ex            gdb                    hwclock       linux32     mesg          nix-shell             printenv        scp            ssh                test          uuidparse    zcat
 basename     col          expand        gdb-add-index          iconv         linux64     mkdir         nix-store             printf          script         ssh-add            timeout       vdir         zcmp
 basenc       colcrt       expr          gdbserver              iconvconfig   ln          mkfifo        nl                    prlimit         scriptlive     ssh-agent          tload         vi           zdiff
 bash         colrm        factor        gencat                 id            locale      mkfs          nm                    ps              scriptreplay   ssh-copy-id        top           view         zdump
 bashbug      column       fadvise       getconf                install       localedef   mkfs.bfs      nohup                 ptx             seq            ssh-keygen         touch         vim          zegrep
 blkdiscard   comm         fallocate     getopt                 ionice        logger      mkfs.cramfs   nologin               pwait           setarch        ssh-keyscan        tr            vimdiff      zfgrep
 blkid        coreutils    false         git                    ipcmk         login       mkfs.minix    nproc                 pwd             setpriv        sshd               tracepath     vimtutor     zforce
 blkpr        cp           fdisk         git-credential-netrc   ipcrm         logname     mknod         nscd                  pwdx            setsid         stat               true          vmstat       zgrep
 blkzone      csplit       fgrep         git-cvsserver          ipcs          look        mkswap        nsenter               ranlib          setterm        stdbuf             truncate      w            zic
 blockdev     ctrlaltdel   file          git-http-backend       irqtop        losetup     mktemp        numfmt                readelf         sfdisk         strace             tsort         waitpid      zless
 c++filt      curl         fincore       git-jump               isosize       ls          more          objcopy               readlink        sftp           strace-log-merge   tty           wall         zmore
 cal          cut          find          git-receive-pack       join          lsblk       mount         objdump               readprofile     sh             strings            tzselect      watch        znew
 cat          date         findfs        git-shell              kill          lscpu       mountpoint    od                    realpath        sha1sum        strip              uclampset     wc           zramctl
 catman       dd           findmnt       git-upload-archive     last          lsfd        mv            partx                 rename          sha224sum      stty               ul            wdctl        zsh
 cfdisk       delpart      fish          git-upload-pack        lastb         lsipc       namei         paste                 renice          sha256sum      sulogin            umount        webfsd
 chcon        df           fish_indent   gprof                  ld            lsirq       nano          pathchk               resizepart      sha384sum      sum                uname         wget

まとめ

ということで docker debug 機能について軽く調べてみました。仕事でめちゃくちゃ Dockerfile を叩くという人にとっては神機能ですが、一度 Dockerfile を使ったら別に必要ない感じにはなりそうですね。自分は開発合宿で Dockerfile を作る機会があり、時間がない中で確認したかったので非常に重宝させていただきました。

*1:要検証