0

I am reading about volumes in docker, and am a bit confused.

One way we work with them is by creating a volume and then running a container using it:

docker volume create data_volume
docker run -v data-volume:/var/opt/project bash:latest \
  bash -c "echo Testing-Volume> /var/opt/project/testing.txt"

First, we create a docker volume data-volume on host, and then map it to /var/opt/project in container. So when a file is created/modified in /var/opt/project, all those changes would be persisted to data-volume in host OS. Persistence is achieved.

But I also see volume created in dockerfile:

FROM ubuntu:latest
# Create a volume for storing application logs
VOLUME /var/log/app
.....

How does this one work, as there is no mapping to some persistent storage on host OS?

Mandroid
  • 6,200
  • 12
  • 64
  • 134
  • 1
    There's a pretty cool answer to an other SO post https://stackoverflow.com/a/46992367/6336357 – Rick Rackow Aug 17 '23 at 10:08
  • 1
    If nothing else is mounted on that directory, `VOLUME` creates an _anonymous volume_, which is just like the named volume in the first example but it doesn't have a name. You usually don't want a `VOLUME` directive. – David Maze Aug 17 '23 at 10:40

0 Answers0