人们如何处理Docker容器的持久存储?
我目前正在使用这种方法:构建映像,例如PostgreSQL,然后启动容器
docker run --volumes-from c0dbc34fd631 -d app_name/postgres
恕我直言,这有缺点,我不能(意外地)删除容器“c0dbc34fd631”。
另一个想法是将主机卷“-v”挂载到容器中,然而,容器中的用户id不一定与来自主机的用户id匹配,然后权限可能会被打乱。
注意:除了——volumes-from 'cryptic_id',你还可以使用——volumes-from my-data-container,其中my-data-container是你分配给仅数据容器的名称,例如docker run——name my-data-container…(见公认答案)
根据您的需要,管理持久数据有几个级别:
Store it on your host
Use the flag -v host-path:container-path to persist container directory data to a host directory.
Backups/restores happen by running a backup/restore container (such as tutumcloud/dockup) mounted to the same directory.
Create a data container and mount its volumes to your application container
Create a container that exports a data volume, use --volumes-from to mount that data into your application container.
Backup/restore the same as the above solution.
Use a Docker volume plugin that backs an external/third-party service
Docker volume plugins allow your datasource to come from anywhere - NFS, AWS (S3, EFS, and EBS)
Depending on the plugin/service, you can attach single or multiple containers to a single volume.
Depending on the service, backups/restores may be automated for you.
While this can be cumbersome to do manually, some orchestration solutions - such as Rancher - have it baked in and simple to use.
Convoy is the easiest solution for doing this manually.
要保存或存储数据库数据,请确保docker-compose。Yml看起来就像
如果你想使用Dockerfile
version: '3.1'
services:
php:
build:
context: .
dockerfile: Dockerfile
ports:
- 80:80
volumes:
- ./src:/var/www/html/
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- mysql-data:/var/lib/mysql
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
mysql-data:
你的docker-compose。Yml将看起来像
如果您想使用您的映像而不是Dockerfile
version: '3.1'
services:
php:
image: php:7.4-apache
ports:
- 80:80
volumes:
- ./src:/var/www/html/
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- mysql-data:/var/lib/mysql
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
如果你想存储或保存mysql的数据,那么
必须记得在docker-compose.yml中添加两行
volumes:
- mysql-data:/var/lib/mysql
and
volumes:
mysql-data:
之后使用此命令
docker-compose up -d
现在您的数据将被持久化,即使在使用此命令后也不会被删除
docker-compose down
额外:-但如果你想删除所有的数据,那么你将使用
docker-compose down -v
此外,您还可以使用此命令检查数据库数据列表
docker volume ls
DRIVER VOLUME NAME
local 35c819179d883cf8a4355ae2ce391844fcaa534cb71dc9a3fd5c6a4ed862b0d4
local 133db2cc48919575fc35457d104cb126b1e7eb3792b8e69249c1cfd20826aac4
local 483d7b8fe09d9e96b483295c6e7e4a9d58443b2321e0862818159ba8cf0e1d39
local 725aa19ad0e864688788576c5f46e1f62dfc8cdf154f243d68fa186da04bc5ec
local de265ce8fc271fc0ae49850650f9d3bf0492b6f58162698c26fce35694e6231c
local phphelloworld_mysql-data