このページで解説している内容は、以下の YouTube 動画の解説で見ることができます。

【Docker入門】Dockerで簡単にUbuntuコンテナをGUIで接続する方法(Git活用)

Dockerで簡単にUbuntu24.04LTSコンテナにGUIで接続する方法を紹介します。

この方法は、あらかじめGitをインストールしておく必要があります。

Gitを活用して、簡単にUbuntu24.04LTSコンテナにGUIで接続して操作する方法を紹介します。

 GitHub上に「Webtop」というソフトウェアが公開されています。Webtopでは、デスクトップ環境が構築されており、Dockerコンテナ内のVNCサーバーに、Webブラウザでアクセスすることで、UbuntuコンテナをGUIを操作することができます。

URL:https://github.com/karaage0703/docker-webtop

 karaageさんが、提供している「Webtop」というソフトウェアを活用することで、超簡単にUbuntuの最新バージョンのコンテナをGUIで利用できるようになります。

不要なDockerオブジェクトの削除

まず、不要なDockerオブジェクトをすべて削除しておきます。

以下のコマンドを実行します。

・「docker system prune -a」コマンドを実行します。

Are you sure you want to continue? [y/N] の質問には「y」と入力します。

PS C:\Users\joeac> docker system prune -a
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N] y
Deleted Containers:
(略)

・「docker system prune -a」コマンドで削除されなかったDockerオブジェクトは、個別に削除しておきます。

環境によって削除されずに残っているDockerオブジェクトは異なります。

ディレクトリの移動と作成

Gitでダウンロードするファイルを保存するためのディレクトリを作成します。

まず、作業ディレクトリに移動しておきます。

・「cd desktop/docker」コマンドを実行します。

PS C:\Users\joeac> cd desktop/docker
PS C:\Users\joeac\Desktop\docker>

 「gui-ubuntu2」ディレクトリを作成して、作成したディレクトリに移動します。次のコマンドを実行します。

  • 「mkdir gui-ubuntu2」コマンド
  • 「cd gui-ubuntu2」コマンド
PS C:\Users\joeac\Desktop\docker> mkdir gui-ubuntu2

    Directory: C:\Users\joeac\Desktop\docker

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2024/05/04     0:16                gui-ubuntu2

PS C:\Users\joeac\Desktop\docker> cd gui-ubuntu2
PS C:\Users\joeac\Desktop\docker\gui-ubuntu2>

リポジトリのダウンロード

gitコマンドを使って、リポジトリをダウンロードします。

・「git clone https://github.com/karaage0703/docker-webtop」コマンドを実行します。

PS C:\Users\joeac\Desktop\docker\gui-ubuntu2> git clone https://github.com/karaage0703/docker-webtop
Cloning into 'docker-webtop'...

「docker-webtop」というディレクトリにコンテナに必要なファイルがダウンロードされます。

コンテナの作成と実行

ディレクトリを移動します。

・「cd docker-webtop」コマンドを実行します。

PS C:\Users\joeac\Desktop\docker\gui-ubuntu2> cd docker-webtop

・「docker compose up webtop」コマンドを実行します。

このコマンド docker compose up webtop は、Docker Composeを使用して特定のサービス(webtop)を起動するためのものです。

コマンド意味
docker-compose複数のコンテナを一括管理するためのツール。
upコンテナを起動するためのコマンド。Composeファイルに記述された構成に基づいて、指定されたサービスを起動する。
webtop起動するサービスの名前。Composeファイル内で定義されたサービス名。
コマンドの意味

イメージのビルドに、少し時間を要します。

PS C:\Users\joeac\Desktop\docker\gui-ubuntu2\docker-webtop> docker compose up webtop
time="2024-05-04T01:44:43+09:00" level=warning msg="C:\\Users\\joeac\\Desktop\\docker\\gui-ubuntu2\\docker-webtop\\docker-compose.yaml: `version` is obsolete"
[+] Building 0.0s (0/0)  docker:default
2024/05/04 01:44:43 http2: server: error reading preface from client //./pipe/[+] Building 350.4s (8/8) FINISHED                             docker:default
 => [webtop internal] load build definition from Dockerfile              0.0s
 => => transferring dockerfile: 250B                                     0.0s
 => [webtop internal] load metadata for lscr.io/linuxserver/webtop:ubun  3.0s
(省略)
 => => sha256:b030d9d176dd259a398eb979c66dc7bce9b1cc5d1 3.04kB / 3.04kB  0.0s
 => => sha256:6868199178568135ae6fb5e786c18e6cac77ca8 18.32kB / 18.32kB  0.0s
 => => sha256:7369fca29ebaf59b089019205ca64ffea716fec 53.14MB / 53.14MB  3.7s
               0.0s
[+] Running 1/2
 ✔ Network docker-webtop_default  Cre...                                 0.0s
 - Container webtop               Created                                0.1s
Attaching to webtop
webtop  | [migrations] started
webtop  | [migrations] no migrations found
webtop  | ───────────────────────────────────────
webtop  |
webtop  |       ██╗     ███████╗██╗ ██████╗
webtop  |       ██║     ██╔════╝██║██╔═══██╗
webtop  |       ██║     ███████╗██║██║   ██║
webtop  |       ██║     ╚════██║██║██║   ██║
webtop  |       ███████╗███████║██║╚██████╔╝
webtop  |       ╚══════╝╚══════╝╚═╝ ╚═════╝
webtop  |
webtop  |    Brought to you by linuxserver.io
webtop  | ───────────────────────────────────────
(省略)
webtop  |  2024-05-03 16:51:53,419 [PRIO] Connections: accepted: @172.24.0.1_1714755113.412089::websocketw Enable Watch

v View in Docker Desktop   w Enable Watch

 これで、Ubuntuコンテナ内でVNCサーバーが実行され、ホストOSのポート3000にマッピングされます。Webブラウザから「http://localhost:3000」にアクセスすると、Ubuntuコンテナのデスクトップ環境にアクセスできます。

UbuntuへのGUI接続

・WebブラウザのURLに「localhost:3000/」と入力します。

UbuntuをGUIで操作できるようになります。

ターミナルへの起動

XTermを起動します。

Ubuntuのバージョンの確認

Ubuntuのバージョンを確認します。

・「cat /etc/os-release」のコマンド実行します。

ベースイメージが、Ubuntu24.04 LTSであることが確認できます。

最新のLTSバージョンです。学習環境として、思いっきり活用できるかと思います。