Home > Operation and Maintenance > Docker > How to use volume for file sharing in Docker

How to use volume for file sharing in Docker

PHPz
Release: 2023-04-17 17:31:42
Original
854 people have browsed it

Docker is a very popular containerization platform that helps developers build and deploy applications more conveniently. In Docker, you can use volumes to manage file sharing between the container and the host, which is very useful for storing data in the container. Here are detailed instructions on how to use volumes for file sharing in Docker.

  1. Create volume

In Docker, you can use the following command to create a volume:

docker volume create [VOLUME_NAME]
Copy after login

Where [VOLUME_NAME] is the volume to be created name. After creation, you can use the following command to list all volumes:

docker volume ls
Copy after login
  1. Mount the volume into the container

When starting the Docker container, you can mount the volume into the container. Use the following docker run command to start the container and mount the volume to the container's /CONTAINER_PATH directory:

docker run -v [VOLUME_NAME]:/CONTAINER_PATH [IMAGE_NAME]
Copy after login

where [IMAGE_NAME] is the name of the container to be started. This will map the contents of [VOLUME_NAME] to the /CONTAINER_PATH directory in the container.

  1. Using volumes in containers

Now, volumes can be used in containers just like file systems. For example, you can create a file in the container and save it in the mounted volume:

cd /CONTAINER_PATH
touch [FILENAME]
echo "Hello World" > [FILENAME]
Copy after login

This will create a file in the mounted volume and write the "Hello World" string into it .

  1. Using volume on the host

After the data in the container is saved to the volume, the volume can be accessed on the host. Use the following command to copy the contents of the volume to the host:

docker volume inspect [VOLUME_NAME] | grep Mountpoint
Copy after login

This will display the mount point of the volume. By copying the files in the mount point, the data in the container can be copied to the host.

  1. Delete volume

When the mounted volume is no longer needed, you can delete it using the following command:

docker volume rm [VOLUME_NAME]
Copy after login

This will permanently delete the volume and all its data, please proceed with caution!

Summary

Using volume is a simple and effective way to share files in Docker. Files can be easily shared between the container and the host by creating a volume and mounting it into the container. For the best container management experience, try using Docker’s volume feature.

The above is the detailed content of How to use volume for file sharing in Docker. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template