Skip to main content
ドキュメントへの更新が頻繁に発行されており、このページの翻訳はまだ行われている場合があります。 最新の情報については、「英語のドキュメント」を参照してください。

codespace でのコンテナーのリビルド

コンテナーをリビルドして、作業中の codespace に構成変更を適用できます。 場合によっては、完全なリビルドを実行する必要があります。

コンテナーのリビルドについて

codespace で作業する場合、開発環境は仮想マシン上で実行される Docker コンテナーです。 codespace 内から開発コンテナー構成に変更を加え、それらの変更を現在の codespace に適用する場合は、コンテナーをリビルドする必要があります。

既定では、コンテナーをリビルドすると、GitHub Codespaces によって、コンテナーの以前のビルドからキャッシュされたイメージを再利用することでビルド プロセスが高速化されます。 これは通常、次の理由により、開発コンテナー構成に対する変更を実装する最も簡単な方法です。

  • GitHub Codespaces では、コンテナー レジストリから再度プルするのではなく、キャッシュ内のイメージを再利用できます。
  • 開発コンテナーの機能や Dockerfile 命令など、コンテナーの構築方法を定義する開発コンテナー構成の一部が、キャッシュ内のイメージ レイヤーに既に実装されている可能性があるため、これらのプロセスが再度実行されるのを待つ必要はありません。 (ただし、onCreateCommand など、コンテナーの構築後に実行される構成内のコマンドは、再度実行されます。)

場合によっては、コンテナーの完全なリビルドを実行することが必要になる場合があります。 完全なリビルドにより、GitHub Codespaces はキャッシュからすべての Docker コンテナー、イメージ、ボリュームをクリーンアップし、新しくプルされたイメージを使用してコンテナーを再構築します。 構成で定義されているすべてのセットアップが再度実行され、新しいイメージ レイヤーが生成されます。 次のような状況で、キャッシュされたイメージを使用してコンテナーを何度もリビルドした後、完全なリビルドを実行できます。

  • 構成で定義されているセットアップがキャッシュされたイメージに依存しないようにし、構成に基づいて新しい codespace が作成されるときに必要に応じて実行されるようにしたい。 たとえば、依存関係が最後に codespace にプルされてから、基本イメージから削除されている可能性があります。
  • たとえば、ディスク領域が不足している場合や、ストレージ料金を最小限に抑える場合など、キャッシュで使用されるディスク領域を解放したい。 基本イメージを複数回変更した場合、構成に対して多数の反復的な変更を行った場合、または Docker Compose を使って複数のコンテナーを実行している場合は、イメージ キャッシュで大量のディスク領域が使用されている可能性があります。

コンテナーのリビルド

コンテナーは VS Code の Web クライアントまたはデスクトップ アプリケーションの codespace 内でリビルドできます。GitHub CLI を使用することもできます。

VS Code Web クライアントまたはデスクトップ アプリケーションで開発コンテナーをリビルドする

  1. Shift+Command+P (Mac) または Ctrl+Shift+P (Windows/Linux) を使用して、VS Code Command Palette にアクセスします。

  2. 「リビルド」と入力し始め、 [Codespaces: コンテナーのリビルド] または [Codespaces: コンテナーの完全なリビルド] を選択します。

    コマンド パレットの [Codespaces: コンテナーの完全リビルド] オプションのスクリーンショット。

  3. 開発コンテナー構成を変更してコンテナー エラーが発生した場合、codespace は回復モードで動作し、エラー メッセージが表示されます。

    Screenshot of a message saying that the codespace is running in recovery mode. Below the message are buttons labeled "Cancel" and "View creation log."

    • To diagnose the error by reviewing the creation logs, click View creation log.
    • To fix the errors identified in the logs, update your devcontainer.json file.
    • To apply the changes, rebuild your container.

Using GitHub CLI to rebuild a dev container

If you've changed a dev container configuration outside of VS Code (for example, on GitHub.com or in a JetBrains IDE), you can use GitHub CLI to rebuild the dev container for an existing codespace.

  1. In a terminal, enter the following command.

    gh codespace rebuild
    

    Your codespaces are listed.

  2. Use the arrow keys on your keyboard to highlight the required codespace, then press Enter.

GitHub CLI を使用して完全なリビルドを実行するには、gh codespace rebuild --full コマンドを使用します。

リビルドでのデータの保持

When you create a codespace, your repository is cloned into the /workspaces directory in your codespace. This is a persistent directory that is mounted into the container. Any changes you make inside this directory, including editing, adding, or deleting files, are preserved when you stop and start the codespace, and when you rebuild the container in the codespace.

Outside the /workspaces directory, your codespace contains a Linux directory structure that varies depending on the image used to build your codespace. You can add files or make changes to files outside the /workspaces directory: for example, you can install new programs, or you can set up your shell configuration in a file such as ~/.bashrc. As a non-root user, you may not automatically have write access to certain directories, but most images allow root access to these directories with the sudo command.

Outside /workspaces, with the exception of the /tmp directory, the directories in a codespace are tied to the lifecycle of the container. This means any changes you make are preserved when you stop and start your codespace, but are not preserved when you rebuild of the container.

リビルドで /workspaces ディレクトリの外部にファイルを保持する場合は、コンテナー内の目的の場所に、持続させるディレクトリへのシンボリック リンクを作成できます。 たとえば、/workspaces/.devcontainer ディレクトリ内に、再構築先でも持続される config ディレクトリを作成できます。 その後、config ディレクトリとその内容を devcontainer.json ファイルの postCreateCommand としてシンボリック リンクすることができます。

{
    "image": "mcr.microsoft.com/vscode/devcontainers/base:alpine",
    "postCreateCommand": ".devcontainer/postCreate.sh"
}

以下の例の postCreate.sh ファイルでは、config ディレクトリの内容がホーム ディレクトリにシンボリック リンクされています。

#!/bin/bash
ln -sf $PWD/.devcontainer/config $HOME/config && set +x

参考資料