<p>k8s have documentation page about volumes and their types <a href="https://kubernetes.io/docs/concepts/storage/volumes/" rel="noreferrer">https://kubernetes.io/docs/concepts/storage/volumes/</a></p>

<p>So in short you need to define <code>spec.volumes</code> and <code>spec.containers[].volumeMounts</code> like:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}
</code></pre>

CC BY-SA 3.0
2017-08-21T15:37:50.877Z
is `emptyDir: {}` required if content from one container has to be shared with another container?
030
CC BY-SA 3.0
2017-08-21T15:42:05.280Z
`emptyDir` is one of the container types. As stated in linked docs "An `emptyDir` volume is first created when a Pod is assigned to a Node, and exists as long as that Pod is running on that node." So this is simple directory on storage assigned to the runner. This can be used to share data however this data should be treated as ephemeral.
Hauleth
CC BY-SA 3.0
2017-08-21T15:48:30.037Z
I try to share a directory from container x in container y
030
CC BY-SA 3.0
2017-08-21T16:03:21.233Z
@030 everything depends on what data you want to share and if that containers run on the same node.
Hauleth
CC BY-SA 4.0
2021-07-21T17:42:45.383Z
What would be the equivalent of `emptyDir`, but the data is **not** ephemeral. I don't want to have to define a `hostPath` or anything similar- I would like k8s to simply allocate some persistent disk resources for me and mount them onto a container. This is for local dev only - not needed for production. I've read the docs, but nothing seems clear and easy for what is so simple in docker-compose world.
Ryan Wheale
CC BY-SA 3.0
2017-08-21T15:34:11.783Z
2017-08-21T15:34:11.783Z
Hauleth
<p>Several <a href="https://docs.docker.com/compose/compose-file/#userns_mode" rel="nofollow noreferrer">named volumes</a> have been created in docker-compose. How to create these in Kubernetes?</p>

<p>How to transform the following <code>named volume</code> example defined in docker-compose to kubernetes format</p>

<pre><code>version: "3"
services:
  x:
    image: z
    volumes:
      - a:/path/inside/container
  y:
    image: c
    volumes:
      - a:/path/inside/container
volumes:
  a:
</code></pre>

<kubernetes>
How to create named volumes in Kubernetes?
030
# How to create named volumes in Kubernetes?

(Asked by: username_0 on 2017-08-21)

Several named volumes have been created in docker-compose. How to create these in Kubernetes?
How to transform the following named volume example defined in docker-compose to kubernetes format
version: "3"
services:
  x:
    image: z
    volumes:
      - a:/path/inside/container
  y:
    image: c
    volumes:
      - a:/path/inside/container
volumes:
  a:

**Tags:** kubernetes


---

## 1 answers

### Answer by username_1 on 2017-08-21. Score: 5 | Accepted answer

k8s have documentation page about volumes and their types https://kubernetes.io/docs/concepts/storage/volumes/
So in short you need to define spec.volumes and spec.containers[].volumeMounts like:
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

> **Comments:**
> **username_0**: is `emptyDir: {}` required if content from one container has to be shared with another container?  
> **username_1**: `emptyDir` is one of the container types. As stated in linked docs "An `emptyDir` volume is first created when a Pod is assigned to a Node, and exists as long as that Pod is running on that node." So this is simple directory on storage assigned to the runner. This can be used to share data however this data should be treated as ephemeral.  
> **username_0**: I try to share a directory from container x in container y  
> **username_1**: @username_0 everything depends on what data you want to share and if that containers run on the same node.  
> **username_2**: What would be the equivalent of `emptyDir`, but the data is **not** ephemeral. I don't want to have to define a `hostPath` or anything similar- I would like k8s to simply allocate some persistent disk resources for me and mount them onto a container. This is for local dev only - not needed for production. I've read the docs, but nothing seems clear and easy for what is so simple in docker-compose world.  

---

