How to set the workdir of a container launched by Kubernetes

DockerKubernetes

Docker Problem Overview


Is it possible to set the working directory when launching a container with Kubernetes ?

Docker Solutions


Solution 1 - Docker

Yes, through the workingDir field of the container spec. Here's an example replication controller with an nginx container that has workingDir set to /workdir:

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

Solution 2 - Docker

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)"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpditommasoView Question on Stackoverflow
Solution 1 - DockerghodssView Answer on Stackoverflow
Solution 2 - DockerDebView Answer on Stackoverflow