数据卷(data volume)
数据卷特点和使用
数据卷实际上就是宿主机上的目录或者是文件,可以被直接mount到容器当中使用
实际生成环境中,需要针对不同类型的服务、不同类型的数据存储要求做相应的规划,最终保证服务的可扩展性、稳定性以及数据的安全性
数据卷使用场景
-日志输出
-静态web页面
-应用配置文件
-多容器间目录或文件共享
数据卷的特点
-数据卷是目录或者文件,并且可以在多个容器之间共同使用
-对数据卷更改数据在所有容器里面会立即更新。
-数据卷的数据可以持久保存,即使删除使用使用该容器卷的容器也不影响。
-在容器里面的写入数据不会影响到镜像本身
-依赖于宿主机目录,宿主机出问题,上面容器会受影响,当宿主机较多时,不方便统一管理
数据卷使用方法
启动容器时,指定使用数据卷
docker run 命令的以下选项可以实现数据卷
-v, --volume list Bind mount a volume
格式:
-v <宿主机目录或文件>:<容器目录或文件>[:ro] #将宿主机目录挂载容器目录,两个目录都可自动创建
说明:
:ro 此项为只读,不写此项默认为可读可写
docker rm 选项可以删除容器时,删除相关联的卷
-v, --volumes Remove the volumes associated with the container
实战案例:目录数据卷
在宿主机创建容器所使用的目录
[root@ubuntu1804 ~]#mkdir /data/testdir
[root@ubuntu1804 ~]#echo Test page on host > /data/testdir/index.html
查看容器相关目录路径
[root@ubuntu1804 ~]#docker images "*nginx*"
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-ubuntu1804 1.16.1 19efdd23ac87 2 days ago 378MB
nginx-alpine 1.16.1 978a43bbb61d 2 days ago 211MB
[root@ubuntu1804 ~]#docker run -it --rm nginx-alpine:1.16.1 sh
/ # cat /apps/nginx/conf/nginx.conf
...
server {
...
location / {
root /data/nginx/html; #nginx存放网页文件的路径
index index.html index.htm;
}
...
/ # cat /data/nginx/html/index.html
Test Page based nginx-alpine
/ # exit
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
引用宿主机的数据卷启动容器
引用数据卷目录,开启多个容器
[root@ubuntu1804 ~]#docker run -d -v /data/testdir:/data/nginx/html/ -p 8001:80 nginx-alpine:1.16.1
56a5460f584bd2de56040c4a1dff86ad8a9723cfd6bf21ed8a538b9629b0874c
[root@ubuntu1804 ~]#docker run -d -v /data/testdir:/data/nginx/html/ -p 8002:80 nginx-alpine:1.16.1
e7b5bff6ce56fa51ed6411175c9c9f1fb9bf8e7b1b9471080380b01692f89e58
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7b5bff6ce56 nginx-alpine:1.16.1 "nginx" 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:8002->80/tcp hungry_robinson
56a5460f584b nginx-alpine:1.16.1 "nginx" 33 seconds ago Up 31 seconds 443/tcp, 0.0.0.0:8001->80/tcp stupefied_dubinsky
[root@ubuntu1804 ~]#curl 127.0.0.1:8001
Test page on host
[root@ubuntu1804 ~]#curl 127.0.0.1:8002
Test page on host
进入到容器内测试写入数据
进入其中一个容器写入数据,可以其它容器的数据也变化
[root@ubuntu1804 ~]#docker exec -it e7b5bff6ce56 sh
/ # df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 47799020 5294492 40046724 12% /
tmpfs 65536 0 65536 0% /dev
tmpfs 492552 0 492552 0% /sys/fs/cgroup
shm 65536 0 65536 0% /dev/shm
/dev/sda2 47799020 5294492 40046724 12% /etc/resolv.conf
/dev/sda2 47799020 5294492 40046724 12% /etc/hostname
/dev/sda2 47799020 5294492 40046724 12% /etc/hosts
/dev/sda3 19091540 3958732 14139940 22% /data/nginx/html
tmpfs 492552 0 492552 0% /proc/asound
tmpfs 492552 0 492552 0% /proc/acpi
tmpfs 65536 0 65536 0% /proc/kcore
tmpfs 65536 0 65536 0% /proc/keys
tmpfs 65536 0 65536 0% /proc/timer_list
tmpfs 65536 0 65536 0% /proc/sched_debug
tmpfs 492552 0 492552 0% /proc/scsi
tmpfs 492552 0 492552 0% /sys/firmware
/ # cat /data/nginx/html/index.html
Test page on host
/ # echo Test page v2 on host > /data/nginx/html/index.html
#进入另一个容器看到数据变化
[root@ubuntu1804 ~]#docker exec -it 56a5460f584b sh
/ # cat /data/nginx/html/index.html
Test page v2 on host
#访问应用
[root@ubuntu1804 ~]#curl 127.0.0.1:8001
Test page v2 on host
[root@ubuntu1804 ~]#curl 127.0.0.1:8002
Test page v2 on host
在宿主机修改数据
[root@ubuntu1804 ~]#echo Test page v3 on host > /data/testdir/index.html
[root@ubuntu1804 ~]#cat /data/testdir/index.html
Test page v3 on host
[root@ubuntu1804 ~]#curl 127.0.0.1:8001
Test page v3 on host
[root@ubuntu1804 ~]#curl 127.0.0.1:8002
Test page v3 on host
[root@ubuntu1804 ~]#docker exec -it e7b5bff6ce56 sh
/ # cat /data/nginx/html/index.html
Test page v3 on host
[root@ubuntu1804 ~]#docker exec -it 56a5460f584b sh
/ # cat /data/nginx/html/index.html
Test page v3 on host
只读方法挂载数据卷
默认数据卷为可读可写,加ro选项,可以实现只读挂载,对于不希望容器修改的数据,比如:配置文件,脚本等,可以用此方式挂载
[root@ubuntu1804 ~]#docker run -d -v /data/testdir:/data/nginx/html/:ro -p 8004:80 nginx-alpine:1.16.1
727d3ecf65c5a79bd9a11033812dc01619c4c45bd25af5155f904016f5f0c45a
[root@ubuntu1804 ~]#docker exec -it 727d3ecf65c5a79bd9 sh
/ # cat /data/nginx/html/index.html
Test page v3 on host
/ # echo Test page v4 on host > /data/nginx/html/index.html
sh: can't create /data/nginx/html/index.html: Read-only file system
/ # cat /data/nginx/html/index.html
删除容器
删除容器后,宿主机的数据卷还存在,可继续给新的容器使用
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7b5bff6ce56 nginx-alpine:1.16.1 "nginx" 9 minutes ago Up 9 minutes 443/tcp, 0.0.0.0:8002->80/tcp hungry_robinson
56a5460f584b nginx-alpine:1.16.1 "nginx" 9 minutes ago Up 9 minutes 443/tcp, 0.0.0.0:8001->80/tcp stupefied_dubinsky
[root@ubuntu1804 ~]#docker rm -f docker ps -aq
e7b5bff6ce56
56a5460f584b
[root@ubuntu1804 ~]#cat /data/testdir/index.html
Test page v3 on host
#新建的容器还可以继续使用原有的数据卷
[root@ubuntu1804 ~]#docker run -d -v /data/testdir:/data/nginx/html/ -p 8003:80 nginx-alpine:1.16.1
ecd016506f6af3f4af61dbd869f8fce5f634ecdc0e3272f9e0402c981acd80a4
[root@ubuntu1804 ~]#curl 127.0.0.1:8003
Test page v3 on host
MySQL使用的数据卷
[root@ubuntu1804 ~]#docker pull mysql:5.7.29
5.7.29: Pulling from library/mysql
Status: Downloaded newer image for mysql:5.7.29
docker.io/library/mysql:5.7.29
[root@ubuntu1804 ~]#docker images "mysql*"
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.29 b598110d0fff 2 weeks ago 435MB
[root@ubuntu1804 ~]#docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.29
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c21ca6f8a7fe mysql:5.7.29 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp nifty_banach
[root@ubuntu1804 ~]#docker exec -it c21ca6f8a7fe bash
root@c21ca6f8a7fe:/# cat /etc/issue
Debian GNU/Linux 9 /n /l
root@c21ca6f8a7fe:/# cat /etc/mysql/
conf.d/ my.cnf my.cnf.fallback mysql.cnf mysql.conf.d/
root@c21ca6f8a7fe:/# cat /etc/mysql/my.cnf
......
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
root@8c39824a5ace:/# cat /etc/mysql/conf.d/
docker.cnf mysql.cnf mysqldump.cnf
root@c21ca6f8a7fe:/# cat /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql #数据存放路径
root@c21ca6f8a7fe:/# pstree -p
mysqld(1)-+-{mysqld}(130)
|-{mysqld}(131)
|-{mysqld}(132)
|-{mysqld}(133)
|-{mysqld}(134)
|-{mysqld}(135)
|-{mysqld}(136)
|-{mysqld}(137)
|-{mysqld}(138)
|-{mysqld}(139)
|-{mysqld}(140)
|-{mysqld}(141)
|-{mysqld}(143)
|-{mysqld}(144)
|-{mysqld}(145)
|-{mysqld}(146)
|-{mysqld}(147)
|-{mysqld}(148)
|-{mysqld}(149)
|-{mysqld}(150)
|-{mysqld}(151)
|-{mysqld}(152)
|-{mysqld}(153)
|-{mysqld}(154)
|-{mysqld}(155)
`-{mysqld}(156)
[root@ubuntu1804 ~]#mysql -uroot -p123456 -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
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 dockerdb;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dockerdb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql>
#删除容器后,再创建新的容器,数据库信息丢失
[root@ubuntu1804 ~]#docker rm -f c21ca6f8a7fe
c21ca6f8a7fe
[root@ubuntu1804 ~]#docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.29
f52a50c7f80ee39d9a935a762eacb05db72dcfa5f0d02af8b4f23b5538080b67
[root@ubuntu1804 ~]#mysql -uroot -p123456 -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
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)
#利用数据卷创建容器
[root@ubuntu1804 ~]#mkdir /data/mysql
[root@ubuntu1804 ~]#docker run -d --name mysql -p 3306:3306 -v /data/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.29
d64ef3f9a64491061132020306fd3e97e1aa361b0fb9f6f644f2a1e3f334119c
[root@ubuntu1804 ~]#mysql -uroot -p123456 -h127.0.0.1
mysql> create database dockerdb;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dockerdb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit
#删除容器,数据存放在数据卷中,不会删除
[root@ubuntu1804 ~]#docker rm -fv mysql
mysql
[root@ubuntu1804 ~]#ls /data/mysql/
auto.cnf ca.pem client-key.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
ca-key.pem client-cert.pem dockerdb ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
#重新创建新容器,之前数据还在
[root@ubuntu1804 ~]#docker run -d --name mysql -p 3306:3306 -v /data/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.29
8d6cd0822cc61db30c0401992d2dbfa5c33b525f9aead44c165e0e247c37e5df
[root@ubuntu1804 ~]#mysql -uroot -p123456 -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dockerdb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit
实战案例:文件数据卷
文件挂载用于很少更改文件内容的场景,比如nginx 的配置文件、tomcat的配置文件等。
准备相关文件
[root@ubuntu1804 ~]#mkdir /data/{bin,testapp,logs}
[root@ubuntu1804 ~]#echo testapp v1 > /data/testapp/index.html
[root@ubuntu1804 ~]#cat /data/testapp/index.html
testapp v1
[root@ubuntu1804 ~]#cp /data/dockerfile/web/tomcat/tomcat-base-8.5.50/apache-tomcat-8.5.50/bin/catalina.sh /data/bin/
[root@ubuntu1804 ~]#vim /data/bin/catalina.sh
#加下面tomcat的优化参数行
# -----------------------------------------------------------------------------
JAVA_OPTS="-server -Xms4g -Xmx4g -Xss512k -Xmn1g -XX:CMSInitiatingOccupancyFraction=65 -XX:+UseFastAccessorMethods -XX:+AggressiveOpts
-XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10 -XX:NewSize=2048M -XX:MaxNewSize=2048M -XX:NewRatio=2 -XX:Pe
rmSize=128m -XX:MaxPermSize=512m -XX:CMSFullGCsBeforeCompaction=5 -XX:+ExplicitGCInvokesConcurrent -XX:+UseConcMarkSweepGC -XX:+UsePar
NewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods"
# OS specific support. $var _must_ be set to either true or false.
[root@ubuntu1804 ~]#chown 2019:2019 /data/bin/catalina.sh
[root@ubuntu1804 ~]#ll /data/bin/catalina.sh
-rwxr-x--- 1 2019 2019 24324 Jan 31 21:43 /data/bin/catalina.sh*
[root@ubuntu1804 ~]#chown 2019:2019 /data/logs/
引用文件数据卷启动容器
同时挂载可读可写方式的目录数据卷和只读方式的文件数据卷,实现三个数据卷的挂载,数据,日志和启动脚本
[root@ubuntu1804 ~]#docker run -d -v /data/bin/catalina.sh:/apps/tomcat/bin/catalina.sh:ro -v /data/testapp:/data/tomcat/webapps/testapp -v /data/logs:/apps/tomcat/logs -p 8080:8080 tomcat-web:app1
55de76261c5e489de9a24d5533fe630d44aa7cce821627f6503a91c041c8f8d3
验证容器可以访问
[root@ubuntu1804 ~]#curl 127.0.0.1:8080/testapp/
testapp v1
[root@ubuntu1804 ~]#ls -l /data/logs/
total 36
drwxr-xr-x 2 2019 2019 4096 Jan 31 22:44 ./
drwxr-xr-x 7 root root 4096 Jan 31 22:43 ../
-rw-r----- 1 2019 2019 8336 Jan 31 22:44 catalina.2020-01-31.log
-rw-r----- 1 2019 2019 8808 Jan 31 22:44 catalina.out
-rw-r----- 1 2019 2019 0 Jan 31 22:44 host-manager.2020-01-31.log
-rw-r----- 1 2019 2019 0 Jan 31 22:44 localhost.2020-01-31.log
-rw-r----- 1 2019 2019 76 Jan 31 22:44 localhost_access_log.2020-01-31.txt
-rw-r----- 1 2019 2019 0 Jan 31 22:44 manager.2020-01-31.log
直接修改宿主机的数据
#宿主机修改目录数据卷
[root@ubuntu1804 ~]#echo testapp v2 > /data/testapp/index.html
[root@ubuntu1804 ~]#curl 127.0.0.1:8080/testapp/
testapp v2
[root@ubuntu1804 ~]#ll /data/bin/catalina.sh
-rwxr-x--- 1 2019 2019 24324 Jan 31 21:43 /data/bin/catalina.sh*
[root@ubuntu1804 ~]#echo >> /data/bin/catalina.sh
[root@ubuntu1804 ~]#ll /data/bin/catalina.sh
-rwxr-x--- 1 2019 2019 24325 Jan 31 22:21 /data/bin/catalina.sh*
进入容器修改数据
[root@ubuntu1804 ~]#docker exec -it 55de76261c5e bash
[root@55de76261c5e /]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
#文件数据卷上的文件为只读
[root@55de76261c5e /]# echo >> /apps/tomcat/bin/catalina.sh
bash: /apps/tomcat/bin/catalina.sh: Read-only file system
#目录数据卷可读可写
[root@55de76261c5e /]# cat /data/tomcat/webapps/testapp/index.html
testapp v2
[root@55de76261c5e /]# echo testapp v3 > /data/tomcat/webapps/testapp/index.html
[root@55de76261c5e /]# cat /data/tomcat/webapps/testapp/index.html
testapp v3
[root@ubuntu1804 ~]#curl 127.0.0.1:8080/testapp/
testapp v3
查看容器中挂载和进程信息
[root@55de76261c5e /]# mount
......
/dev/sda3 on /apps/apache-tomcat-8.5.50/bin/catalina.sh type ext4 (ro,relatime,data=ordered)
/dev/sda3 on /data/tomcat/webapps/testapp type ext4 (rw,relatime,data=ordered)
......
[root@55de76261c5e /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 47799020 5295032 40046184 12% /
tmpfs 65536 0 65536 0% /dev
tmpfs 492552 0 492552 0% /sys/fs/cgroup
shm 65536 0 65536 0% /dev/shm
/dev/sda2 47799020 5295032 40046184 12% /etc/hosts
/dev/sda3 19091540 3974908 14123764 22% /data/tomcat/webapps/testapp
tmpfs 492552 0 492552 0% /proc/asound
tmpfs 492552 0 492552 0% /proc/acpi
tmpfs 492552 0 492552 0% /proc/scsi
tmpfs 492552 0 492552 0% /sys/firmware
[root@55de76261c5e /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 15136 3040 ? Ss 22:09 0:00 /bin/bash /apps/tomcat/bin/run_tomcat.sh
www 25 0.5 12.7 6253760 125268 ? Sl 22:09 0:04 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/apps/tomcat
root 26 0.0 0.4 85428 4468 ? S 22:09 0:00 su - www -c tail -f /etc/hosts
www 28 0.0 0.0 4416 716 ? Ss 22:09 0:00 tail -f /etc/hosts
root 85 0.0 0.4 15800 3980 pts/0 Ss 22:16 0:00 bash
root 109 0.0 0.3 55196 3696 pts/0 R+ 22:25 0:00 ps aux
[root@55de76261c5e /]# ps aux|grep java
www 25 0.5 12.7 6253760 125268 ? Sl 22:09 0:04 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/apps/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms4g -Xmx4g -Xss512k -Xmn1g -XX:CMSInitiatingOccupancyFraction=65 -XX:+UseFastAccessorMethods -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10 -XX:NewSize=2048M -XX:MaxNewSize=2048M -XX:NewRatio=2 -XX:PermSize=128m -XX:MaxPermSize=512m -XX:CMSFullGCsBeforeCompaction=5 -XX:+ExplicitGCInvokesConcurrent -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/apps/tomcat -Dcatalina.home=/apps/tomcat -Djava.io.tmpdir=/apps/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 111 0.0 0.2 12536 2320 pts/0 S+ 22:25 0:00 grep --color=auto java
本文链接:http://www.yunweipai.com/34865.html
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52673.html