Share data from one Kubernetes container to another inside same pod

11/28/2018

I have two containers inside my Pod:

  • Container A based on my own Dockerfile. Inside this Dockerfile, there is COPY ./files /my-files command. This image is inside my GitLab Docker registry.
  • Container B based on image from hub.docker.com.

I'd like to share data from Container A that are stored inside /my-files to Container B. I thought that I need to create a volume (it's not a persisted data) inside this pod and volumeMounts to the container.

Unfortunately when I add volumeMounts to Container A with mountPath: /my-files this directory is emptied and there are no files that were added when an image was created.

What should I do to keep this data and share it with Container B.

This is part of my Deployment.yaml file:

containers:
- name: Container-A
  image: "my-gitlab-registry/my-image-a-with-copied-files"
  volumeMounts:
    - name: shared-data
      mountPath: /my-files
- name: Container-B
  image: "some-public-image"
  volumeMounts:
  - name: shared-data
    mountPath: /files-from-container-a

volumes:
- name: shared-data
  emptyDir: {}
-- Marcin
kubernetes

1 Answer

10/2/2019

A ugly hack, use init container, to copy data into emptyDir volume, then mount volume in 2nd container.

  initContainers:
    - name: init-config-data-copy-wait
      image: datacontainer
      command:
        - sh
        - "-ce"
        - |
          set -ex
          cp -r /src-data/* /dst-data/
          ls /dst-data/
      volumeMounts:
        - mountPath: /dst-data
          name: dst-data-volume

  volumes:
    - name: dst-data-volume
      emptyDir: {}
-- Pieter
Source: StackOverflow