JavaScriptを有効にしてください

Google Colabでudockerを使ってDockerコンテナを実行する方法

 ·  ☕ 3 分で読めます

Google Colabでudockerを使ってDockerコンテナを実行する方法

Google Colabでudockerを使ってDockerコンテナを実行する方法

udocker環境設定

以下のGithubのREADMEの通りにGoogle Colab上で環境を作成します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright 2024 Drengskapur
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @title {display-mode:"form"}
# @markdown <br/><br/><center><img src="https://cdn.jsdelivr.net/gh/drengskapur/docker-in-colab/assets/docker.svg" height="150"><img src="https://cdn.jsdelivr.net/gh/drengskapur/docker-in-colab/assets/colab.svg" height="150"></center><br/>
# @markdown <center><h1>Docker in Colab</h1></center><center>github.com/drengskapur/docker-in-colab<br/><br/><br/><b>udocker("run hello-world")</b></center><br/>
def udocker_init():
    import os
    if not os.path.exists("/home/user"):
        !pip install udocker > /dev/null
        !udocker --allow-root install > /dev/null
        !useradd -m user > /dev/null
    print(f'Docker-in-Colab 1.1.0\n')
    print(f'Usage:     udocker("--help")')
    print(f'Examples:  https://github.com/indigo-dc/udocker?tab=readme-ov-file#examples')

    def execute(command: str):
        user_prompt = "\033[1;32muser@pc\033[0m"
        print(f"{user_prompt}$ udocker {command}")
        !su - user -c "udocker $command"

    return execute

udocker = udocker_init()

これでDocker環境ができました。

hello-world

コンテナを実行してみます。

1
udocker("run hello-world")

以下が表示されたら成功になります。

1
Hello from Docker!

コマンド紹介

ヘルプ

1
udocker("--help")

コンテナリスト

1
udocker("ps --help")

コンテナの実行

1
udocker("run --help")

udockerの一般的な制限事項

udockerは、ルート権限を必要とする操作を行うことはできません。以下は、udockerで行うことができない操作の例です。

  • ホストが保護するデバイスやファイルへのアクセス
  • TCP/IPの特権ポート(1024未満の範囲)でのリスニング
  • ファイルシステムのマウント
  • suコマンドの使用不可
  • システム時間の変更
  • ルーティングテーブル、ファイアウォールルール、ネットワークインターフェースの変更

これらの権限を必要とするコンテナの場合、代わりにDockerを使用する必要があります。

udockerはコンテナの作成を目的としていません。コンテナの作成はDockerおよびDockerfileを使用して行う方が適しています。

udockerはDockerのすべての機能を提供するわけではなく、Dockerの代替として意図されていません。

udockerは主に、ユーザースペースでのコンテナ実行環境を提供することを目的としています。udockerは特に、Dockerコンテナにカプセル化されたユーザーアプリケーションの実行に適しています。

PRootエンジンを使用してのデバッグやstraceの使用は、デバッガーとPRootが同じトレースメカニズムを使用するため、機能しません。

まとめ

Google Colab 環境上でudockerを使用すればコンテナ自体は使えるようです。
ただし、それでも本来のDockerのような動作をすることはできなかったり、compose もサポートされていないため、求めているものに対しては不十分かもしれません。

共有

こぴぺたん
著者
こぴぺたん
Copy & Paste Engineer