docker 容器挂载技术

创建和使用桥接网络

docker network create  --driver bridge  --subnet 192.168.13.0/24  --gateway 192.168.13.1 mqynet

详情

[root@docker ~]# docker network inspect mqynet
[
    {
        "Name": "mqynet",
        "Id": "412a5fcb7d5b08cf80f4bec33f258b572dfd7d37f36b325a9a6426eb6a7518cf",
        "Created": "2022-04-20T19:11:51.020561175+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.13.0/24",
                    "Gateway": "192.168.13.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
[root@docker ~]# 

创建四个容器使之相互ping通

docker run -dit --name alpine1 --network mqynet alpine ash
docker run -dit --name alpine2 --network mqynet alpine ash
docker run -dit --name alpine3 --network mqynet alpine ash
docker run -dit --name alpine4 --network mqynet alpine ash

docker exec -it alpine1 ping alpine1
docker exec -it alpine1 ping alpine2
docker exec -it alpine1 ping alpine3
docker exec -it alpine1 ping alpine4

docker卷的使用

创建卷

[root@docker ~]# docker volume create supermqy
supermqy

查看卷

[root@docker ~]# docker volume inspect supermqy
[
    {
        "CreatedAt": "2022-05-04T16:10:26+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/supermqy/_data",
        "Name": "supermqy",
        "Options": {},
        "Scope": "local"
    }
]
[root@docker ~]# docker volume inspect supermqy --format '{{ .Mountpoint }}     {{ .Name}}'
/var/lib/docker/volumes/supermqy/_data     supermqy
[root@docker ~]# 

挂载卷

docker run -dit --name mqytest --mount source=supermqy,destination=/world centos /bin/bash

查看卷是否挂载成功

docker inspect fc83217fe0b873f9d14010f8f7c6502d607e
[root@docker ~]#

            "Type": "volume",
            "Name": "supermqy",
            "Source": "/var/lib/docker/volumes/supermqy/_data",
            "Destination": "/world",
            "Driver": "local",
            "Mode": "z",

先删容器再删卷

docker rm -f supermqy 
docker volume rm   fc83217fe0b873f9d14010f8f7c6502d607e

挂载主机目录

注:此实验中
--mount在目录不存在时会报错
-v 则会自动创建目录
一个更严谨一个更方便

docker run -dti --name mqydev --mount type=bind,source='/fox/target',target=/world centos

查看验证绑定

[root@docker fox]# docker inspect f68963bdf1ef63f3aec102a88919db --format='{{json .Mounts}}'
[{"Type":"bind","Source":"/fox/target","Destination":"/world","Mode":"","RW":true,"Propagation":"rprivate"}]
[root@docker fox]# 

删除

docker rm -f mqydev  

只读挂载

[root@docker fox]# docker run -dti --name mqydev --mount type=bind,source='/fox/target',target=/world,readonly centos
fa936c5bb9dca13217a35ed2c48c354e8ac90b1d6f58567e93aa326119e0af0c
[root@docker fox]# docker exec -ti mqydev  /bin/bash
[root@fa936c5bb9dc /]# 
[root@fa936c5bb9dc /]# 
[root@fa936c5bb9dc /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr  world
[root@fa936c5bb9dc /]# cd world/
[root@fa936c5bb9dc world]# ls
aaa
[root@fa936c5bb9dc world]# touch aa
touch: cannot touch 'aa': Read-only file system
[root@fa936c5bb9dc world]# 

测试容器填充卷

挂载填充卷

[root@docker fox]#  docker run -d --name=mqynginx  --mount source=nginx-vol,destination=/usr/share/nginx/html nginx
5965571a15cf46a86bb08aed1af654255a3ccd708417ab5317f0cb3d28b29d82

查看该卷详情

[root@docker fox]# docker volume inspect nginx-vol
[
    {
        "CreatedAt": "2022-05-04T16:39:44+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data",
        "Name": "nginx-vol",
        "Options": null,
        "Scope": "local"
    }
]
[root@docker fox]# 

查看该卷所在的目录内容

[root@docker fox]# ls -l /var/lib/docker/volumes//nginx-vol/_data/
total 8
-rw-r--r--. 1 root root 497 Dec 28 23:28 50x.html
-rw-r--r--. 1 root root 615 Dec 28 23:28 index.html
[root@docker fox]# 

容器里的目录映射到了主机里

启动另一个容器挂载该卷,以使用其中预先填充的内容

[root@docker fox]# docker run -dit --name mqytest3 --mount source=nginx-vol,destination=/world centos /bin/bash
1e2bd2ec6955fbf7f1dc79266355cd6042277f13224cc8236576853727a5d1a7
[root@docker fox]# 

查看容器中的挂载目录

[root@docker fox]# docker exec -it  mqytest3  ls /world 
50x.html  index.html

删除卷和容器

[root@docker fox]# x
5965571a15cf
fa936c5bb9dc
[root@docker fox]# docker volume rm -f  nginx-vol
nginx-vol
[root@docker fox]# 

x为函数  删除所有容器
x () 
{ 
    docker rm -f $(docker ps -aq)
}
[root@docker fox]# declare 

docker加速器

修改加速器:https://www.cnblogs.com/xiugeng/p/16043012.html#_label0_3

文件的遮盖挂载

[root@docker fox]# docker run -tid --name broken-test --mount type=bind,source=/tmp,target=/etc/yum.repos.d centos
39c3835b9d0d11bea9ce8605c995ce02697eeb8c83dedbae8239292ff37ccad4
[root@docker fox]# 
[root@docker fox]# 
[root@docker fox]# docker exec -it broken-test /bin/bash
[root@39c3835b9d0d /]# 
[root@39c3835b9d0d /]# 
[root@39c3835b9d0d /]# 
[root@39c3835b9d0d /]# cd /etc/yum.repos.d/
[root@39c3835b9d0d yum.repos.d]# ls
vmware-root_704-2990744159  vmware-root_713-4290166671
[root@39c3835b9d0d yum.repos.d]# cd

容器里原来的文件被挂载文件遮盖,取消挂载则会出现

posted @ 2022-05-04 18:42  supermao12  阅读(196)  评论(0编辑  收藏  举报