Closed1

colaboratory上でdockerを動かす"docker-in-colab"を試す

kun432kun432

こんなのを見つけた

https://github.com/drengskapur/docker-in-colab

早速試してみる。READMEにあるコードをコピペして実行するだけ。

udockerというユーザー空間でコンテナを動かすものを使っているらしい。

でコマンドの実行は!とかでコマンド実行するのではなく、pythonコードっぽく書けるみたい。

udocker("run hello-world")

udocker("ps")

udockerコマンドは、dockerコマンドと完全互換ってわけではないみたい。


runのオプションを見てみた

udocker("run --help")
user@pc$ udocker run --help

        run: execute a container
        run [options] <container-id-or-name>
        run [options] <repo/image:tag>
        --rm                       :delete container upon exit
        --workdir=/home/userXX     :working directory set to /home/userXX
        --user=userXX              :run as userXX
        --user=root                :run as root
        --volume=/data:/mnt        :mount host directory /data in /mnt
        --novol=/proc              :remove /proc from list of volumes to mount
        --env="MYTAG=xxx"          :set environment variable
        --env-file=<file>          :read environment variables from file
        --hostauth                 :get user account and group from host
        --containerauth            :use container passwd and group directly
        --nosysdirs                :do not bind the host /proc /sys /run /dev
        --nometa                   :ignore container metadata
        --dri                      :bind directories relevant for dri graphics
        --hostenv                  :pass the host environment to the container
        --cpuset-cpus=<1,2,3-4>    :CPUs in which to allow execution
        --name=<container-name>    :set or change the name of the container
        --bindhome                 :bind the home directory into the container
        --kernel=<kernel-id>       :simulate this Linux kernel version
        --device=/dev/xxx          :pass device to container (R1 mode only)
        --location=<container-dir> :use container outside the repository
        --nobanner                 :don't print a startup banner
        --entrypoint               :override the container metadata entrypoint
        --platform=os/arch         :pull image for OS and architecture
        --pull=<when>              :when to pull (missing|never|always)

        Only available in Rn execution modes:
        --device=/dev/xxx          :pass device to container (R1 mode only)

        Only available in Pn execution modes:
        --publish=<hport:cport>    :map container TCP/IP <cport> into <hport>
        --publish-all              :bind and connect to random ports

        run <container-id-or-name> executes an existing container, previously
        created from an image by using: create <repo/image:tag>

        run <repo/image:tag> always creates a new container from the image
        if needed the image is pulled. This is slow and may waste storage.

普通にポートの紐づけもできそう。試しにnginx動かしてみる。なお、colaboratoryだと8080ポートはデフォルトで使われているので使えない模様。

udocker("run -p 30080:80 --name nginx nginx")
user@pc$ udocker run -p 30080:80 --name nginx nginx
Info: downloading layer sha256:92b11f67642b62bbb98e7e49169c346b30e20cd3c1c034d31087e46924b9312e
Info: downloading layer sha256:33952c5995320e59a81112f411bfb02e097562a72c12e85828da51132ace47cd
Info: downloading layer sha256:c5cdd1ce752da415a6563d9432e1ee718b2f4ba353ee2bb7c8ce2aa78d5b4ee1
Info: downloading layer sha256:87c3fb37cbf2f763f67f3b270aa0785ca05a2caedac399b4bfeedfd0ccd77d87
Info: downloading layer sha256:035788421403127b57e688a82706198331f06545a955b526f89f2bf53f52b078
Info: downloading layer sha256:39fc875bd2b2e4f867e8e5cc5ad43bd5d6650ddeaf8c28b04f374f7fbca085f3
Info: downloading layer sha256:e78b137be3552e1f36d84cb01c533a23febe4c48f6fcdff5d5b26a45a636053b
Info: downloading layer sha256:8a1e25ce7c4f75e372e9884f8f7b1bedcfe4a7a7d452eb4b0a1c7477c9a90345
Warning: this container exposes TCP/IP ports
 
 ****************************************************************************** 
 *                                                                            * 
 *               STARTING 66fe70ba-f104-3015-bb19-28cbd5abf8bd                * 
 *                                                                            * 
 ****************************************************************************** 
 executing: docker-entrypoint.sh
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2024/03/24 12:36:10 [emerg] 1573#1573: open() "/var/log/nginx/error.log" failed (13: Permission denied)
udocker("ps")

立ち上がっているように見えるけども、

user@pc$ udocker ps
CONTAINER ID                         P M NAMES              IMAGE               
66fe70ba-f104-3015-bb19-28cbd5abf8bd . W ['nginx']          nginx:latest        
!curl localhost:30080
curl: (7) Failed to connect to localhost port 30080 after 0 ms: Connection refused
!ss -tan | grep 30080

何も表示されないので、どうやらポートが開いていない。psでは出てきたんだけどな。

udockerについて調べてみると、

https://qiita.com/percipere/items/73623b7f658c46356c95

udocker run イメージ名で多数のコンテナを利用すると、何やらやたらと起動しているように見える

udocker run イメージ名では、実行した時点でユーザーのディレクトリ上にファイルを展開し、そのディレクトリをルートディレクトリとして見えるように実行環境に介入しているだけなので、実際に仮想マシンが起動しているという意味ではない

らしい。なるほど。

慣れたdockerのようにはいかなさそうなので、一旦ペンディングかな。。。

このスクラップは1ヶ月前にクローズされました