In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
A Docker image is made up of multiple file systems (read-only layers) superimposed. When we start a container, Docker loads the read-only mirror layer and adds a read-write layer on top of it (that is, the top of the mirror stack). If the running container modifies an existing file, the file will be copied from the read-only layer below the read-write layer to the read-write tier, and the read-only version of the file still exists, but has been hidden by a copy of the file in the read-write layer. When you delete the Docker container and restart through the image, the previous changes will be lost.
In Docker, the combination of the read-only layer and the read-write layer at the top is called Union File System (federated file system).
In order to preserve (persist) data and share data between containers, Docker proposed the concept of Volume.
Simply put, Volume is a directory or file, which can bypass the default federated file system and exist on the host in the form of a normal file or directory.
Summary: Volume can separate the container and the data generated by the container, when you use docker rm
When my_container deletes a container, it does not affect the relevant data.
We can initialize Volume in two ways, with some small but important differences. we
You can declare Volume at run time with-v:
Docker manages data in two ways:
∙ data Volume (Data Volumes)
∙ data volume container (Data Volumes Containers)
Data volumes:
A data volume is a directory that is specifically designated for one or more containers to bypass Union File System. It is located in the container, and you can set the
The directory of the host is mounted to the data volume, and the modification of the data volume is immediately visible, and updating the data will not affect the mirror.
For example, to realize the migration of data between the host and the container, and provide some useful functions for persistence or sharing data
The use of data volumes, similar to the mount operation on the directory under Linux
∙ data volumes can be shared and reused among containers
∙ data volume data changes are modified directly
∙ data volume data changes are not included in the container
The ∙ data volume is persistent until no container uses it
1. Create a data volume
Use the-v option in the docker run command to create data volumes in the container. Use the-v option multiple times to create multiple
Data volume. For example:
[root@docker01] # docker run-itd-v / data1-v / date2-- name web003 centos:httpv1
90030942574d6b47b6be2922a86b38f7483956f6231ab5e30fccc9d4431f61ec
When you enter the container web003, you can see that the two data volumes have been successfully created and mounted on / data1 and / date2, respectively.
You can use docker inspect web003 to view mounted data volumes.
[root@docker01 ~] # docker inspect web003
Users can also specify data volumes in Dockerfile:
For example:
FROM debian:wheezy
VOLUME / data
Note the default permission for 1:Docker to mount data volumes is read and write, and users can also specify read-only through: ro.
For example:
2. Mount the host directory as a data volume
If you want to use a directory on the host in the container, you can specify it with the-v parameter (note: note that the
And the following):
Docker run-v / host/path:/some/path...
This explicitly tells Docker to use the specified host path instead of the root path created by Docker itself and mount it into the container
The specified path (the above example is / some/path). It is important to note that the path on the host local directory must make
With an absolute path, if the path on the host does not exist, the directory is automatically created in the given path.
When using the docker run command, you can specify the directory where a local host is mounted to the container, and you can use many
The secondary-v option mounts multiple local host directories for a docker container run.
Example: 1. Create a / web/webapp1 directory on the host, and create an index.html file, as follows:
[root@docker01 ~] # mkdir / web/webapp1-p
[root@docker01 ~] # cd / web/webapp1/
[root@docker01 webapp1] # echo "hello world" > > index.html
[root@docker01 webapp1] # cat index.html
Hello world
[root@docker01 webapp1] #
2. Use an image with an apache service to generate a container
[root@docker01] # docker run-itd-v / web/webapp1/:/var/www/html/-p 8000 itd 80-- name web005
Centos:httpv1
3. Visit the container's website service:
The above command loads the host's / web/webapp1 directory into the container's / var/www/html directory. This work
It is very convenient when testing, for example, users can place some programs in the local directory to see if the container
Working normally. The path to the local directory must be absolute. If the directory does not exist, Docker will automatically create it for you.
it. All the files in the / web/webapp1 directory will appear in the container. This is useful for sharing files between hosts and containers
Is very helpful, such as mounting source code that needs to be compiled. To ensure portability (not the master of all systems
The machine directory is available), and the mount host directory does not need to be specified from Dockerfile.
Data volume container:
If you need to share some data between containers, the easiest way is to use a data volume container. Data volume container
It is an ordinary container that provides data volumes for mounting use by other containers.
The usage is as follows: first, you need to create a container as a data volume container, and then use it when other containers are created-
-data volumes in the volumes-from mount data volume container are used.
A common usage scenario is to use pure data volume containers to persist databases, configuration files, or data files, and so on.
For example: docker run-- name dbdata postgres echo "Data-only container for postgres"
This command will use a postgres image that already has a Volume defined in Dockerfile to generate a volume.
Dbdata (for example: VOLUME / var/lib/postgresql/data), run the echo command and exit. When I
When we run the docker ps command, echo can help us identify the purpose of a certain image. We can use the-- volumesfrom command to identify the Volume of other containers:
Docker run-d-- volumes-from dbdata-- name db1 postgres
Example:
Now let's create a named data volume container:
# docker run-dit-v / test-- name data image
Mount the / test volume in another container using the-- volumes-from option. Regardless of whether the data container is running or not, other containers
The container data volume can be mounted, of course, if it is only a separate data volume, it is not necessary to run the container.
You can then mount the / test volume using-- volumes-from in other containers
# docker run-dit-- volumes-from data-- name test1 image
Add another container:
Note: you can also use multiple-- volumes-from parameters to mount multiple data volumes from multiple containers
Perform docker ps View
Enter the test1 and test2 containers, and execute df to view
You can also inherit other containers containing / test volumes
# docker run-dit-- volumes-from test1-- name test3 image
If you delete mounted containers (including data, db1, and db2), the data volume is not automatically deleted. If you want to delete
Except for one data volume, you must use the docker rm-v command to specify when deleting the last container that still holds it
Delete the associated container at the same time. Use Data Volume Container to back up, restore and move data
Backup
Another function of data volumes is to use them to back up, restore, and move data. If you are using a data container, prepare
It is quite easy to share. Use the-- volume flag to create a new container with the volume loaded, with the following command:
This example should compress everything in Volume into a single tar package
[root@docker01] # docker run-- rm-- volumes-from data-v $(pwd): / backup centos:httpv1 tar cvf / backup/backup.tar / test
Here we create a container to mount the data volume from the data container. Then mount the current directory from the local host to
/ backup directory of the container. Finally, use the tar command to back up the data volume as backup.tar. When the command is executed
After the container stops, we back up the data data volume
Delete the container-rm after execution, and the backup is in the current directory, named backup.tar.
The backup file test.tar of test volume is generated under the current directory of the host.
Restore
Docker run-- volumes-from data-v / root/:/backup centos:httpv1 tar zxf / backup/backup.tar-C / test/
Attached:
Permissions and licenses
Usually you need to set the permissions of Volume or initialize some default data or configuration files for Volume. Pay attention to
The key point is that no instruction after the VOLUME instruction of Dockerfile can change the Volume, such as:
FROM debian:wheezy
RUN useradd foo
VOLUME / data
RUN touch / data/x
RUN chown-R foo:foo / data
The Docker file does not work as expected, and we had hoped that the touch command would run on the mirrored file system, but
Yes, it is actually running on the Volume of a temporary container. As follows:
FROM debian:wheezy
RUN useradd foo
RUN mkdir / data & & touch / data/x
RUN chown-R foo:foo / data
VOLUME / data
So, remember the location of the VOLUME instruction in Dockerfile (VOLUME is the setting instruction)
If you do not set permissions through the RUN directive, then you need to use CMD or ENTRYPOINT when the container starts
Instruction to execute
Delete Volumes
Volume can only be deleted under the following circumstances:
∙ docker rm-v added the-v option when deleting the container
For example, you can tell Docker to delete both the container and its Volume:
Docker rm-v my_container
∙ docker run-- rm added the-- rm option when running the container
Even with the above two commands, you can only delete Volume without container connections. Connect to the user-specified host directory
Volume will never be deleted by docker. Otherwise, you will get some in the / var/lib/docker/volumes directory
Zombie files and directories, and it's not easy to say what they represent.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.