Answers: <p>Solution that worked for me in the past: create a new image out of the existing one and let entrypoint script of that new image to modify permissions of your volume_mountpoint.</p>
  
  <p>So in the case of your image (php:7.4-apache) you create entrypoint.sh file, something like</p>
  
  <pre><code>chown www-data:www-data -R /path/to/volume_mountpoint
  apache2-foreground
  </code></pre>
  
  <p>And then your Dockerfile should be something like</p>
  
  <pre><code>FROM php:7.4-apache
  COPY entrypoint.sh /entrypoint.sh
  ENTRYPOINT ["/entrypoint.sh"]
  </code></pre>
  
  <p>This way it should correct the permissions when container starts. Note, that I haven't tested the above, might require some tweaks - but hopefully it shows the idea.</p>
  
  <p>P.s. Maybe somebody has a better way of doing this - I would be very interested myself.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-13T04:08:32.393Z
Answers: 2020-03-13T04:08:32.393Z
Answers: taleodor
Body: <h3>My set-up</h3>
  
  <ul>
  <li>I have a Docker image based on <code>php:7.4-apache</code>.</li>
  <li>I have two Docker volumes for saving files that need to be persisted.</li>
  <li>On container start, I run <a href="https://certbot.eff.org/" rel="nofollow noreferrer">Certbot</a> to install HTTPs certificates.</li>
  </ul>
  
  <h3>Issue</h3>
  
  <p>I discovered that <strong>my application can't write to Docker volumes</strong>. </p>
  
  <h3>Debugging</h3>
  
  <p>Using <code>ls -l</code> I found out that the mountpoints of my volumes are owned by user <code>1451</code>:</p>
  
  <pre><code>drwxr-xr-x 4     1451 users     176128 Mar 12 13:25 volume_mountpoint
  </code></pre>
  
  <p>My web server is running under user <code>www-data</code>.</p>
  
  <h3>Attempted solution</h3>
  
  <p>I've tried setting the user under which my container runs to <code>www-data</code>. This caused the volume mountpoints to be owned by <code>www-data</code>, which fixed the permissions issue. The problem with this solution is that <code>Certbot</code> requires <code>root</code> permissions and fails to acquire HTTPs certificate without it.</p>
  
  <h3>Question</h3>
  
  <p>What is the proper way to fix this issue? I need <code>Certbot</code> to run under root, but let mountpoints to be owned by <code>www-data</code>.</p>
  
Tags: <docker>
Title: Can't write to Docker volume
UserDisplayName: Draex_
ThreadText: # Can't write to Docker volume
  
  (Asked by: username_0 on 2020-03-12)
  
  ### My set-up
  
  * I have a Docker image based on `php:7.4-apache`.
  * I have two Docker volumes for saving files that need to be persisted.
  * On container start, I run [Certbot](https://certbot.eff.org/) to install HTTPs certificates.
  
  ### Issue
  
  I discovered that **my application can't write to Docker volumes**.
  
  ### Debugging
  
  Using `ls -l` I found out that the mountpoints of my volumes are owned by user `1451`:
  
  ```
  drwxr-xr-x 4     1451 users     176128 Mar 12 13:25 volume_mountpoint
  
  ```
  
  My web server is running under user `www-data`.
  
  ### Attempted solution
  
  I've tried setting the user under which my container runs to `www-data`. This caused the volume mountpoints to be owned by `www-data`, which fixed the permissions issue. The problem with this solution is that `Certbot` requires `root` permissions and fails to acquire HTTPs certificate without it.
  
  ### Question
  
  What is the proper way to fix this issue? I need `Certbot` to run under root, but let mountpoints to be owned by `www-data`.
  
  **Tags:** docker
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-03-13. Score: 1 | Accepted answer
  
  Solution that worked for me in the past: create a new image out of the existing one and let entrypoint script of that new image to modify permissions of your volume\_mountpoint.
  
  So in the case of your image (php:7.4-apache) you create entrypoint.sh file, something like
  
  ```
  chown www-data:www-data -R /path/to/volume_mountpoint
  apache2-foreground
  
  ```
  
  And then your Dockerfile should be something like
  
  ```
  FROM php:7.4-apache
  COPY entrypoint.sh /entrypoint.sh
  ENTRYPOINT ["/entrypoint.sh"]
  
  ```
  
  This way it should correct the permissions when container starts. Note, that I haven't tested the above, might require some tweaks - but hopefully it shows the idea.
  
  P.s. Maybe somebody has a better way of doing this - I would be very interested myself.
  
  
  ---
  
  