I am trying to run an nginx image as unprivileged, and found the following command stanza required to make this happen. I am NOT concerned with running the official nginx-unprivileged image, as that would defeat the purpose of the exercise (don’t ask why…please).
Intended commands to convert from linux terminal style to Kubernetes YAML Pod manifest init-container section…
RUN sed -i 's,listen 80;,listen 8080;,' /etc/nginx/conf.d/default.conf
&& sed -i '/user nginx;/d' /etc/nginx/nginx.conf
&& sed -i 's,/var/run/nginx.pid,/tmp/nginx.pid,' /etc/nginx/nginx.conf
&& sed -i "/^http {/a proxy_temp_path /tmp/proxy_temp;n client_body_temp_path /tmp/client_temp;n fastcgi_temp_path /tmp/fastcgi_temp;n uwsgi_temp_path /tmp/uwsgi_temp;n scgi_temp_path /tmp/scgi_temp;n" /etc/nginx/nginx.conf
&& chown -R 101:0 /var/cache/nginx
&& chmod -R g+w /var/cache/nginx
&& chown -R 101:0 /etc/nginx
&& chmod -R g+w /etc/nginx
I have tried the following using block scalars to no avail…
...
command: ["/bin/sh", "-c"]
args:
- >
sed -i 's,listen 80;,listen 8080;,' /etc/nginx/conf.d/default.conf
&& sed -i '/user nginx;/d' /etc/nginx/nginx.conf
&& sed -i 's,/var/run/nginx.pid,/tmp/nginx.pid,' /etc/nginx/nginx.conf
&& sed -i "/^http {/a proxy_temp_path /tmp/proxy_temp;n client_body_temp_path /tmp/client_temp;n fastcgi_temp_path /tmp/fastcgi_temp;n uwsgi_temp_path /tmp/uwsgi_temp;n scgi_temp_path /tmp/scgi_temp;n" /etc/nginx/nginx.conf
&& chown -R 101:0 /var/cache/nginx
&& chmod -R g+w /var/cache/nginx
&& chown -R 101:0 /etc/nginx
&& chmod -R g+w /etc/nginx
...
...
command: ["/bin/sh", "-c"]
args:
- |
sed -i 's,listen 80;,listen 8080;,' /etc/nginx/conf.d/default.conf
&& sed -i '/user nginx;/d' /etc/nginx/nginx.conf
&& sed -i 's,/var/run/nginx.pid,/tmp/nginx.pid,' /etc/nginx/nginx.conf
&& sed -i "/^http {/a proxy_temp_path /tmp/proxy_temp;n client_body_temp_path /tmp/client_temp;n fastcgi_temp_path /tmp/fastcgi_temp;n uwsgi_temp_path /tmp/uwsgi_temp;n scgi_temp_path /tmp/scgi_temp;n" /etc/nginx/nginx.conf
&& chown -R 101:0 /var/cache/nginx
&& chmod -R g+w /var/cache/nginx
&& chown -R 101:0 /etc/nginx
&& chmod -R g+w /etc/nginx
...
Also using a single line…
...
command: ["/bin/sh"]
args: ["-c", "sed -i 's,listen 80;,listen 8080;,' /etc/nginx/conf.d/default.conf && sed -i '/user nginx;/d' /etc/nginx/nginx.conf && sed -i 's,/var/run/nginx.pid,/tmp/nginx.pid,' /etc/nginx/nginx.conf && sed -i "/^http {/a proxy_temp_path /tmp/proxy_temp;n client_body_temp_path /tmp/client_temp;n fastcgi_temp_path /tmp/fastcgi_temp;n uwsgi_temp_path /tmp/uwsgi_temp;n scgi_temp_path /tmp/scgi_temp;n" /etc/nginx/nginx.conf && chown -R 101:0 /var/cache/nginx && chmod -R g+w /var/cache/nginx && chown -R 101:0 /etc/nginx && chmod -R g+w /etc/nginx"]
...
None of these worked…the init-container never starts.
Here is another attempt…but the initContainer remains in a crashloopbackoff state…
apiVersion: v1
kind: Pod
metadata:
name: securityreview
spec:
securityContext:
runAsUser: 101
runAsNonRoot: True
initContainers:
- name: permission-fix
image: nginx
command:
- /bin/sh
- -c
- sed -i 's,listen 80;,listen 8080;,' /etc/nginx/conf.d/default.conf
&& sed -i '/user nginx;/d' /etc/nginx/nginx.conf
&& sed -i 's,/var/run/nginx.pid,/tmp/nginx.pid,' /etc/nginx/nginx.conf
&& sed -i "/^http {/a proxy_temp_path /tmp/proxy_temp;n client_body_temp_path /tmp/client_temp;n
fastcgi_temp_path /tmp/fastcgi_temp;n uwsgi_temp_path /tmp/uwsgi_temp;n
scgi_temp_path /tmp/scgi_temp;n" /etc/nginx/nginx.conf
&& chown -R 101:0 /var/cache/nginx && chmod -R g+w /var/cache/nginx
&& chown -R 101:0 /etc/nginx && chmod -R g+w /etc/nginx
containers:
- name: webguy
image: nginx
securityContext:
runAsUser: 101
runAsGroup: 101
allowPrivilegeEscalation: false
2
Answers
The following file will run..but user 'nginx' still lacks the permissions; also the "&&" is not recognized by kubernetes.
I like to use the following approach to separating multiple commands in a readable way:
However, your case is more complicated, as running
sed
,chown
andchmod
commands without root privileges will result in aPermission denied
error.You can use an init container that shares a Volume with the nginx container.
The init container will run the
sed
,chown
andchmod
commands asroot
and then copy the modified files to the shared Volume that will be mounted and used by the nginx container. In this approach, you need a volume that init and application containers can use.A similar use case can be found in the Configure Pod Initialization documentation.
I will create an example to illustrate how it works.
As you can see in the code snippet below, I created the
permission-fix
init container that runs required commands and then copies modified files to the shared volume (cp -Rp /etc/nginx/* /mnt/nginx-fix/
). Thewebguy
container then mounts these files to/etc/nginx
:We can check if it works as expected:
If this response does not answer your question, please provide more details on what you want to achieve.