Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 566
ealeon Avatar asked Feb 06 '18 09:02

ealeon


People also ask

Does Docker container data persist?

By default all files created inside a container are stored on a writable container layer. This means that: The data doesn't persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.

Do Docker compose volumes persist?

Whichever you choose, once you have set up a volume to the folder where the data is stored in the container, if you do a docker-compose down , and then a docker-compose up , your data will not be erased and it will become persistent.

Which of the following helps to persist the database container data?

Volumes are the preferred way to persist data in Docker containers and services. Some use cases for volumes include: Sharing data among multiple running containers. If you don't explicitly create it, a volume is created the first time it is mounted into a container.


1 Answers

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.

like image 88
Mateo Marconi Avatar answered Oct 05 '22 02:10

Mateo Marconi