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 container

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Xiaobian to share with you how to use Volume in the host and Docker container file transfer, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

1. Use Volume to transfer files between hosts and containers.

You can see in the official documentation that you can create a volume using the following command:

Create a volume:

$ docker volume create my-vol

Note that this command does not work with all docker versions:

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.

Details can be viewed after creation:

$ docker volume inspect my-vol

Note that this Mountpoint corresponds to the directory we use for file transfers between hosts and containers.

Then you can use the volume when you start a container with run:

You can see that the created data volume is mounted to the hostdata directory in the container by using the-v command. At this time, when we add files to the hostdata directory in the container, you can see it in/var/lib/docker/volumes/my-vol/_data of the host. Similarly, you can also see it in the hostdata directory of the container by adding files to the host directory.

I copy the nginx file to the directory used for exchange in the host. After entering the container, you can also view it under the hostdata directory:

Similarly, copy files from the container to the hostdata directory, and view them in/var/lib/docker/volumes/my-vol/_data on the host.

Here I create a file testfile in the container and write: This is container write!, Then go back to the host and use vim to add: "This is host write! "and return to the container for viewing.

2. Use volume containers.

I've seen some places where people use volume containers to share data between multiple containers, and the process goes something like this:

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

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

2. Then, you can mount the data volumes in the dbdata container using-volumes-from in other containers, for example, create two containers db1 and db2, and mount the data volumes from the dbdata container:

3. Then at this point the writes in the/dbdata directory in any of the three containers are visible in the other containers.

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

4. Then you can mount multiple data volumes through multiple-volumes-from, and then give backup and recovery methods, a lot of search on the Internet.

3. Why not use volume containers?

I have seen many articles about data volume containers, and they are all similar. Then I thought about it and realized that there was no need to use data volumes (maybe I didn't think about it well enough).

Data volume containers mentioned above are used to share data among multiple containers, but this can obviously be done by mounting the same local directory. For example, the my-vol volume created in step 1 can be mounted in both db3 and db4 containers.

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

That is to say, I can also achieve data sharing in multiple containers by mounting a data volume, and files added to the host directory can also be viewed in all containers. If the backup, then directly in the host folder copy can be made locally, it is not more convenient.

4. Why use volume containers?

However, the volume container also has a good function, 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 directory where the data volume is stored locally is: /var/lib/docker/volumes/my-vol/_data. It can be seen that this name is very long and inconvenient to operate. This problem can be solved through the volume container.

For example, I created a directory on the host dedicated to storing files 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 directly copy the files to this directory:

I haven't found a command in the official documentation that specifies the local directory for volume when creating a volume using docker volume create (perhaps I haven't read enough).

But what if I want to use/usr/local/datadb? At this point, the role 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 mount the/usr/local/datadb directory of the previous host into it using the-v parameter, then create a file testfile in the container and write "HAHAHA" to view it on the host.

Then, when creating other containers, you can use-volumes-from to mount the data volume container, and then copy the files directly to the/usr/local/datadb directory when you want to transfer them to the container. Conversely, the files transferred from the container to the host can be copied directly to the mount directory in the container.

For backup, simply assign a copy of host/usr/local/datadb, and mount it again for recovery.

That's all for "How to use Volume in Host and Docker Container File Transfer", thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to 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: 245

*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

Servers

Wechat

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

12
Report