<p>I am trying to deploy a test pod with nginx and logrotate sidecar.
Logrotate sidecar taken from: <a href="https://github.com/honestbee/logrotate" rel="nofollow noreferrer">logrotate</a></p>

<p>My Pod yaml configuration:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: nginx-apache-log
  labels:
    app: nginx-apache-log

spec:
  containers:

  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
    volumeMounts:
    - name: logs
      mountPath: /var/log

  - name: logrotate
    image: path/to/logrtr:sidecar
    volumeMounts:
    - name: logs
      mountPath: /var/log

  volumes:
  - name: logs
    emptyDir: {}
</code></pre>

<p>What I'd like to achieve is Logrotate container watching /var/log/<em>/</em>.log, however with the configuration above, nginx container is failing because there is no /var/log/nginx:</p>

<pre><code>nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
2018/10/15 10:22:12 [emerg] 1#1: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
</code></pre>

<p>However if I change mountPath for nginx from </p>

<pre><code>mountPath: /var/log 
</code></pre>

<p>to:</p>

<pre><code>mountPath: /var/log/nginx
</code></pre>

<p>then it is starting, logging to /var/log/nginx/access.log and error.log, but logrotate sidecar sees all logs in /var/log not /var/log/nginx/. It is not a problem with just one nginx container, but I am planning to have more container apps logging to their own /var/log/appname folders.</p>

<p>Is there any way to fix/workaround that? I don't want to run sidecar for each app.</p>

<p>If I change my pod configuration to:</p>

<pre><code>  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
    volumeMounts:
    - name: logs
      mountPath: /var/log
  initContainers:
    - name: install
      image: busybox
      command:
      - mkdir -p /var/log/nginx
      volumeMounts:
      - name: logs
        mountPath: "/var/log"
</code></pre>

<p>then it is failing with: </p>

<pre><code>Warning  Failed     52s (x4 over 105s)  kubelet, k8s-slave1  Error: failed to start container "install": Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"mkdir -p /var/log/nginx\": stat mkdir -p /var/log/nginx: no such file or directory": unknown
</code></pre>

DisplayName
<p>Leave the mount path as <code>/var/log</code>. In your nginx container, execute <code>mkdir /var/log/nginx</code> in a startup script. You might have to tweak directory permissions a bit to make this work.</p>
