2
1

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で起動したCosmosDBのデータを永続化させる

Posted at

概要

公式の手順に則ってDocker(Linux コンテナー)を起動する(windowsでWSLを使って起動した場合も同じ手順だと思う)が、環境変数だけではデータを永続化できない(docker downしたらデータが消える)ので消えない方法を調べました

公式の手順

結論

  1. 環境変数「AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE」に何かしらを設定する
  2. /tmp/cosmosにボリュームをマウントする

dockerコマンドサンプル

docker run \
    --publish 8081:8081 \
    --publish 10250-10255:10250-10255 \
    --interactive \
    --tty \
    -env AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
    -v ./db:/tmp/cosmos
    mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest  

docker-compose.yml サンプル

version: '3.8'

services:
  cosmos:
    container_name: cathy_cosmos
    image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
    tty: true
    stdin_open: true
    ports:
      - 8081:8081
      - 10250-10255:10250-10255
    environment:
      AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE: true
    volumes:
      - cosmos-volume:/tmp/cosmos

volumes:
  cosmos-volume:

どうなっているのか

提供されているイメージでは /usr/local/bin/cosmos/start.sh を起動するようになっています。
image.png

start.sh をみると、以下のような箇所があり、/tmp/cosmosを利用していることと、環境変数によってDBの初期化処理を切り分けていることが分かります。

#!/bin/sh
~~略~~
COSMOS_APP_HOME=/tmp/cosmos


EMULATOR_DEFAULT_CERTIFICATE="default.sslcert.pfx"
EMULATOR_CERTIFICATE_OPTION="/exportcert=c:\\${EMULATOR_DEFAULT_CERTIFICATE}"

if [ -z "${AZURE_COSMOS_EMULATOR_CERTIFICATE}${AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE}" ]; then
    # Work around to remove old emulator data since restarting with existing data does not work at this time.
    if test -d "${COSMOS_APP_HOME}/appdata"; then
        rm -fr "${COSMOS_APP_HOME}/appdata"
    fi

    mkdir -p ${COSMOS_APP_HOME}/appdata
else
    if test -d "${COSMOS_APP_HOME}/appdata"; then
        rm -fr "${COSMOS_APP_HOME}/appdata/log"
        rm -fr "${COSMOS_APP_HOME}/appdata/var"
        rm -fr "${COSMOS_APP_HOME}/appdata/wfroot"
        rm -fr "${COSMOS_APP_HOME}/appdata/Packages"
        rm -fr "${COSMOS_APP_HOME}/appdata/gateway.log"
    else
        mkdir -p ${COSMOS_APP_HOME}/appdata
    fi
    
~~略~~

ということで、環境変数を設定し、/tmp/cosmosにマウントすることでデータを永続化することができます。

公式のマニュアルに書いてほしい

ここから依頼しましょう。依頼済みです。
image.png

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?