Answers: <p>Dockerfile's <code>VOLUME</code> does not allow you to specify a host path.<br />
  On the host-side, the volumes are created with a very long ID-like name, these volumes are often referred to as unnamed/anonymous volumes.</p>
  <p>Given this Dockerfile:</p>
  <pre><code>FROM php7:latest
  VOLUME /var/www
  </code></pre>
  <p>Build it:</p>
  <pre><code>docker build -t myTest
  </code></pre>
  <p>Run it :</p>
  <pre><code>docker run --rm -it myTest
  </code></pre>
  <p>Inside the container, run <code>ls</code> and you'll notice the directory exists; <code>/var/www</code></p>
  <p>Running the container also creates a directory on the host-side.</p>
  <p>While having the container running, execute <code>docker volume ls</code> on the host machine and you'll see something like this</p>
  <pre><code>DRIVER    VOLUME NAME
  local     c984..e4fc
  </code></pre>
  <p>Back in the <em>container</em>, execute <code>touch /var/www/myFile</code></p>
  <p>This file is now available on the host machine, in one of the unnamed volumes.</p>
  <pre><code>ls /var/lib/docker/volumes/c984..e4fc/_data
  </code></pre>
  <p>Similarly, you can try to delete this file on the host and it will be deleted in the container as well.</p>
  <p>Now run a new container, but specify a volume using <code>-v</code>:</p>
  <pre><code>docker run --rm -it -v /myVolume myTest
  </code></pre>
  <p>This <em>adds</em> a second volume and the whole system ends up having two unnamed volumes.
  On the host-side, the new second volume is anonymous and resides together with the other volume in <code>/var/lib/docker/volumes/</code>.</p>
  <p>It was stated earlier that the <code>Dockerfile</code> can not map to a host path which sort of pose a problem for us when trying to bring files in from the host to the container during runtime. A different <code>-v</code> syntax solves this problem.</p>
  <p>Imagine I have a subfolder in my project directory <code>./src</code> that I wish to sync to <code>/src</code> inside the container. This command does the trick:</p>
  <pre><code>docker run -it -v $(pwd)/src:/src myTest
  </code></pre>
  <p>Both sides of the <code>:</code> character expects an absolute path. Left side being an absolute path on the host machine, right side being an absolute path inside the container.</p>
  <p>We run this command:</p>
  <pre><code>docker run -v $(pwd)/src:/src myTest
  </code></pre>
  <p><strong>Is the specification purely informational?</strong></p>
  <p>No, it does mount a volume, it's similar to <code>-v /var/www</code> you just don't specify a mount point in the host machine, so docker will take care of it.</p>
  <p><strong>What are the (dis)advantages of (not) specifying volumes?</strong><br />
  <strong>Why and in which cases should I use VOLUME and when shouldn't I?</strong></p>
  <p>It's probably a best practice to never use <code>VOLUME</code>.<br />
  The first reason we have already identified: We can not specify the host path.<br />
  The second reason is people might forget to use the <code>--rm</code> option when running the container, you will end up with a couple of unused volumes and it might be a daunting task to figure out which of all anonymous volumes are safe to remove</p>
  <p><strong>What about files that may optionally be mounted? Do they count as volumes in that case, too?</strong></p>
  <p>In this case you don't need volumes; mapping a single file does not make sense, I would use <code>ADD</code> or <code>COPY</code> inside the dockerfile.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-04-06T10:42:36.187Z
Answers: Can I start a new container, but point to an existing unnamed volume?
Answers: Levi
Answers: CC BY-SA 4.0
Answers: 2022-04-06T10:50:51.957Z
Answers: Anonymous volumes cannot be reused in next container running, if you have to do that,you will need to find them in /var/lib/docker/volumes/ (good luck with that) and mount them with -v, or create a named volume with `docker volume create` outside the scope of any container.
Answers: storm
Answers: CC BY-SA 4.0
Answers: 2022-04-13T14:12:29.853Z
Answers: Can you please take a look at this question https://devops.stackexchange.com/questions/13179/how-to-run-a-container-from-within-another-container-while-also-mounting-a-volum Its something I often struggle with and while I have "solved" it, I don't think I did it in the best way.
Answers: Levi
Answers: CC BY-SA 4.0
Answers: 2022-04-06T09:38:46.450Z
Answers: 2022-04-06T09:38:46.450Z
Answers: storm
Answers: <p>Say we have an application running on a docker container that generates some kind of daily reports. The daily reports that our application generates are stored somewhere within the container. One fine day we entered the office to realize the docker container was deleted because of some internal issues and that’s it, we lost all the reports which were stored within the container.</p>
  <p>To tackle such issues we should have some way to persist that valuable data even if the container is removed. This is possible with the help of the volume and the bind mount feature in the docker.</p>
  <p>Docker volume is a storage area on our local host system which is completely managed by docker. This is the area where our data will be persisted. In case our container is removed still we can refer to this volume and get back our data.</p>
  <blockquote>
  <blockquote>
  <p>docker volume create storage</p>
  </blockquote>
  </blockquote>
  <ul>
  <li>Here we are creating a volume named storage</li>
  </ul>
  <blockquote>
  <blockquote>
  <p>docker container run --name ubuntu-container-1 -d -it -v storage:/opt/test ubuntu</p>
  </blockquote>
  </blockquote>
  <p>-v is use to map the volume to a container path</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-10-01T06:46:45.773Z
Answers: 2022-10-06T16:12:02.687Z
Answers: 2022-10-06T16:12:02.687Z
Answers: Suraj M Nagrale
Body: <p>Somebody creates a php and Apache project, with docker, using volumes to persist the files that this project generated (plain text files saved in a folder).</p>
  <p>This person built the image and ran the project normally, with the exception of one detail, (s)he didn't link the folder where (s)he saved the messages (www/site/messages) (s)he simply used the /data folder, in which the project had no connection. And it worked.</p>
  <p>How does the volume really work? Because to me it doesn't make sense.</p>
  <p>This is the project:</p>
  <p><a href="https://github.com/matheusbattisti/curso_docker/tree/main/2_volumes" rel="nofollow noreferrer">https://github.com/matheusbattisti/curso_docker/tree/main/2_volumes</a></p>
  
Tags: <docker><volume>
Title: How Docker volume works?
UserDisplayName: Jose_Neto5689
ThreadText: # How Docker volume works?
  
  (Asked by: username_0 on 2022-04-05)
  
  Somebody creates a php and Apache project, with docker, using volumes to persist the files that this project generated (plain text files saved in a folder).
  
  This person built the image and ran the project normally, with the exception of one detail, (s)he didn't link the folder where (s)he saved the messages (www/site/messages) (s)he simply used the /data folder, in which the project had no connection. And it worked.
  
  How does the volume really work? Because to me it doesn't make sense.
  
  This is the project:
  
  <https://github.com/matheusbattisti/curso_docker/tree/main/2_volumes>
  
  **Tags:** docker, volume
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-04-06. Score: 3
  
  Dockerfile's `VOLUME` does not allow you to specify a host path.  
  On the host-side, the volumes are created with a very long ID-like name, these volumes are often referred to as unnamed/anonymous volumes.
  
  Given this Dockerfile:
  
  ```
  FROM php7:latest
  VOLUME /var/www
  
  ```
  
  Build it:
  
  ```
  docker build -t myTest
  
  ```
  
  Run it :
  
  ```
  docker run --rm -it myTest
  
  ```
  
  Inside the container, run `ls` and you'll notice the directory exists; `/var/www`
  
  Running the container also creates a directory on the host-side.
  
  While having the container running, execute `docker volume ls` on the host machine and you'll see something like this
  
  ```
  DRIVER    VOLUME NAME
  local     c984..e4fc
  
  ```
  
  Back in the *container*, execute `touch /var/www/myFile`
  
  This file is now available on the host machine, in one of the unnamed volumes.
  
  ```
  ls /var/lib/docker/volumes/c984..e4fc/_data
  
  ```
  
  Similarly, you can try to delete this file on the host and it will be deleted in the container as well.
  
  Now run a new container, but specify a volume using `-v`:
  
  ```
  docker run --rm -it -v /myVolume myTest
  
  ```
  
  This *adds* a second volume and the whole system ends up having two unnamed volumes.
  On the host-side, the new second volume is anonymous and resides together with the other volume in `/var/lib/docker/volumes/`.
  
  It was stated earlier that the `Dockerfile` can not map to a host path which sort of pose a problem for us when trying to bring files in from the host to the container during runtime. A different `-v` syntax solves this problem.
  
  Imagine I have a subfolder in my project directory `./src` that I wish to sync to `/src` inside the container. This command does the trick:
  
  ```
  docker run -it -v $(pwd)/src:/src myTest
  
  ```
  
  Both sides of the `:` character expects an absolute path. Left side being an absolute path on the host machine, right side being an absolute path inside the container.
  
  We run this command:
  
  ```
  docker run -v $(pwd)/src:/src myTest
  
  ```
  
  **Is the specification purely informational?**
  
  No, it does mount a volume, it's similar to `-v /var/www` you just don't specify a mount point in the host machine, so docker will take care of it.
  
  **What are the (dis)advantages of (not) specifying volumes?**  
  **Why and in which cases should I use VOLUME and when shouldn't I?**
  
  It's probably a best practice to never use `VOLUME`.  
  The first reason we have already identified: We can not specify the host path.  
  The second reason is people might forget to use the `--rm` option when running the container, you will end up with a couple of unused volumes and it might be a daunting task to figure out which of all anonymous volumes are safe to remove
  
  **What about files that may optionally be mounted? Do they count as volumes in that case, too?**
  
  In this case you don't need volumes; mapping a single file does not make sense, I would use `ADD` or `COPY` inside the dockerfile.
  
  > **Comments:**
  > **username_2**: Can I start a new container, but point to an existing unnamed volume?  
  > **username_1**: Anonymous volumes cannot be reused in next container running, if you have to do that,you will need to find them in /var/lib/docker/volumes/ (good luck with that) and mount them with -v, or create a named volume with `docker volume create` outside the scope of any container.  
  > **username_2**: Can you please take a look at this question https://devops.stackexchange.com/questions/13179/how-to-run-a-container-from-within-another-container-while-also-mounting-a-volum Its something I often struggle with and while I have "solved" it, I don't think I did it in the best way.  
  
  ---
  
  ### Answer by username_3 on 2022-10-01. Score: 1
  
  Say we have an application running on a docker container that generates some kind of daily reports. The daily reports that our application generates are stored somewhere within the container. One fine day we entered the office to realize the docker container was deleted because of some internal issues and that’s it, we lost all the reports which were stored within the container.
  
  To tackle such issues we should have some way to persist that valuable data even if the container is removed. This is possible with the help of the volume and the bind mount feature in the docker.
  
  Docker volume is a storage area on our local host system which is completely managed by docker. This is the area where our data will be persisted. In case our container is removed still we can refer to this volume and get back our data.
  
  > > docker volume create storage
  
  * Here we are creating a volume named storage
  
  > > docker container run --name ubuntu-container-1 -d -it -v storage:/opt/test ubuntu
  
  -v is use to map the volume to a container path
  
  
  ---
  
  