I created an init container to mount volume.
Here is an example and actually I will use the volume to inject something into main container for monitoring purpose. Imagine I will do this for all current running pods, it is not good to rebuild all images.
init container dockerfile:
FROM alpine:3.15
RUN apk add curl
RUN curl -Lo /tmp/resource.zip "https://some/resource.zip" && unzip /tmp/resource.zip -d /data/
pod yaml:
...
volumes:
- name: my-volume
emptyDir: {}
initContainers:
- name: my-volume
image: init/container/image:tag
command: ['sh']
args: ['-c', 'cp -r /data/* /var/volume/']
volumeMounts:
- name: my-volume
mountPath: /var/volume
containers:
- name: main-app
image: python:3.9-alpine
command: ['sh']
args: ['-c', 'cd /var/volume && python -m http.server 80']
volumeMounts:
- name: my-volume
mountPath: /var/volume
...
Will init container keep consuming cpu and memory during runtime? Is it like a job that just exits before main container? If so, how is the volume provided after init container exits?
CodePudding user response:
Containers that run in the same pod, behave like they are running in the same 'logical host'. There will only be one IP number associated with it, and there will be only one set of storage volumes associated with it, which can be mounted on multiple containers at the same time.
The docker engine will create a single persistent volume 'my-volume', and then first run the init-container process with that volume mounted on it, and then the main app with the same volume (and also the same IP address).
The cpu and memory resources for the init-container are released when that container finishes.