Persisting container dataコンテナデータの永続化

Explanation説明

When a container starts, it uses the files and configuration provided by the image. Each container is able to create, modify, and delete files and does so without affecting any other containers. When the container is deleted, these file changes are also deleted.コンテナが起動すると、イメージによって提供されたファイルと設定を使用します。各コンテナはファイルを作成、変更、削除することができ、他のコンテナに影響を与えることなく行います。コンテナが削除されると、これらのファイルの変更も削除されます。

While this ephemeral nature of containers is great, it poses a challenge when you want to persist the data. For example, if you restart a database container, you might not want to start with an empty database. So, how do you persist files?この一時的な性質は素晴らしいですが、データを永続化したいときには課題をもたらします。たとえば、データベースコンテナを再起動する場合、空のデータベースから始めたくないかもしれません。では、ファイルをどのように永続化するのでしょうか?

Container volumesコンテナボリューム

Volumes are a storage mechanism that provide the ability to persist data beyond the lifecycle of an individual container. Think of it like providing a shortcut or symlink from inside the container to outside the container.ボリュームは、個々のコンテナのライフサイクルを超えてデータを永続化する能力を提供するストレージメカニズムです。コンテナの内部から外部へのショートカットやシンボリックリンクを提供するようなものだと考えてください。

As an example, imagine you create a volume named log-data.例えば、log-dataという名前のボリュームを作成したとしましょう。

$ docker volume create log-data

When starting a container with the following command, the volume will be mounted (or attached) into the container at /logs:次のコマンドでコンテナを起動すると、ボリュームは/logsにマウント(またはアタッチ)されます:

$ docker run -d -p 80:80 -v log-data:/logs docker/welcome-to-docker

If the volume log-data doesn't exist, Docker will automatically create it for you.ボリュームlog-dataが存在しない場合、Dockerは自動的にそれを作成します。

When the container runs, all files it writes into the /logs folder will be saved in this volume, outside of the container. If you delete the container and start a new container using the same volume, the files will still be there.コンテナが実行されると、/logsフォルダーに書き込まれたすべてのファイルは、このボリュームに保存され、コンテナの外に保存されます。コンテナを削除して同じボリュームを使用して新しいコンテナを起動しても、ファイルはそのまま残ります。

Sharing files using volumesボリュームを使用したファイルの共有

You can attach the same volume to multiple containers to share files between containers. This might be helpful in scenarios such as log aggregation, data pipelines, or other event-driven applications.同じボリュームを複数のコンテナにアタッチして、コンテナ間でファイルを共有することができます。これは、ログ集約、データパイプライン、またはその他のイベント駆動型アプリケーションなどのシナリオで役立つかもしれません。

Managing volumesボリュームの管理

Volumes have their own lifecycle beyond that of containers and can grow quite large depending on the type of data and applications you’re using. The following commands will be helpful to manage volumes:ボリュームはコンテナのライフサイクルを超えた独自のライフサイクルを持ち、使用しているデータやアプリケーションの種類によってはかなり大きく成長する可能性があります。ボリュームを管理するために役立つコマンドは以下の通りです:

  • docker volume ls - list all volumesdocker volume ls - すべてのボリュームをリスト表示
  • docker volume rm <volume-name-or-id> - remove a volume (only works when the volume is not attached to any containers)docker volume rm <volume-name-or-id> - ボリュームを削除します(ボリュームがコンテナに接続されていない場合のみ機能します)
  • docker volume prune - remove all unused (unattached) volumesdocker volume prune - すべての未使用(未接続)ボリュームを削除します

Try it out試してみる

In this guide, you’ll practice creating and using volumes to persist data created by a Postgres container. When the database runs, it stores files into the /var/lib/postgresql/data directory. By attaching the volume here, you will be able to restart the container multiple times while keeping the data.このガイドでは、Postgresコンテナによって作成されたデータを永続化するためにボリュームを作成し使用する練習をします。データベースが実行されると、/var/lib/postgresql/dataディレクトリにファイルが保存されます。ここにボリュームを接続することで、データを保持しながらコンテナを何度も再起動できるようになります。

Use volumesボリュームを使用する

  1. Download and install Docker Desktop.Docker Desktopをダウンロードしてインストールします。

  2. Start a container using the Postgres image with the following command:Postgresイメージを使用してコンテナを開始するには、次のコマンドを実行します:

    $ docker run --name=db -e POSTGRES_PASSWORD=secret -d -v postgres_data:/var/lib/postgresql/data postgres
    

    This will start the database in the background, configure it with a password, and attach a volume to the directory PostgreSQL will persist the database files.これにより、データベースがバックグラウンドで起動し、パスワードで構成され、PostgreSQLがデータベースファイルを永続化するディレクトリにボリュームが接続されます。

  3. Connect to the database by using the following command:次のコマンドを使用してデータベースに接続します:

    $ docker exec -ti db psql -U postgres
    
  4. In the PostgreSQL command line, run the following to create a database table and insert two records:PostgreSQLコマンドラインで、次のコマンドを実行してデータベーステーブルを作成し、2つのレコードを挿入します:

    CREATE TABLE tasks (
        id SERIAL PRIMARY KEY,
        description VARCHAR(100)
    );
    INSERT INTO tasks (description) VALUES ('Finish work'), ('Have fun');
  5. Verify the data is in the database by running the following in the PostgreSQL command line:PostgreSQLコマンドラインで次のコマンドを実行して、データがデータベースに存在することを確認します:

    SELECT * FROM tasks;

    You should get output that looks like the following:次のような出力が得られるはずです:

     id | description
    ----+-------------
      1 | Finish work
      2 | Have fun
    (2 rows)
  6. Exit out of the PostgreSQL shell by running the following command:次のコマンドを実行してPostgreSQLシェルから退出します:

    \q
    
  7. Stop and remove the database container. Remember that, even though the container has been deleted, the data is persisted in the postgres_data volume.データベースコンテナを停止して削除します。コンテナが削除されても、データはpostgres_dataボリュームに保持されていることを忘れないでください。

    $ docker stop db
    $ docker rm db
    
  8. Start a new container by running the following command, attaching the same volume with the persisted data:次のコマンドを実行して新しいコンテナを起動し、保持されたデータの同じボリュームを接続します:

    $ docker run --name=new-db -d -v postgres_data:/var/lib/postgresql/data postgres 
    

    You might have noticed that the POSTGRES_PASSWORD environment variable has been omitted. That’s because that variable is only used when bootstrapping a new database.POSTGRES_PASSWORD環境変数が省略されていることに気づいたかもしれません。それは、その変数が新しいデータベースをブートストラップする際にのみ使用されるからです。

  9. Verify the database still has the records by running the following command:次のコマンドを実行して、データベースにまだレコードがあることを確認します:

    $ docker exec -ti new-db psql -U postgres -c "SELECT * FROM tasks"
    

View volume contentsボリュームの内容を表示

The Docker Desktop Dashboard provides the ability to view the contents of any volume, as well as the ability to export, import, and clone volumes.Docker Desktop Dashboardは、任意のボリュームの内容を表示する機能に加え、ボリュームのエクスポート、インポート、クローン作成の機能を提供します。

  1. Open the Docker Desktop Dashboard and navigate to the Volumes view. In this view, you should see the postgres_data volume.Docker Desktop Dashboardを開き、ボリュームビューに移動します。このビューでは、postgres_dataボリュームが表示されるはずです。

  2. Select the postgres_data volume’s name.postgres_dataボリュームの名前を選択します。

  3. The Data tab shows the contents of the volume and provides the ability to navigate the files. Double-clicking on a file will let you see the contents and make changes.データタブはボリュームの内容を表示し、ファイルをナビゲートする機能を提供します。ファイルをダブルクリックすると、その内容を表示して変更できます。

  4. Right-click on any file to save it or delete it.任意のファイルを右クリックして保存または削除します。

Remove volumesボリュームを削除

Before removing a volume, it must not be attached to any containers. If you haven’t removed the previous container, do so with the following command (the -f will stop the container first and then remove it):ボリュームを削除する前に、それがどのコンテナにも接続されていないことを確認する必要があります。前のコンテナを削除していない場合は、次のコマンドを使用して削除してください(-fは最初にコンテナを停止し、その後削除します):

$ docker rm -f new-db

There are a few methods to remove volumes, including the following:ボリュームを削除する方法はいくつかあります。以下を含みます:

  • Select the Delete Volume option on a volume in the Docker Desktop Dashboard.Docker Desktop Dashboardでボリュームのボリュームを削除オプションを選択します。

  • Use the docker volume rm command:docker volume rmコマンドを使用します:

    $ docker volume rm postgres_data
    
  • Use the docker volume prune command to remove all unused volumes:docker volume pruneコマンドを使用して、未使用のすべてのボリュームを削除します:

    $ docker volume prune
    

Additional resources追加リソース

The following resources will help you learn more about volumes:以下のリソースは、ボリュームについてさらに学ぶのに役立ちます:

Next steps次のステップ

Now that you have learned about persisting container data, it’s time to learn about sharing local files with containers.コンテナデータの永続化について学んだので、次はコンテナとローカルファイルを共有する方法について学びましょう。

Sharing local files with containers