Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Volume to transfer files between host and Docker

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article Xiaobian introduces in detail for you "how to use Volume to transfer files between the host and Docker", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use Volume to transfer files between the host and Docker" can help you solve your doubts.

1. Use volume to transfer files between the host and the container.

You can see in the official documentation that a volume can be created using the following command:

Create a volume:

$docker volume create my-vol

Note that not all versions of docker can run this command:

The client and daemon api must both be at least 1.21 to use this command. Use the docker version command on the client to check your client and daemon api versions.

You can view the details after the creation is complete:

$docker volume inspect my-vol

Notice that the directory corresponding to this mountpoint is the directory we use for file transfer between the host and the container.

You can then use the volume when you start a container using run:

You can see that the data volume you just created is mounted to the hostdata directory in the container using the-v command. When we add files to the hostdata directory in the container, we can see them in the / var/lib/docker/volumes/my-vol/_data of the host. Similarly, adding files in this directory of the host can also be seen in the hostdata of the container.

I copied the file nginx to the directory used for exchange on the host, and after entering the container, you can also view it in the directory of hostdata:

Similarly, the files are copied to the hostdata directory in the container, and can also be viewed in the / var/lib/docker/volumes/my-vol/_data of the host.

Here I create a file testfile in the container and write to it: this is container writes, then go back to the host to view, and use vim in the host to add: "this is host write!" to it, and return to the container for viewing.

two。 Use a data volume container.

I have seen people in some places use data volume containers to share data between multiple containers, and the process goes like this:

1. First create a data volume container dbdata and mount a data volume to / bdata in it:

You can view the volume with a random name generated through docker volume ls.

two。 You can then use-volumes-from in other containers to mount the data volumes in the dbdata container, such as creating db1 and db2 containers, and mounting the data volumes from the dbdata container:

3. Then writes from either of the three containers under the / dbdata directory can be seen in the other containers.

In the figure, create a testfile file in the dbdata container and write "dbdata container write!", then view and write "db1 container write!" in the db1 container, then view and write "da2 container write!" in the db2 container, and finally go back to the dbdata container to view.

4. Then you can mount multiple data volumes through multiple-volumes-from, and then give the methods of backup and recovery, which can be searched online.

3. Why not use a data volume container?

See a lot of articles about data volume containers, and they are more or less the same. Then I thought about the need to find out that I didn't use data volumes (maybe I wasn't thoughtful enough).

It is mentioned above that the data volume container is used to share data among multiple containers, but this method can be achieved by mounting the same local directory. For example, the my-vol data volume created in the first step, I can mount it to both the db3 and db4 containers.

In the figure, I mount the my-vol data volume into db3 and db4, where I can find the previous data file, and write to the testfile file in the db4 container, which can also be viewed in the db3 container.

That is to say, I can also share data in multiple containers by mounting a data volume, and the files added in the host directory can also be viewed in all containers. If you back up, it is more convenient to copy the folder locally on the host computer.

4. Why use a data volume container?

However, the data volume container also has a good effect, that is, it can be used to specify the mount local directory. In the first point, we create a data volume named my-vol. The data volume is stored locally in the directory: / var/lib/docker/volumes/my-vol/_data. You can see that the name is very long and inconvenient to operate. You can solve this problem through the data volume container.

For example, I have created a directory on the host to store the file interaction between the host and the container, that is to say, if I want to transfer any files to the container in the future, I can copy the files directly to this directory:

I didn't find the command in the official documentation to specify the local directory corresponding to volume when creating data volumes using docker volume create (maybe I haven't seen enough).

But what if I just want to use the / usr/local/datadb directory? Now the function of the data volume container comes out. You can create a data volume container and hang the directory on the data volume container:

In the figure, I create a so-called data volume container and use the-v parameter to mount the / usr/local/datadb directory of the host just now, then create a file testfile in the container and write it to "" to view it on the host.

Then you can use-volumes-from to mount the data volume container when other containers are created, and then directly copy the files to the / usr/local/datadb directory when you want to transfer the files to the container. In turn, the container can transfer the files to the host and copy them directly to the mount directory in the container.

Read this, the "how to use Volume to transfer files between the host and Docker" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report