0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dockerコンテナ上で作成したDynamoDBのデータが永続化されない

Last updated at Posted at 2023-10-01

はじめに

Dockerコンテナ上で作成したDynamoDBのデータが、docker down → upすると、データが消えるので、毎回テーブル作成する手間がかかる。

手順

1. docker-compose.ymlにオプション追加

docker-compose.yml
  api_dynamodb:
    container_name: api_dynamodb
    image: amazon/dynamodb-local:latest
    networks: [overlay]
    command: ["-jar", "DynamoDBLocal.jar", "-sharedDb"] ##オプション追加
    ports:
      - '8000:8000'
    volumes:
      - 'dynamo:/home/dynamodblocal' ##マウントするパスを設定

デフォルトのimagesはinMemoryオプションになっている。
これは、コンテナのメモリ上に保存するもので、コンテナをdownするとそのまま消えてしまう。
そのため、sharedDbオプションで、volumesで設定したディレクトリをコンテナにマウントする。
あくまで、データ保存は、コンテナではなく、ホストOSのvolumesのshared-local-instance.dbで管理する。

2. マウントされているvolumesを確認

Dynamoのコンテナに入り、マウントされているvolumesの中身を確認
shared-local-instance.dbがあればOK

➜  (main) ✗ docker-compose exec api_dynamodb bash
[dynamodblocal@d63f6813053d ~]$ pwd
/home/dynamodblocal
[dynamodblocal@d63f6813053d ~]$ ls
DynamoDBLocal.jar  DynamoDBLocal_lib  LICENSE.txt  README.txt  THIRD-PARTY-LICENSES.txt  shared-local-instance.db

InMemory: falseと、SharedDb: trueを確認

➜  (main) ✗ docker logs api_dynamodb
Initializing DynamoDB Local with the following configuration:
Port:	8000
InMemory:	false
DbPath:	null
SharedDb:	true
shouldDelayTransientStatuses:	false
CorsParams:	null

以下のようにvolumeを確認できる

 docker volume ls
 docker volume inspect (volume名)

以上。

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?