docker(十二):docker持久化
程序员文章站
2022-03-03 07:58:47
...
一、实现功能
当关闭docker容器时,其内部的文件会消失。然后重新启动docker,则原始保存文件会丢失。所以,需要做一定的持久化,即第一次关闭的时候,把文件保存到本地磁盘,进而第二次打开docker容器,可以获得之前保存的数据,不至于丢失。
二、docker volume持久化实现
1.运行mysql容器
[[email protected] ~]# sudo docker run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
cfb5287addde854587c74311788c3c40846f1b68c269e48ce490bb7a9ca094db
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cfb5287addde mysql "docker-entrypoint.s…" 5 seconds ago Up 5 seconds 3306/tcp, 33060/tcp mysql1
2.查看生成volume具体信息
(1)查看生成volume
[[email protected] ~]# sudo docker volume ls
DRIVER VOLUME NAME
local 715be444d8a88c937f0c9ff47147b445a3144069fa94db9bb85129da6ad496a3
(2)查看volume具体细节信息
sudo docker volume inspect [volume name] (上一步骤)
实例
[[email protected] ~]# sudo docker volume inspect 715be444d8a88c937f0c9ff47147b445a3144069fa94db9bb85129da6ad496a3
[
{
"CreatedAt": "2019-06-28T21:39:18Z",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/715be444d8a88c937f0c9ff47147b445a3144069fa94db9bb85129da6ad496a3/_data",
"Name": "715be444d8a88c937f0c9ff47147b445a3144069fa94db9bb85129da6ad496a3",
"Options": null,
"Scope": "local"
}
]
3.创建第二个mysql容器mysql2
sudo docker run -d --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
4.查看
(1)查看生成volume
[[email protected] ~]# docker volume ls
DRIVER VOLUME NAME
local 715be444d8a88c937f0c9ff47147b445a3144069fa94db9bb85129da6ad496a3
local 76863a4ee8f28b072fa2a6fe6fc334522af7e694e9aca98cfa16862629bae938
(2)查看volume具体细节信息
[[email protected] ~]# docker volume inspect 76863a4ee8f28b072fa2a6fe6fc334522af7e694e9aca98cfa16862629bae938
[
{
"CreatedAt": "2019-06-28T21:45:54Z",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/76863a4ee8f28b072fa2a6fe6fc334522af7e694e9aca98cfa16862629bae938/_data",
"Name": "76863a4ee8f28b072fa2a6fe6fc334522af7e694e9aca98cfa16862629bae938",
"Options": null,
"Scope": "local"
}
]
5.关闭两个容器,volume不会删除
sudo docker stop mysql1 mysql2
sudo docker rm mysql1 mysql2
查看
sudo docker ps -a
查看volume,依旧存在
[[email protected] ~]# sudo docker volume ls
DRIVER VOLUME NAME
local 715be444d8a88c937f0c9ff47147b445a3144069fa94db9bb85129da6ad496a3
local 76863a4ee8f28b072fa2a6fe6fc334522af7e694e9aca98cfa16862629bae938
6.删除刚刚新建的volume
sudo docker volume rm [volume name]
查看
[[email protected] ~]# docker volume ls
DRIVER VOLUME NAME
7.重新创建mysql容器,赋予volume简单名称
ps:mysql产生数据都会产生到这个目录内
sudo docker run -d -v mysql:/var/lib/mysql --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
查看volume
sudo docker volume ls
[[email protected] labs]# sudo docker volume ls
DRIVER VOLUME NAME
local mysql
8.验证持久化
(1)写入数据到mysql1容器中,创建数据库docker
sudo docker exec -it mysql1 /bin/bash
输入
mysql -u root
创建database
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.06 sec)
mysql> create database docker;
Query OK, 1 row affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| docker |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
(2)删除mysql1,创建mysql2
[[email protected] labs]# docker rm -f mysql1
mysql1
创建
sudo docker run -d -v mysql:/var/lib/mysql --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
进入
sudo docker exec -it mysql2 /bin/bash
查看
输入
mysql -u root
创建database
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| docker |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.04 sec)
三、bind mounting持久化实现
1.实现原理
即实现docker宿主虚拟机的目录和docker目录同步
2.进入目录,依据Dockerfile新建image
(1)找到Dockerfile位置
cd /home/vagrant/labs/labs/labs/docker-nginx
(2)Dockerfile
# this same shows how we can extend/change an existing official image from Docker Hub
FROM nginx:latest
# highly recommend you always pin versions for anything beyond dev/learn
WORKDIR /usr/share/nginx/html
# change working directory to root of nginx webhost
# using WORKDIR is prefered to using 'RUN cd /some/path'
COPY index.html index.html
# I don't have to specify EXPOSE or CMD because they're in my FROM
(3)新建image
docker build -t nginx:latest ./
3.运行nginx
docker run -d -v $(pwd):/usr/share/nginx/html -p 80:80 --name web nginx
4.进入容器
docker exec -it web /bin/bash
新建文件,然后退出
touch test2.txt
exit
5.查看docker宿主机上对应目录会同步修改文件
[[email protected] docker-nginx]$ pwd
/home/vagrant/labs/labs/labs/docker-nginx
[[email protected] docker-nginx]$ ls
Dockerfile index.html test2.txt
6.在宿主机上修改test2.txt,进入docker查看
docker exec -it web /bin/bash
依旧会有修改内容,即二者是同步的
vi test2.txt
四、持久化在生产中应用
1.在目录下,新建image
[[email protected] flask-skeleton]# pwd
/home/vagrant/labs/labs/labs/flask-skeleton
docker build -t flask-skeleton:latest ./
2.运行
docker run -d -p 80:5000 --name flask flask-skeleton
3.查看网页
192.168.205.10:80
4.同步目录本地和容器中目录
docker run -d -p 80:5000 -v $(pwd):/skeleton --name flask flask-skeleton
5.打开浏览器
192.168.205.10:80
6.打开linux源码
修改skeleton/client/template/main/home.html
修改内容:
welcome为
welcome to dockerworld!
重新查看网页