Beginner Question: Understanding Volumes and persistance

Hi there,

I am a Docker newbie and trying to teach myself all about images, containers, etc…

I have installed Docker on a Linux VM and was able to pull and run some simple Docker containers that did not require much in the way of configuration.

Now I am trying to get a Dokuwiki container set up as a test, and I am a little stumped by the “volumes” concept.

Is my understanding correct: in order to save data that should exist even after a Docker container has been stopped, I can provide a folder on my host to the Docker container so that the app within the container can save its data to this folder?

If my understanding is correct, then the next question is: do I simply create folder somewhere in my file structure on my host system and then add it to the yaml config file for the respective container, or do I need to use the “docker volume” command? Or does the command also just create a folder on my host system?

This bit is a little confusing to me despite having read about it on various sources.

Containers are designed to be ephemeral by nature. Since applications still require persistent state, docker introduced volumes, which allow mounting storage into a container path.

Docker is able to mount named volumes and volume-binds into a container path.

volume-bind:

  • can mount an existing host path of your choice into a container path
  • you will need to make sure the process inside the container has permissions to access the folder
  • does not copy data back existing data from the container folder (as it’s not a named volume)
  • will eclipse the content of the container folder. You need to make that all expected files are available in your volume
  • is not a real volume, as such not listed by docker volume ls

named volume:

  • can be a manged file system, a remote share, or anything that exists as storage plugin for docker
  • usually takes care of setting the permissions for you
  • if the volume is empty: docker will copy existing data from the container path back into the volume
  • will eclipse the content of the container folder as well, but since it copies back pre-existing data, all expected files will be available in your volume.
  • is listed by docker volume ls

Thanks for taking the time to explain it, really appreciated. I think I am starting to understand how it works.