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
initContainer
using the sameIMAGE
as your main container, share avolumeMount
(e.g. namedscratch
) between your main container and theinitContainer
, mount each desired writable folder in theinitContainer
under an arbitrary directory (e.g./copy
) within thescratch
mount and at an arbitrarysubPath
, recursively copy each image's original directory contents into eachmountPath
duringinitContainer
execution, and finally use these samesubPath
mountPaths
to 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: {}
- Configure
readOnlyRootFilesystem: true
- Get the restricted SCC:
oc get scc restricted -oyaml > readonly-scc.yaml
- Edit
readonly-scc.yaml
and make the following changes:- Change
metadata.name
toreadonly
- Change
readOnlyRootFilesystem
totrue
- Change
groups
to[]
- Change
users
to[]
- Change
metadata.annotations.kubernetes.io/description
toreadonly 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: