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.
In Docker, you can use the following command to create a volume:
docker volume create [VOLUME_NAME]
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
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]
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.
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]
This will create a file in the mounted volume and write the "Hello World" string into it .
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
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.
When the mounted volume is no longer needed, you can delete it using the following command:
docker volume rm [VOLUME_NAME]
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!