Share folder content between two containers in same pod

11/13/2017

I have a Pod with two containers, Nginx and Rails. I want to share the public folder from the rails to the nginx container, but the public contains already files, I don't want the folder be empty.

Is there a way with a shared-volume?

I tried:

- name: rails-assets
    hostPath:
      path: /app/public

But im getting this error:

Error: failed to start container "nginx": Error response from daemon: {"message":"error while creating mount source path '/app/public': mkdir /app: read-only file system"}
Error syncing pod
Back-off restarting failed container

Thanks,

-- murb83
google-kubernetes-engine
kubernetes
nginx
ruby-on-rails

2 Answers

11/15/2017

I fixed that problem creating a shared-volument shared-assets/ on the rails app. On the rails dockerfile I created a ENTRYPOINT with a bash script to copy the public/ files on the shared-assets/ folder. With this I can see the files now on the Nginx Container.

---
kind: Deployment
apiVersion: apps/v1beta1
metadata:
  name: staging-deployment
spec:
  replicas: 1
  revisionHistoryLimit: 2
  template:
    metadata:
      labels:
        app: staging
    spec:
      containers:
      - name: staging
        image: some/container:v5
        volumeMounts:
        - mountPath: /var/run/
          name: rails-socket
        - mountPath: /app/shared-assets
          name: rails-assets
      - name: nginx
        image: some/nginx:latest
        volumeMounts:
        - mountPath: /var/run/
          name: rails-socket
        - mountPath: /app
          name: rails-assets
      imagePullSecrets:
      - name: app-backend-secret
      volumes:
      - name: rails-socket
        emptyDir: {}
      - name: rails-assets
        emptyDir: {}

And the script ENTRYPOINT:

cp -r /app/public/ /app/shared-assets/
-- murb83
Source: StackOverflow

11/13/2017

One possible option would be to use ConfigMaps. You can put files into ConfigMaps and mount them into a Pod:

$ kubectl create configmap my-config --from-file=hello/world/

The yaml would look like this:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  restartPolicy: Never
  containers:
    - name: my-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh", "-c", "ls /hello/world" ]
      volumeMounts:
      - name: hello-world
        mountPath: '/hello/world'
  volumes:
    - name: hello-world
      configMap:
        name: my-config

However, Kubernetes sometimes reloads ConfigMaps, so changes in the /hello/world directory might get lost...

On the bottom, volumes are basically just some mounts, with all the restrictions. Especially the fact that if you mount anything into, say, /hello/world, all files that have been in that directory will not be visible while the directory is used as a mount point.

-- user3151902
Source: StackOverflow