Docker イメージの開始コマンドの選択

Docker の紹介

Tim Sangster

Software Engineer @ DataCamp

スタートコマンドとは何ですか?

hello-world イメージはテキストを出力してから停止します。

docker run hello-world
Hello from Docker!

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon created a new container from the hello-world image which runs the
    executable that produces the output you are currently reading.
 3. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
Docker の紹介

スタートコマンドとは何ですか?

Pythonを含むイメージは起動時にPythonを開始できます。

docker run python3-sandbox
Python 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
...
....
>>> exit()
repl@host:/#
Docker の紹介

起動時のシェルコマンドの実行

CMD <shell-command>

CMD命令:

  • 画像が開始されると実行されます。
  • 画像のサイズを大きくしない。
  • ビルドに時間を追加しません。
  • 複数ある場合は、最後のもののみが有効です。
Docker の紹介

一般的な使用例

ワークフローを実行する、または外部接続を受け入れるアプリケーションの開始。

CMD python3 my_pipeline.py
CMD postgres

複数のアプリケーションを起動するスクリプトの開始

CMD start.sh
CMD python3 start_pipeline.py
Docker の紹介

いつ停止しますか?

$$

  • hello-world画像 -> テキストを印刷した後
  • データベースイメージ -> データベースが終了したとき

$$

より一般的なイメージには、より一般的な開始コマンドが必要です。

  • Ubuntuイメージ -> シェルが閉じられたとき
Docker の紹介

デフォルトの開始コマンドを上書きする

イメージの起動

docker run <image>

カスタム起動コマンドを使用したイメージの起動

docker run <image> <shell-command>

カスタム起動コマンドを使用したイメージのインタラクティブな起動

docker run -it <image> <shell-command>
docker run -it ubuntu bash
Docker の紹介

概要

使用法 Dockerfile 命令
イメージからコンテナが起動されたときに実行するシェルコマンドを追加します。 CMD <shell-command>

使用方法 シェルコマンド
イメージ内で設定された CMD を上書きする docker run <image> <shell-command>
画像内で設定された CMD を上書きし、対話形式で実行する docker run -it <image> <shell-command>
Docker の紹介

練習しましょう!

Docker の紹介

Preparing Video For Download...