只读层
叠加而成,启动容器时,docker会加载只读镜像层,并在镜像层添加一个读写层
COW(写时复制)
机制
目录
,此类目录可以绕过联合文件系统,与宿主机上的某个目录可以绑定(关联)
[root@centos7-node1 ~]# docker run --name mybbox5 -it -v /mydata --rm busybox /bin/sh #指定容器卷,容器中不存在则自动创建
/ # cd /mydata/
[root@centos7-node1 ~]# docker inspect mybbox5 # 另起窗口查看挂载
"Mounts": [
{
"Type": "volume",
"Name": "a39b4a68fc51d83900093f35b05b4e29085b2d45d1fa9a19f63f75130dbf1d0c",
"Source": "/data/docker/volumes/a39b4a68fc51d83900093f35b05b4e29085b2d45d1fa9a19f63f75130dbf1d0c/_data",
"Destination": "/mydata",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
[root@centos7-node1 ~]# mkdir /data/volumes/v1 -p
[root@centos7-node1 v1]# echo "hello" > /data/volumes/v1/my.txt
[root@centos7-node1 ~]# docker run --name mybox -it -v /data/volumes/v1:/mydata busybox /bin/sh
[root@centos7-node1 ~]# docker rm mybox
mybox
[root@centos7-node1 ~]# ls /data/volumes/v1/
my.txt
容器间数据共享的案例
[root@centos7-node1 ~]# docker run --name box6 -it -d -v /data/volumes/v1:/mydata busybox /bin/sh
[root@centos7-node1 ~]# docker run --name box7 -it --rm --volumes-from box6 busybox
inspect数据显示
[root@centos7-node1 v1]# docker inspect box7 #查看容器,找出对应字段
[root@centos7-node1 v1]# docker inspect -f {{.Mounts}} box6 #.Mounts是顶级字段
[root@centos7-node1 v1]# docker inspect -f {{.NetworkSettings.Networks.bridge}} box7 #多级字段之间用.连接
-e MYSQL_ROOTPASSWORD=hello
[root@centos7-node1 ~]# mkdir /data/volumes/mysql
[root@centos7-node1 ~]# docker pull mysql:5.5
[root@centos7-node1 ~]# docker run --name db -d -v /data/volumes/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=qwer1234 mysql:5.5
[root@centos7-node1 ~]# docker exec -it db /bin/bash #进入数据库,创建用户以及数据库
root@38b54e3d76cf:/# mysql -uroot -p
mysql> create database wordpress charset utf8;
mysql> grant all on wordpress.* to 'wordpress'@'127.0.0.1' identified by 'wpss2020';
[root@centos7-node1 ~]# docker pull php:5.6-fpm
[root@centos7-node1 ~]# docker pull nginx:1.18-alpine
[root@centos7-node1 ~]#mkdir /data/volumes/nginx_conf -p #nginx配置文件目录
[root@centos7-node1 ~]# cat /data/volumes/nginx_conf/nginx_php.conf #定义好Nginx-php配置文件
server {
listen 80;
server_name localhost;
location / {
root /www/wordpress;
index index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/wordpress/$fastcgi_script_name;
include fastcgi_params;
}
}
[root@centos7-node1 ~]# docker run --name php-fpm -d -v /data/volumes/web:/www php:5.6-fpm #启动php
[root@centos7-node1 ~]# docker run --name nginx_php -d -p 80:80 -v /data/volumes/nginx_conf:/etc/nginx/conf.d --volumes-from php-fpm --link php-fpm:php nginx:1.18-alpine #启动nginx
[root@centos7-node1 ~]# cd /data/volumes/web && wget https://wordpress.org/latest.zip
root@centos7-node1 web]# unzip wordpress-5.4.1.zip
[root@centos7-node1 ~]# docker exec nginx_php nginx -s reload