Run a Container with a readOnlyRootFilesystem
One approach to running a container with readOnlyRootFilesystem: true
is to use writable
volume mounts (e.g. emptyDir, generic ephemeral
volumes, etc.) and then copy those directories in from the main image
using an initContainer; for example, for a
Deployment:
- Create an initContainerusing the sameIMAGEas your main container, share avolumeMount(e.g. namedscratch) between your main container and theinitContainer, mount each desired writable folder in theinitContainerunder an arbitrary directory (e.g./copy) within thescratchmount and at an arbitrarysubPath, recursively copy each image's original directory contents into eachmountPathduringinitContainerexecution, and finally use these samesubPathmountPathsto mount these writable directories in the main container:spec: template: spec: initContainers: - name: initcopy image: IMAGE volumeMounts: - name: scratch mountPath: /copy/AppServer subPath: appserver - name: scratch mountPath: /copy/tmp subPath: tmp - name: scratch mountPath: /copy/home subPath: home command: - /bin/sh args: - '-c' - "mkdir -p /copy && cp -rv /opt/IBM/WebSphere/AppServer /copy/ && cp -rv /tmp /copy/ && cp -rv /home/was /copy/" containers: - volumeMounts: - name: scratch mountPath: /opt/IBM/WebSphere/AppServer subPath: appserver - name: scratch mountPath: /tmp subPath: tmp - name: scratch mountPath: /home/was subPath: home volumes: - name: scratch emptyDir: {}- If there are small, default namespace-level CPU limits, then ensure
the initContainerhas a CPU limit so that I/O is not bottlenecked on CPU.
 
- If there are small, default namespace-level CPU limits, then ensure
the 
- Configure readOnlyRootFilesystem: true- Get the restricted SCC:
oc get scc restricted -oyaml > readonly-scc.yaml
- Edit readonly-scc.yamland make the following changes:- Change metadata.nametoreadonly
- Change readOnlyRootFilesystemtotrue
- Change groupsto[]
- Change usersto[]
- Change metadata.annotations.kubernetes.io/descriptiontoreadonly scc
 
- Change 
- Create the new SCC:
oc create -f readonly-scc.yaml
- Create a new service account:
oc create sa readonlysa
- Add the new SCC to the new SA:
oc adm policy add-scc-to-user readonly -z readonlysa
- Edit the deployment to use the readonly service account:
spec: serviceAccountName: readonlysa
 
- Get the restricted SCC: