简体   繁体   中英

Docker volume: persist data on a remote host

https://docs.docker.com/storage/#more-details-about-mount-types

Good use cases for volumes

  • When you want to store your container's data on a remote host or a cloud provider, rather than locally.

How is this accomplished with docker volume? Aren't docker volume under hosts's /var/lib/docker?

Could you give me an example of "docker volume create" of this and how it could be utilized?

Yes, volumes are created under /var/lib/docker/volumes/ so you need to link this volume with the folder you want to persist or where you have your data to persist.

Example:

You have your image named ImageExample and your project under /var/www/MyProject/.

First, you need to create new volume and assign a name.

$ docker volume create --name VolumeExample

# if you run: docker volume ls, they list all your volumes available

$ docker volume ls
DRIVER              VOLUME NAME
local               JbpmVolume1
local               VolumeExample

Second, you have to link your new volume to a folder in your container.

$ docker run -v VolumeExample:/var/www/MyProject/ -p 8080:8080 MyImage

Where run is the command to create the container, -p is to map the local and host ports, MyImage is the image used in this example, VolumeExample is the volume created before and /var/www/MyProject/ is the example folder which you need to persist.

You can use this volume to store application configuration, database data or configuration too and so on. Maybe, depends on what you need to store, you can use bind mount or volumes or if your host is in linux, you can use tmpfs mounts.

As simple as that, you can read more about in docker webpage but basically this is how to work with a volume. Every time you stop/start or create/delete the container, the data in your volume will persist.

I do it in this way, because this is not the "happy path" you want. You have to mount before you store the data in the folder, because when you mount the volume, the folder will be empty because the volume is empty. If you have data in the folder before you mount the volume, the data will be not visible for you. So it depends on your project the way you will create the volume, but basically, with this two commands you mount the volume into the host container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM