# Dockerコンテナ

## イメージの取得

まず `CLI` イメージを取得します。

```bash
docker pull keeper/keeper-secrets-manager-cli:latest
```

[こちらのDockerウェブサイト](https://hub.docker.com/r/keeper/keeper-secrets-manager-cli)でイメージをご覧ください。

## コンテナの実行

次にコンテナを実行します。デフォルトでは、シェルモードで `ksm` を起動するように設定されています。

```bash
docker run \
    --rm \
    -it \
    -v $PWD:/wd --workdir /wd \
    -v $HOME/.config:/etc/keeper -e KSM_INI_DIR=/etc/keeper \
    keeper/keeper-secrets-manager-cli:latest
```

1. `docker run` コマンド。
2. 実行が完了したときにコンテナを削除するフラグ。非アクティブなコンテナが増え続けるのを防ぎます。
3. コンテナとの対話操作を可能にするフラグ。
4. カレントディレクトリをコンテナ内の `/wd` にマウントし、作業ディレクトリも `/wd` に設定します。`/wd` への書き込みは、コンテナ外のカレントディレクトリに反映されます。ファイルのダウンロード先にするときなどに便利です。
5. `keeper.ini` を置きたい、またはすでに置いてあるディレクトリをマウントします。続けて、`keeper.ini` の読み書き先をCLIに伝える環境変数を渡します。
6. イメージの名前。

## エイリアス

`docker run` コマンドは毎回入力するには長すぎるため、エイリアスの作成をおすすめします。

```bash
$ alias ksm_shell='docker run --rm -it --workdir $PWD -v $PWD:$PWD -v $HOME/.config:/etc/keeper -e KSM_INI_DIR=/etc/keeper keeper/keeper-secrets-manager-cli:latest'
```

上記を実行すると、`ksm` のシェルが起動します。

以下のエイリアスでは、`run` コマンドの末尾にアプリケーション `ksm` を付けます。これにより、`ksm` はシェルモードでは起動しません。

```bash
$ alias ksm='docker run --rm -it --workdir $PWD -v $PWD:$PWD -v $HOME/.config:/etc/keeper -e KSM_INI_DIR=/etc/keeper keeper/keeper-secrets-manager-cli:latest ksm'
```

## 組み込みバイナリ

KSM CLIのDockerイメージには、GLIBC (ほとんどのLinuxディストリビューション) 用とMUSL (Alpine Linux) 用のCLIバイナリをまとめたボリュームマウントが含まれています。マウントポイントは `/cli` です。このディレクトリは、docker-composeの `volumes_from` またはDockerコマンドラインの `-v` で、別コンテナから取り込めます。`ksm` の実行ファイルは、利用するCライブラリの種類ごとのディレクトリに分かれています。

* `/cli/glibc/ksm` - Ubuntu、Debian、Fedora、CentOSなど、一般的なGLIBC環境向け
* `/cli/musl/ksm` - Alpine Linux向け

Alpine Linuxベースの構成向けには、専用のAlpineイメージも用意されています。[Docker Hubの該当ページ](https://hub.docker.com/r/keeper/keeper-secrets-manager-cli-alpine)をご参照ください。

以下は、別コンテナからCLIバイナリを参照する手順の一例です。

```
---
version: "2"
services:
  init:
    image: keeper/keeper-secrets-manager-cli:latest
  main:
    image: ubuntu:latest
    volumes_from:
      - init:ro
    command: [ '/cli/glibc/ksm', 'exec', 'printenv', 'MY_LOGIN' ]
    environment:
      KSM_CONFIG: ewog ... M09IemdQMnc9Igp9
      MY_LOGIN: keeper://bf18xLR3aVut5eYy7oIZZZ/field/login
      LC_ALL: C.UTF-8
      LANG: C.UTF-8
    depends_on:
      init:
        condition: service_completed_successfully
```

`init` サービスは、CLI用のDockerイメージを読み込みます。コンテナは起動してCLIのスプラッシュ画面を表示したあと終了します。コンテナが停止したあとも、`/cli` ボリュームにはアクセスできます。

`main` サービスは、`volumes_from` でCLI用イメージのボリュームを `/cli` にマウントします。`command` を上書きし、GLIBC版のKSM CLIを実行します。CLIの `exec` 機能により、[Keeper表記法](/keeperpam/jp/secrets-manager/about/keeper-notation.md)を使った環境変数の値がシークレットに置き換わります。ここではCLIの `exec` から `printenv` を起動し、Keeper表記法で設定した環境変数 **MY\_LOGIN** の展開結果を表示しています。

```
$ example : docker-compose up
[+] Running 2/0
 ⠿ Container example-init-1  Created                                                                                                                      0.0s
 ⠿ Container example-main-1  Recreated                                                                                                                    0.1s
Attaching to example-init-1, example-main-1
example-init-1  |
example-init-1  | ██╗  ██╗███████╗███╗   ███╗     ██████╗██╗     ██╗
example-init-1  | ██║ ██╔╝██╔════╝████╗ ████║    ██╔════╝██║     ██║
example-init-1  | █████╔╝ ███████╗██╔████╔██║    ██║     ██║     ██║
example-init-1  | ██╔═██╗ ╚════██║██║╚██╔╝██║    ██║     ██║     ██║
example-init-1  | ██║  ██╗███████║██║ ╚═╝ ██║    ╚██████╗███████╗██║
example-init-1  | ╚═╝  ╚═╝╚══════╝╚═╝     ╚═╝     ╚═════╝╚══════╝╚═╝
example-init-1  |
example-init-1  | Current Version: 1.0.13
example-init-1  |
example-init-1  | Running in shell mode. Type 'quit' to exit.
example-init-1  |
example-init-1 exited with code 0
example-main-1  | john.smith@localhost
example-main-1 exited with code 0
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keeper.io/keeperpam/jp/secrets-manager/secrets-manager-command-line-interface/docker-container.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
