Docker: Data volume mounting

Docker: Data volume mounting

  • 1. Data volume
  • 2. Data volume command
      • Replenish

1. Data volume

Data volume (volume) is a virtual directory, which is the mapping bridge between the directory in the container and the host directory.

Nginx containers have their own independent directories (Docker creates an independent container for each image, and each container is a running instance created based on the image). As shown in the figure above, Nginx will have a directory /usr/share that stores static resources. /nginx/html and the directory /etc/nginx/conf where the configuration file is saved.

It is very troublesome when we make modifications in the container. At this time, we need data volume.

Data Volume

  • A data volume is a directory or file on the host
  • When the container directory and the data volume directory are bound, each other’s modifications will be synchronized immediately.
  • A data volume can be mounted by multiple containers at the same time
  • A container can also be mounted with multiple data volumes

The role of data volumes

  • Container data persistence
  • Indirect communication between external machines and containers
  • Data exchange between containers

The location of the host where the data volume is located

Generally, they are /var/lib/docker/volumes/html/_data and /var/lib/docker/volumes/conf/_data

2. Data volume command

  • docker volume create Create data volume
  • docker volume ls View all data volumes
  • docker volume rm Delete the specified data volume
  • docker volume inspect View details of a data volume
  • docker volume prune Clear data volume


Note: The mounting of containers and data volumes must be configured when creating the container. Data volumes cannot be set for the created container. And during the process of creating a container, the data volume will be automatically created.

Demonstrate nginx’s html directory mounting

# 1. First create the container and specify the data volume. Note that the data volume is specified through the -v parameter\
docker rm -f nginx
docker run -d --name nginx -p 80:80 -v html:/usr/share/nginx/html nginx

# 2. Then view the data volume
docker volume ls
# result
DRIVER VOLUME NAME
local 29524ff09715d3688eae3f99803a2796558dbd00ca584a25a4bbc193ca82459f
local html

# 3. View data volume details
docker volume inspect html This command can find where the volume is mounted to the host.
# result
[
    {<!-- -->
        "CreatedAt": "2024-05-17T19:57:08 + 08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/html/_data",
        "Name": "html",
        "Options": null,
        "Scope": "local"
    }
]

# 4. View the /var/lib/docker/volumes/html/_data directory
ll /var/lib/docker/volumes/html/_data
# You can see the same content as nginx’s html directory. The results are as follows:
Total usage 8
-rw-r--r--. 1 root root 497 December 28 2021 50x.html
-rw-r--r--. 1 root root 615 December 28 2021 index.html

# 5. Enter the directory and modify the content of index.html at will
cd /var/lib/docker/volumes/html/_data
vi index.html

# 6. Open the page and check the effect

# 7. Enter the container and check whether the files in the /usr/share/nginx/html directory have changed.
docker exec -it nginx bash

View the content bound to the host and image

Change the html file of the host and the container will also be modified


Supplement

The docker rm -f command is used to forcefully delete one or more running Docker containers. The following explains the function of this command:

  • docker rm: This part represents the command to delete the container.
  • -f: This option means to force the container to be deleted, even if the container is running or has other related resources referenced.

Normally, if you want to delete a running container, Docker will not allow the deletion until the container is stopped. However, if you use the -f option, Docker will force stop the running container and delete it immediately.

When you use the docker rm -f command, Docker will immediately stop and delete the specified container. This is useful for situations where you need to quickly clean a container or deal with some issue. Be aware, however, that this may result in data loss, so use this command with caution.

The docker rmi nginx:latest command is used to delete the specified image from the local Docker image. The following explains the function of this command:

  • docker rmi: This part represents the command to delete the image.
  • nginx:latest: This is the name and label of the image to be deleted. In this example, nginx is the name of the image and latest is the tag.

After running this command, Docker will try to delete the nginx:latest image in local storage. If the image is not used by other containers and has no other tags associated with it, the image will be successfully deleted.

Note that once you delete an image, you can no longer use it to create new container instances. If the image is not backed up or pushed to the remote repository before deletion, the image cannot be restored. Therefore, before executing the docker rmi command, please ensure that you no longer need the image or have other appropriate backups and operations.

If you want to delete an image with multiple tags, you can use the same docker rmi command and list all the tags you want to delete. For example: docker rmi nginx:latest nginx:1.18 will delete both nginx:latest and nginx:1.18 images at the same time.