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?

k8sでのcommnadとargsの挙動について

Last updated at Posted at 2025-05-08

image.png

はじめに

KubernetesPodをデプロイする際、
コンテナの起動コマンドを制御するためにcommandargsを指定することがあります。

例えば、以下のようなDockerfilemanifestファイルがあるとします。

Dockerfile
FROM alpine:latest

ENTRYPOINT ["echo", "hello"]
CMD ["world"]
manifest.yaml
apiVersion: v1
kind: Pod
metadata:
  name: echo
spec:
  containers:
  - name: echo
    image: echo:latest
    imagePullPolicy: Never
    command: ["echo", "1"]
    args: ["2"]

manifestで指定したcommnadargsは、
DockerfileのENTRYPOINTCMDとどのように関係しているのでしょうか。

本記事では、その挙動について解説します。

DockerfileでのCMDとENTRYPOINT

以下のDockerfileを例に挙動を見てみます

Dockerfile
FROM alpine:latest

ENTRYPOINT ["echo", "hello"]
CMD ["world"]

このDockerfileからimageを作成し、実行してみます

$ docker build -t echo .
$ docker run -it --rm echo
hello world

hello worldと出力されました。

つまり、ENTRYPOINTの実行コマンドにCMDが引数として渡されて実行されます

ENTRYPOINTだけ、CMDだけの時にどうなるかは、リファレンスを参照してみてください

(本題) k8sでのcommnadとargsの挙動について

さて、本題ですが、
KubernetesのPodについてのドキュメントに以下のように書いてありました。

Note:
The command field corresponds to ENTRYPOINT, and the args field corresponds to CMD in some container runtimes.

つまり、commandを指定すると、ENTRYPONITが上書かれ、argsを指定すると、CMDを上書きすることができます。

最初の例を見てみましょう。

Dockerfile
FROM alpine:latest

ENTRYPOINT ["echo", "hello"]
CMD ["world"]
manifest.yaml
apiVersion: v1
kind: Pod
metadata:
  name: echo
spec:
  containers:
  - name: echo
    image: echo:latest
    imagePullPolicy: Never
    command: ["echo", "1"]
    args: ["2"]

上書きが起きるとすると、
ENTRYPOINT: echo hello -> echo 1
CMD: world -> echo 2
となるので、echo 1 2が実行されるのでしょうか。

実行してみます。

$ kubectl apply -f manifest.yaml
$ kubectl logs echo       
1 2

予想が当たり、1 2が出力されましたね

まとめ

まとめです。

Dockerfile Kubernetes 実際に実行されるもの
ENTRYPOINT command command が優先(ENTRYPOINTを置き換える)
CMD args args が優先(CMDを置き換える)

CMDcommandが同じじゃ無いんですね〜

サンプルコード

参考

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?