如何设置Kubernetes启动的容器的工作目录

z3yyvxxp  于 2022-12-11  发布在  Kubernetes
关注(0)|答案(3)|浏览(209)

使用Kubernetes启动容器时,是否可以设置工作目录?

wwtsj6pe

wwtsj6pe1#

是的,通过容器规范的workingDir字段。下面是一个示例复制控制器,其中nginx容器将workingDir设置为/workdir

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: mynginximage
          workingDir: /workdir
xe55xuns

xe55xuns2#

I am assuming when launching the container implies setting working directory at container startup time. If not, using workingDir parameter as answered above works. I wanted the container launch command to work with a preset working directory and so I used the following pattern:

spec:
      containers:
        - name: <name>
          image: <image>
          command: ["/bin/bash"]
          args: ["-c", "cd /<my_workdir>; exec <my_command>;"]
n53p2ov0

n53p2ov03#

It should be also possible to set the working directory using env and command attributes. Below is an example:

apiVersion: v1
kind: Pod
metadata:
  name: print-greeting
spec:
  containers:
  - name: env-print-demo
    image: bash
    env:
    - name: GREETING
      value: "Warm greetings to"
    - name: HONORIFIC
      value: "The Most Honorable"
    - name: NAME
      value: "Kubernetes"
    command: ["echo"]
    args: ["$(GREETING) $(HONORIFIC) $(NAME)"]

相关问题