#构建一个rxk/centos
[root@VM-0-15-centos home]# cd docker-test-volume/ [root@VM-0-15-centos docker-test-volume]# ls [root@VM-0-15-centos docker-test-volume]# pwd /home/docker-test-volume [root@VM-0-15-centos docker-test-volume]# [root@VM-0-15-centos docker-test-volume]# nano dockerfile1 #文件中的内容 [root@VM-0-15-centos docker-test-volume]# cat dockerfile1 FROM centos VOLUME ["volume01","volume02"] CMD echo "-----end-------" CMD /bin/bash [root@VM-0-15-centos docker-test-volume]# docker build -f dockerfile1 -t /rxk/centos . invalid argument "/rxk/centos" for "-t, --tag" flag: invalid reference format See 'docker build --help'. [root@VM-0-15-centos docker-test-volume]# docker build -f dockerfile1 -t rxk/centos . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 0d120b6ccaa8 Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in 634e59a80996 Removing intermediate container 634e59a80996 ---> 02a6fde9ba35 Step 3/4 : CMD echo "-----end-------" ---> Running in 3fe4feb278df Removing intermediate container 3fe4feb278df ---> d64085501c38 Step 4/4 : CMD /bin/bash ---> Running in 09d704b706fe Removing intermediate container 09d704b706fe ---> 7735159af50c Successfully built 7735159af50c Successfully tagged rxk/centos:latest
#查看当前镜像 [root@VM-0-15-centos docker-test-volume]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE rxk/centos latest 7735159af50c 5 minutes ago 215MB tomcat02 1.0 80b6126c75b9 7 hours ago 654MB nginx latest bc9a0695f571 8 days ago 133MB mysql 5.7 ae0658fdbad5 12 days ago 449MB tomcat 9.0 e0bd8b34b4ea 2 weeks ago 649MB tomcat latest e0bd8b34b4ea 2 weeks ago 649MB centos latest 0d120b6ccaa8 3 months ago 215MB portainer/portainer latest 62771b0b9b09 4 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 8 months ago 791MB #进入自己 [root@VM-0-15-centos docker-test-volume]# docker run -it 7735159af50c /bin/bash #可以看到挂载的两个volume卷 [root@d6463f570ce4 /]# ls -l total 56 lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x 5 root root 360 Dec 3 15:21 dev drwxr-xr-x 1 root root 4096 Dec 3 15:21 etc drwxr-xr-x 2 root root 4096 May 11 2019 home lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwx------ 2 root root 4096 Aug 9 21:40 lost+found drwxr-xr-x 2 root root 4096 May 11 2019 media drwxr-xr-x 2 root root 4096 May 11 2019 mnt drwxr-xr-x 2 root root 4096 May 11 2019 opt dr-xr-xr-x 91 root root 0 Dec 3 15:21 proc dr-xr-x--- 2 root root 4096 Aug 9 21:40 root drwxr-xr-x 11 root root 4096 Aug 9 21:40 run lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 May 11 2019 srv dr-xr-xr-x 13 root root 0 Dec 3 15:21 sys drwxrwxrwt 7 root root 4096 Aug 9 21:40 tmp drwxr-xr-x 12 root root 4096 Aug 9 21:40 usr drwxr-xr-x 20 root root 4096 Aug 9 21:40 var drwxr-xr-x 2 root root 4096 Dec 3 15:21 volume01 drwxr-xr-x 2 root root 4096 Dec 3 15:21 volume02
#这个卷Volume一定和外部有一个同步的目录 [root@d6463f570ce4 volume01]# touch container.txt [root@d6463f570ce4 volume01]# ls container.txt [root@d6463f570ce4 volume01]# [root@VM-0-15-centos docker-test-volume]# [root@VM-0-15-centos docker-test-volume]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d6463f570ce4 7735159af50c "/bin/bash" 7 minutes ago Up 7 minutes zen_faraday [root@VM-0-15-centos docker-test-volume]# docker inspect d6463f570ce4 [ { "Id": "d6463f570ce423c10082cce5414bf5ba70b8cb9f0ab47df998fde77b8e965881", "Created": "2020-12-03T15:21:34.913373526Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 15638, "ExitCode": 0, "Error": "", "StartedAt": "2020-12-03T15:21:35.202804066Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:7735159af50c4ff09863f0a97c37447043ac0eca6657913a4e33c65b0be4a24d", "ResolvConfPath": "/var/lib/docker/containers/d6463f570ce423c10082cce5414bf5ba70b8cb9f0ab47df998fde77b8e965881/resolv.conf", "HostnamePath": "/var/lib/docker/containers/d6463f570ce423c10082cce5414bf5ba70b8cb9f0ab47df998fde77b8e965881/hostname", "HostsPath": "/var/lib/docker/containers/d6463f570ce423c10082cce5414bf5ba70b8cb9f0ab47df998fde77b8e965881/hosts", "LogPath": "/var/lib/docker/containers/d6463f570ce423c10082cce5414bf5ba70b8cb9f0ab47df998fde77b8e965881/d6463f570ce423c10082cce5414bf5ba70b8cb9f0ab47df998fde77b8e965881-json.log", "Name": "/zen_faraday", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Capabilities": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/16864c6fe5942b10aff31e1156dbb08a055e31975f8725d25a205bd9f76b6f23-init/diff:/var/lib/docker/overlay2/9977c7654d8482400f51116d18f3c0562764ac2c1082c8de455e689c10593b30/diff", "MergedDir": "/var/lib/docker/overlay2/16864c6fe5942b10aff31e1156dbb08a055e31975f8725d25a205bd9f76b6f23/merged", "UpperDir": "/var/lib/docker/overlay2/16864c6fe5942b10aff31e1156dbb08a055e31975f8725d25a205bd9f76b6f23/diff", "WorkDir": "/var/lib/docker/overlay2/16864c6fe5942b10aff31e1156dbb08a055e31975f8725d25a205bd9f76b6f23/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "4bb7c13d437e16eb97bfcc71aa2faac18594ded837fcefc6d266e8ad36845c6a", "Source": "/var/lib/docker/volumes/4bb7c13d437e16eb97bfcc71aa2faac18594ded837fcefc6d266e8ad36845c6a/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "0eca9d146b2fd63f434657516305c20241da68936914059527e728865109e3f1", "Source": "/var/lib/docker/volumes/0eca9d146b2fd63f434657516305c20241da68936914059527e728865109e3f1/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "d6463f570ce4", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "Image": "7735159af50c", "Volumes": { "volume01": {}, "volume02": {} }, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20200809", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "cf151107d8fa56fd220695eed265b431618350222be583761b13354867477e39", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/cf151107d8fa", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "1c1bab6bce8a119f4731a2555a39256791d792539b260d2fd230aea73940d75b", "Gateway": "172.18.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.18.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:12:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "df77c62f666112b12946dbbbc16f1dcdbbcc8548e3fa56519fd062e5331a0c08", "EndpointID": "1c1bab6bce8a119f4731a2555a39256791d792539b260d2fd230aea73940d75b", "Gateway": "172.18.0.1", "IPAddress": "172.18.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:12:00:02", "DriverOpts": null } } } } ]
|