Now let’s look at how you make a Volume.
VolumesCreating VolumesVolumes can be created via a Dockerfile or an API request.
Here’s a Dockerfile instruction that creates a volume at run time:VOLUME /my_volumeThen, when the container is created, Docker will create the volume with any data that already exists at the specified location.
Note that if you create a volume using a Dockerfile, you still need to declare the mountpoint for the volume at run time.
You can also create a volume in a Dockerfile using JSON array formatting.
See this earlier article in this series for more on Dockerfiles.
Volumes also can be instantiated at run time from the command line.
Volume CLI CommandsCreateYou can create a stand-alone volume with docker volume create —-name my_volume.
InspectList Docker volumes with docker volume ls.
Volumes can be inspected with docker volume inspect my_volume.
RemoveThen you can delete the volume with docker volume rm my_volume.
Dangling volumes are volumes not used by a container.
You can remove all dangling volumes with docker volume prune.
Docker will warn you and ask for confirmation before deletion.
If the volume is associated with any containers, you cannot remove it until the containers are deleted.
Even then, Docker sometimes doesn’t realize that the containers are gone.
If this occurs, you can use docker system prune to clean up all your Docker resources.
Then you should should be able to delete the volume.
Where your data might be storedWorking with –mount vs.
–volumeYou will often use flags to refer to your volumes.
For example, to create a volume at the same time you create a container use the following:docker container run –mount source=my_volume, target=/container/path/for/volume my_imageIn the old days (i.
pre-2017) ????the –volume flag was popular.
Originally, the -v or –volume flag was used for standalone containers and the –mount flag was used with Docker Swarms.
However, beginning with Docker 17.
06, you can use –mount in all cases.
The syntax for –mount is a bit more verbose, but it’s preferred over –volume for several reasons.
–mount is the only way you can work with services or specify volume driver options.
It’s also simpler to use.
You’ll see a lot of -v’s in existing code.
Beware that the format for the options is different for –mount and –volume.
You often can’t just replace a -v in your existing code with a –mount and be done with it.
The biggest difference is that the -v syntax combines all the options together in one field, while the –mount syntax separates them.
Let’s see –mount in action!Easy enough to mount–mount — options are key-value pairs.
Each pair is formatted like this: key=value, with a comma between one pair and the next.
Common options:type — mount type.
Options are bind, volume, or tmpfs.
We’re all about the volume.
source — source of the mount.
For named volumes, this is the name of the volume.
For unnamed volumes, this option is omitted.
The key can be shortened to src.
destination — the path where the file or directory is mounted in the container.
The key can be shortened to dst or target.
readonly —mounts the volume as read-only.
Takes no value.
Here’s an example with lots of options:docker run –mount type=volume,source=volume_name,destination=/path/in/container,readonly my_imageVolumes are like spices — they make most things better.
????WrapRecap of Key Volume Commandsdocker volume createdocker volume lsdocker volume inspectdocker volume rmdocker volume pruneCommon options for the –mount flag in docker run –mount my_options my_image:type=volumesource=volume_namedestination=/path/in/containerreadonlyNow that you’ve familiarized yourself with data storage in Docker let’s look at possible next steps for your Docker journey.
Next stepsIf you haven’t read the articles in this series on Docker concepts, the Docker ecosystem, Dockerfiles, slim images, and commands, check those out.
If you’re looking for another article on Docker concepts to help cement your understanding, check out Preethi Kasireddy’s great article here.
If you want to go deeper, check out Nigel Poulton’s book Docker Deep Dive (make sure to get the most recent version).
If you want to do a lot of building while you learn, check out James Turnbull’s The Docker Book.
I hope you found this series to be a helpful intro to Docker.
If you did, please share it with others on your favorite forums or social media channels so your friends can find it, too!I’m working on a series on orchestrating containers with Kubernetes.
If that’s of interest to you, follow me to make sure you don’t miss it!Thanks for reading!.????.