LoginSignup
5
1

More than 5 years have passed since last update.

OCI CLI環境をDockerのコンテナで作る

Last updated at Posted at 2019-01-30

やりたいこと

コマンドライン(CLI)でOCI操作したいが、なんだか環境ごとにインストールするのが面倒だったり、
PythonのVersionに依存してエラーが出たりと面倒だったのでコンテナにしてみました。

参考

OracleのBlogを参考にしました。

Get going quickly with Command Line Interface for Oracle Cloud Infrastructure using Docker container

環境準備

ビルド用のファイルの作成

ビルド用のファイルを作成して配置します。

.
├── Dockerfile
└── requirements.txt
Dockerfile
FROM python:3.6-alpine3.7

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --upgrade pip
RUN apk add --update alpine-sdk libffi libffi-dev openssl openssl-dev && pip install --no-cache-dir -r requirements.txt --no-use-pep517

COPY . .

ENTRYPOINT ["/usr/local/bin/oci"]
CMD []
requirements.txt
oci
oci-cli

--no-use-pep517をつけているのはBlogに記載されている方法で実行すると以下のようなエラーが出ました。
回避策としてpip引数に--no-use-pep517をつけています。

どうやらpipのBugのようです。そのうちFixしそう。

Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pip/_internal/cli/base_command.py", line 176, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.6/site-packages/pip/_internal/commands/install.py", line 346, in run
    session=session, autobuilding=True
  File "/usr/local/lib/python3.6/site-packages/pip/_internal/wheel.py", line 886, in build
    assert have_directory_for_build
AssertionError

イメージの作成

ファイルを配置したディレクトリに移動し、以下を実行。

docker build . -t ocicli

作成が完了するといかのような形でリポジトリに登録されます。

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
ocicli                latest              48a42d414ecd        7 seconds ago       311MB
python                3.6-alpine3.7       9b325f49dce0        3 days ago          74.2MB

設定ファイルとAPIキーの配置用ディレクトリの作成

mkdir ~/.oci

実行そして確認

設定ファイルとAPIキーの作成

$ docker run --rm --mount type=bind,source=$HOME/.oci,target=/root/.oci -it ocicli:latest setup config
    This command provides a walkthrough of creating a valid CLI config file.

    The following links explain where to find the information required by this
    script:

    User OCID and Tenancy OCID:

        https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/apisigningkey.htm#Other

    Region:

        https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/regions.htm

    General config documentation:

        https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm


Enter a location for your config [/root/.oci/config]:
Enter a user OCID: ocid1.user.oc1.xxxxxxx
Enter a tenancy OCID: ocid1.tenancy.oc1.xxxxxx
Enter a region (e.g. ca-toronto-1, eu-frankfurt-1, uk-london-1, us-ashburn-1, us-phoenix-1): us-ashburn-1
Do you want to generate a new RSA key pair? (If you decline you will be asked to supply the path to an existing key.) [Y/n]: Y
Enter a directory for your keys to be created [/root/.oci]:
Enter a name for your key [oci_api_key]:
Public key written to: /root/.oci/oci_api_key_public.pem
Enter a passphrase for your private key (empty for no passphrase):
Private key written to: /root/.oci/oci_api_key.pem
Fingerprint: xxxxxxxxxxxxxxxx
Config written to /root/.oci/config


    If you haven't already uploaded your public key through the console,
    follow the instructions on the page linked below in the section 'How to
    upload the public key':

        https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/apisigningkey.htm#How2

Localにoci_api_keyができます。

$ ls -l ~/.oci/
total 40
-rw-------  1 ykoike  staff   296  1 30 00:55 config
-rw-------  1 ykoike  staff  1675  1 30 00:55 oci_api_key.pem
-rw-------  1 ykoike  staff   451  1 30 00:55 oci_api_key_public.pem

API KEYの登録

APIキーの登録はコマンドライン(CLI)でOCIを操作する - Oracle Cloud Infrastructureアドバンスドの「3-2. APIキーのアップロード」を参考にしてください。

試しにComputeのイメージをリストしてみる

出力が長いのでログは割愛しますが、成功しましたー。

docker run --rm --mount type=bind,source=$HOME/.oci,target=/root/.oci -it ocicli:latest compute image list -c ocid1.compartment.oc1.xxxxxxxxx

これでどの環境でもさっくりOCIのCLIを実行する環境が作れるはず。

おまけ ショートカットコマンドを作成する

profileなどに以下を記載して読み込ませることで、いちいちdockerコマンドを打たなくて済みます。

oci() {docker run --rm --mount type=bind,source=$HOME/.oci,target=/root/.oci -it ocicli:latest "$@"; }
oci --version
2.4.42
5
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
5
1