Sometimes containers produce some data that we don't want to delete with the removal of the container.To ensure data security, it is commonly used in databases.
First, look at the differences between container s and image s:
container containers are created on top of images. container layer s can read and write data while images are read-only. The data we thank for in containers is limited to reading and writing in the current containers. If containers are stopped and deleted, all previously written files and data are deleted, so containers are temporarily stored and saved.Store data.
If there is a requirement that if we create a container for a mysql database, the data is stored locally as a file, and if we delete the mysql container, the data is deleted, which is not desirable.
docker introduces a mechanism for persistence.
By default, data is written to Container Layer, but we can store it to Volume through persistence.Even if the container is deleted, as long as the Volume is still there, our data is still there and saved.
Docker persistent data scheme:
- Volume based on local file system.You can use the -v parameter to use the host's directory as the data volume of the container when executing Docker create or Docker run.This part of the functionality is volume management based on the local file system.
- volume based on plugin.Supports third-party storage schemes, such as NAS, aws
Type of Volume:
- Managed data volume, automatically created by the docker background.
- Bind the Volume to be hung, and the specific location to be hung 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 886583d9191dd8df6deed609f31e9e15ed854f83a85b4b47c335b04a441b55b3 iie4bu@hostdocker:~$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-e MYSQL_ALLOW_EMPTY_PASSWORD indicates an empty password
Query Mirror Discovery is not running
View log: docker logs mysql-1
iie4bu@hostdocker:~$ docker logs mysql-1 error: 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 and recreate this image.
First check if we have volume locally?
iie4bu@hostdocker:~$ docker volume ls DRIVER VOLUME NAME local 01d091f0bc6a045d10d56b5892dc55ccb2c6cda6b19cbcc8ca3c7adc9b3c477b local 0e4b3a8c4be019d86c855a773865e5472dbc0629a7b542d44fe5d132be07ee87
I found a lot of them, all created before.You can delete them first:
docker volume rm 01d091f0bc6a045d10d56b5892dc55ccb2c6cda6b19cbcc8ca3c7adc9b3c477b
Tips:
You can delete all volume s:
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 48ea258e299a6c74f0762e2bd5ff580422160d02ab600d26aec716ca983fc90e iie4bu@hostdocker:~$
iie4bu@hostdocker:~$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48ea258e299a mysql "docker-entrypoint.s..." 37 seconds ago Up 36 seconds 3306/tcp, 33060/tcp mysql-1 iie4bu@hostdocker:~$
Now that you can see that mysql is running, look at volume:
iie4bu@hostdocker:~$ docker volume ls DRIVER VOLUME NAME local 575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d iie4bu@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" } ]
The data found for volume is in the path / var/lib/docker/volumes/575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d/_data.
Let's create another mysql:
iie4bu@hostdocker:~$ docker run -d --name mysql-2 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql 200afb2c335e45bd76ad9b3b8d97f29a23972702ae2c4f82be9ebde1d619b644 iie4bu@hostdocker:~$ docker volume ls DRIVER VOLUME NAME local 54bbb749bc0b45dfa1de4eff8b94620e3184ade9633ffdeaeef230dbcd20c0cc local 575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d
View 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, is the volume still there?
First stop mysql-1 and mysql-2:
iie4bu@hostdocker:~$ docker container stop mysql-1 mysql-2 mysql-1 mysql-2
Then delete mysql-1 and mysql-2:
iie4bu@hostdocker:~$ docker container rm mysql-1 mysql-2 mysql-1 mysql-2
Confirm that we have no containers running locally:
iie4bu@hostdocker:~$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES iie4bu@hostdocker:~$
But volume is still:
iie4bu@hostdocker:~$ docker volume ls DRIVER VOLUME NAME local 54bbb749bc0b45dfa1de4eff8b94620e3184ade9633ffdeaeef230dbcd20c0cc local 575c4038694e1abf8e883fc029813a745de6e5f412412eebec7c1a5b9c46e35d iie4bu@hostdocker:~$
Alias volume
First delete the two volume s above: