How to mount volume for docker container via yaml manifest?

2.5k views Asked by At

I try to launch container-vm machine with following yaml:

version: v1
kind: Pod
spec:
  containers:
    - name: simple-echo
      image: gcr.io/google_containers/busybox
      command: ['nc', '-p', '8080', '-l', '-l', '-e', 'echo', 'hello world!']
      imagePullPolicy: Always
      ports:
        - containerPort: 8080
          hostPort: 8080
          protocol: TCP
      volumeMounts:
        - name: string
          mountPath: /home
          readOnly: false
  restartPolicy: Always
  dnsPolicy: Default
  volumes:
    - name: string
      source:
        # Either emptyDir for an empty directory
        # emptyDir: {}
        # Or hostDir for a pre-existing directory on the host
        hostDir:
          path: /home

I expect host home directory being accessible from the container.

However, container fails to start:

E0619 05:02:09.477574    2212 http.go:54] Failed to read URL: invalid pod: 
[spec.volumes[0].source: invalid value '<*>(0xc2080b79e0){HostPath:<nil> EmptyDir:<nil> GCEPersistentDisk:<nil> AWSElasticBlockStore:<nil> 
GitRepo:<nil> Secret:<nil> NFS:<nil> ISCSI:<nil> Glusterfs:<nil> PersistentVolumeClaimVolumeSource:<nil> RBD:<nil>}': 
exactly 1 volume type is required spec.containers[0].volumeMounts[0].name: not found 'string']

What is the correct way to specify a volume for container?

2

There are 2 answers

2
Robert Bailey On BEST ANSWER

Try replacing hostDir with hostPath as mentioned in v1beta3-conversion-tips-from-v1beta12.

Try replacing

volumes:
    - name: string
      source:
        # Either emptyDir for an empty directory
        # emptyDir: {}
        # Or hostDir for a pre-existing directory on the host
        hostDir:
          path: /home

with

volumes:
    - name: string
      hostPath:
        path: /home

at the bottom of your configuration.

1
pierrelb On

There is a simple way to do it:

version: v1 kind: Pod spec: containers: - name: simple-echo image: gcr.io/google_containers/busybox command: ['nc', '-p', '8080', '-l', '-l', '-e', 'echo', 'hello world!'] imagePullPolicy: Always ports: - containerPort: 8080 hostPort: 8080 protocol: TCP volumeMounts: - name: string mountPath: /home readOnly: false restartPolicy: Always dnsPolicy: Default volumes: - /path/dir/from/host:/name/of/dir/in/container