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 store data to Volume through persistence

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to store data to Volume through persistence, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Sometimes the container generates some data, and we don't want the data to be deleted as the container is deleted. To ensure the security of data, it is generally used in the database.

First, take a look at the difference between container and image:

The container container is created on top of image. Container layer can read and write data, while image is read-only. The data we thank in container is limited to reading and writing in the current container. If container is stopped and deleted, the previously written files and data are deleted, so container stores and stores data temporarily.

If there is such a requirement: if we create a container of a mysql database, the data is stored locally as a file, and if the mysql container is deleted, then the data will be deleted, which is not desirable.

Docker introduces a persistence mechanism.

By default, data is written to Container Layer, but we can store it to Volume through persistence. Even if the container is deleted, our data will be there and saved as long as the Volume is there.

Docker persistent data scheme:

Volume based on the local file system. You can use the host's directory as the data volume of the container with the-v parameter when executing Docker create or Docker run. This part of the function is volume management based on the local file system.

Volume based on plugin. Support third-party checkout solutions, such as NAS,aws

Type of Volume:

Managed data volume, automatically created by the docker backend.

Bind the Volume to which it is hung, and the specific location can be specified by the user.

Prepare the environment

Create a container for mysql

Iie4bu@hostdocker:~$ docker run-d-- name mysql-1-e MYSQL_ALLOW_EMPTY_PASSWORD mysql 886583d9191dd8df6deed609f31e9e15ed854f83a85b4b47c335b04a441b55b3iie4busty hostdockercharge $docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

-e MYSQL_ALLOW_EMPTY_PASSWORD means to use an empty password

Query the image found that it is not running

View the log: docker logs mysql-1

Iie4bu@hostdocker:~$ docker logs mysql-1error: database is uninitialized and password option is not specified You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

Delete the image and recreate it.

First, let's see if we have volume locally.

Iie4bu@hostdocker:~$ docker volume lsDRIVER VOLUME NAMElocal 01d091f0bc6a045d10d56b5892dc55ccb2c6cda6b19cbcc8ca3c7adc9b3c477blocal 0e4b3a8c4be019d86c855a773865e5472dbc0629a7b542d44fe5d132be07ee87

It is found that there are many, all of which were created before. You can delete it first:

Docker volume rm 01d091f0bc6a045d10d56b5892dc55ccb2c6cda6b19cbcc8ca3c7adc9b3c477b

Tips:

You can delete all volume:

Docker volume rm $(docker volume ls-Q)

Recreate the mysql image:

Iie4bu@hostdocker:~$ docker run-d-- name mysql-1-e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql 48ea258e299a6c74f0762e2bd5ff580422160d02ab600d26aec716ca983fc90eiie4busty hostdockerrace docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES48ea258e299a mysql docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES48ea258e299a mysql "docker-entrypoint.s …" 37 seconds ago Up 36 seconds 3306/tcp, 33060/tcp mysql-1iie4bu@hostdocker:~$

Now that you can see that mysql is running, look at volume:

Iie4bu@hostdocker:~$ docker volume lsDRIVER VOLUME NAMElocal 575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35diie4bugs hostdocker

This is mysql's volume.

View volume details:

Iie4bu@hostdocker:~$ docker volume inspect 575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d [{"CreatedAt": "2019-07-01T14:33:51+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/ var/lib/docker/volumes/575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d/_data", "Name": "575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d", "Options": {}, "Scope": "local"}]

It is found that the data for volume is under the / var/lib/docker/volumes/575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d/_data path.

Let's create another mysql:

Iie4bu@hostdocker:~$ docker run-d-- name mysql-2-e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql 200afb2c335e45bd76ad9b3b8d97f29a23972702ae2c4f82be9ebde1d619b644ie4busty hostdockercharge $docker volume lsDRIVER VOLUME NAMElocal 54bbb749bc0b45dfa1de4eff8b94620e3184ade9633ffdeaeef230dbcd20c0cclocal 575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d

Check out this new volume:

Iie4bu@hostdocker:~$ docker volume inspect 54bbb749bc0b45dfa1de4eff8b94620e3184ade9633ffdeaeef230dbcd20c0cc [{"CreatedAt": "2019-07-01T14:50:46+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/ var/lib/docker/volumes/54bbb749bc0b45dfa1de4eff8b94620e3184ade9633ffdeaeef230dbcd20c0cc/_data", "Name": "54bbb749bc0b45dfa1de4eff8b94620e3184ade9633ffdeaeef230dbcd20c0cc", "Options": {} "Scope": "local"}] experiment: if the container is stopped Whether the volume is still there:

Stop mysql-1 and mysql-2 first:

Iie4bu@hostdocker:~$ docker container stop mysql-1mysql- 2mysql-1mysql-2

Then delete mysql-1 and mysql-2:

Iie4bu@hostdocker:~$ docker container rm mysql-1mysql- 2mysql-1mysql-2

Confirm that we do not have a container running locally:

Iie4bu@hostdocker:~$ docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESiie4bu@hostdocker:~$

But volume is still there:

Iie4bu@hostdocker:~$ docker volume lsDRIVER VOLUME NAMElocal 54bbb749bc0b45dfa1de4eff8b94620e3184ade9633ffdeaeef230dbcd20c0cclocal 575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35diie4busty hostdocker $alias to volume for easy use in the future

Delete the above two volume first:

Docker volume rm 54bbb749bc0b45dfa1de4eff8b94620e3184ade9633ffdeaeef230dbcd20c0ccdocker volume rm 575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d

Create a new container for mysql and set the-v option:

Docker run-d-v mysql:/var/lib/mysql-- name mysql-1-e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql7a4ec8f6b8409c7877683d7cd7d2ce3bc85e029aab561b36aea3970a2e8ddcdb

-v means that the local volume is mysql, and the mapping to the container is / var/lib/mysql. Check volume:

Docker volume lsDRIVER VOLUME NAMElocal mysql

Enter inside the mysql-1 container:

Docker exec-it mysql-1 / bin/bashroot@7a4ec8f6b840:/# mysql- u rootWelcome to the MySQL monitor. Commands end with; or\ g.Your MySQL connection id is 8Server version: 8.0.16 MySQL Community Server-GPLCopyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or'\ h' for help. Type'\ c' to clear the current input statement.mysql > show databases;+-+ | Database | +-+ | information_schema | | mysql | | performance_schema | | sys | +-+ 4 rows in set (0.00 sec) mysql > create database docker Query OK, 1 row affected (0.01sec)

Added a database called docker

Then exit the container, stop the mysql-1 container, and delete:

Docker container stop mysql-1mysql-1docker container rm mysql-1mysql-1

But we can still see the volume of mysql using docker volume ls

Docker volume lsDRIVER VOLUME NAMElocal mysql

Let's create a container mysql-2 and use this volume:

Docker run-d-v mysql:/var/lib/mysql-- name mysql-2-e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysqlfb66669da26a8ada40b276e3ae972e8597d911822ef6e4ea90fed211c46eeb1e

Then we go into the mysql-2 container and look at the database:

Docker exec-it mysql-2 / bin/bashroot@fb66669da26a:/# mysql- uroot-pEnter password:Welcome to the MySQL monitor. Commands end with; or\ g.Your MySQL connection id is 8Server version: 8.0.16 MySQL Community Server-GPLCopyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or'\ h' for help. Type'\ c' to clear the current input statement.mysql > show databases;+-+ | Database | +-+ | docker | | information_schema | | mysql | | performance_schema | | sys | +-+ 5 rows in set (0.01 sec)

It is found that the docker database is still there, which means it is the same database as the previous mysql-1 database.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

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

12
Report