docker容器卷技术讲解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker容器卷技术讲解相关的知识,希望对你有一定的参考价值。
参考技术A docker 容器中产生的数据,同步到本地,卷基础;目录的挂载,容器的持久化,和同步操作,容器间可以数据共享。
直接使用命令挂载。
-v
docker run -it -v -p 00:00#主机目录:容器内目录
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
597e67e6ca0f centos "/bin/bash" 3 minutes ago Up 2 minutes tender_brahmagupta
[root@localhost home]# docker inspect 597e67e6ca0f
[
"Id": "597e67e6ca0f15b8c832fd2513b7d672d17763d3a19abb75be30fed726d7ed48",
"Created": "2022-01-03T08:31:46.904882934Z",
"Path": "/bin/bash",
"Args": [],
"State":
,
"Name": "overlay2"
,
"Mounts": [
"Type": "bind",
"Source": "/home/ceshi", 主机目录
"Destination": "/home", 主机目录
"Mode": "",
"RW": true,
"Propagation": "rprivate"
[root@localhost ~]# docker run -it -v /home/ceshi:/home centos /bin/bash
#[root@localhost ~]# cd /home/ 再打开一个端口查看
#[root@localhost home]# ls
#ceshi kuangshen.java shitou test.java
[root@597e67e6ca0f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@597e67e6ca0f /]# cd /home/
[root@597e67e6ca0f home]# ls
[root@597e67e6ca0f home]# mkdir hi
[root@597e67e6ca0f home]# ls
hi
#在另一台上查看
[root@localhost home]# ls
ceshi kuangshen.java shitou test.java
[root@localhost home]# ls ceshi/
hi
宿主机/home/ceshi,和容器中的/home是同步的双向绑定 容器停掉后也会同步的。开启容器后,查看,也能查看到与主机是同步的
docker下安装mysql
安装mysql docker
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11903 [OK]
mariadb MariaDB Server is a high performing open sou… 4555 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 892 [OK]
percona Percona Server is a fork of the MySQL relati… 566 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 409 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 90
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 54
prom/mysqld-exporter 46 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 31 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 23
centos/mysql-56-centos7 MySQL 5.6 SQL database server 21
arey/mysql-client Run a MySQL client from a docker container 20 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 18 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
idoall/mysql MySQL is a widely used, open-source relation… 3 [OK]
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 3 [OK]
jelastic/mysql An image of the MySQL database server mainta… 2
centos/mysql-80-centos7 MySQL 8.0 SQL database server 2
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
获取镜像。
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Extracting [==================================================>] 1.733kB/1.733kB
99da31dd6142: Download complete
626033c43d70: Download complete
37d5d7efb64e: Download complete
ac563158d721: Download complete
d2ba16033dad: Download complete
0ceb82207cd7: Download complete
37f2405cae96: Downloading [==========================================> ] 91.87MB/108.6MB
e2482e017e53: Download complete
#官方测试,docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:TAG
[root@localhost ~]#docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/my.cnf/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456789 --name mysql01 mysql:5.7
f12f68f1ea0eb8c767cce17de8b5bf841244f0ee729ee30d069341829ae329ef
-d后台运行,-p 端口映射,-v卷挂载,同步数据,-e 环境配置 --name 名字,名字后面一定要跟版本号。
docker技术篇-数据卷管理
用户在使用Docker的过程中,往往会涉及到数据的备份,容器间数据共享,在此我们介绍一下容器的数据管理操作。
数据卷
数据卷是一个可供容器使用的特殊目录,使用类似于linux对目录或者文件进行操作。
数据卷可以在容器之间共享
对数据卷的修改会马上生效
对数据卷的更新,不会影响镜像
卷会一直存在,直到没有容器使用
[root@localhost ~]# docker run -d -it -P --name aaa -v /chao chao
6bda302f62f636281499e5e08d76353b934b3af1132ef85189bdfb35b729d96c
[root@localhost ~]# docker attach aaa
[root@6bda302f62f6 /]# ls
bin chao dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
例如我们要把本机的chaols目录分享到容器的/chao目录下
[root@localhost ~]# mkdir chaols
[root@localhost ~]# cd chaols/
[root@localhost chaols]# touch a b c
[root@localhost chaols]# cd
[root@localhost ~]# docker run -v /root/chaols:/chao -it chao
[root@4c214961267f /]# ls
bin chao dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@4c214961267f /]# ls /chao/
a b c
数据卷容器
用户如果需要在容器之间共享一些数据,此时就可以用数据卷容器,数据卷容器就是一个普通容器,就是用它来存放共享的数据。
现在我们创建chao1 chao2 在aaa容器中挂载
[root@localhost ~]# docker run -it --volumes-from aaa --name chao1 chao
[root@b26d0044b084 /]# ls
bin chao dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@b26d0044b084 /]# cd chao/
[root@b26d0044b084 chao]# ls
[root@b26d0044b084 chao]# cd ..
[root@b26d0044b084 /]# [root@localhost ~]#
[root@localhost ~]# docker run -it --volumes-from aaa --name chao2 chao
[root@a8ade4de8d12 /]# ls
bin chao dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@a8ade4de8d12 /]# cd chao/
[root@a8ade4de8d12 chao]# ls
[root@a8ade4de8d12 chao]# mkdir aaa
[root@a8ade4de8d12 chao]# [root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8ade4de8d12 chao "/bin/bash" 17 seconds ago Up 16 seconds chao2
b26d0044b084 chao "/bin/bash" 44 seconds ago Up 42 seconds chao1
4c214961267f chao "/bin/bash" 7 minutes ago Up 7 minutes serene_bose
d1d444fb5fa4 chao "/bin/bash" 9 minutes ago Created zealous_jones
286446231b22 chao "/bin/bash" 10 minutes ago Created fervent_shirley
6bda302f62f6 chao "/bin/bash" 14 minutes ago Up 14 minutes aaa
[root@localhost ~]# docker attach a8ade4de8d12
[root@a8ade4de8d12 chao]# ls
aaa
[root@a8ade4de8d12 chao]# [root@localhost ~]#
[root@localhost ~]# docker attach b26d0044b084
[root@b26d0044b084 /]# ls
bin chao dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@b26d0044b084 /]# ls /chao/
aaa
由此可见他们都有chao目录并且里面数据一样。
END
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604922
Linux技术交流群:193666693
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
以上是关于docker容器卷技术讲解的主要内容,如果未能解决你的问题,请参考以下文章