相关推荐recommended
Docker查看正在运行的容器目录
作者:mmseoamin日期:2024-01-24

起因

Docker查看正在运行的容器目录,在这里插入图片描述,第1张

为了方便操作服务器,大部分都是使用宝塔面板操作的,在宝塔中我发现面板中用来管理Docker的工具使用起来非常方便,如下图:

Docker查看正在运行的容器目录,第2张

可以看到宝塔的面板可以直接查看和从操作容器、Compose 、Compose 模板、镜像、网络、存储卷、仓库等,但是本文主要说说在管理容器的面板有一个操作 【目录】

Docker查看正在运行的容器目录,第3张

点击之后可以直接进入到容器的内部,查看目录地址发现其实就是服务器本地一个文件目录

Docker查看正在运行的容器目录,第4张

/var/lib/docker/overlay2/这个目录其实就是Docker容器挂载点,找到容器的挂载后就很方便了,以后复制宿主机文件到容器内部没必要使用命令了。


从宿主机传输文件夹到 Docker 中的命令

docker cp 需要传输的文件路径 docker名字:docker中放置文件的位置

示例:

# 将宿主机 ik 文件夹复制到名为 address_platform_pj 的容器中的 /usr/local/elasticsearch-7.3.0/plugins 路径下面
docker cp /tmp/ik address_platform_pj:/usr/local/elasticsearch-7.3.0/plugins

如何查看容器挂载点

上面介绍的是用宝塔面板可以直接看到容器挂载点,下面介绍如何使用命令找到容器的挂载点。

需要借助 docker inspect 命令

菜鸟原文

Docker查看正在运行的容器目录,第5张

格式

docker inspect 容器名称/容器ID

在Docker中,一个很重要的概念就是GraphDriver,它主要用于管理和维护镜像,包括把镜像从仓库下载下来,到运行时把镜像挂载起来可以被容器访问等,都是GraphDriver去完成的。

下面是我们运行的一个rabbitmq容器,我们可以看一下他的GraphDriver,注意这里用的docker存储驱动是overlay2

[root@iZbp1dg7mun11n29b6ndm6Z ~]# docker inspect rabbitmq
[
    {
        "Id": "75c688fade05cf116c219c98a3291681b31bd594bdee9e20ca4356de2be49d54",
        "Created": "2023-01-29T08:19:31.81602453Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "rabbitmq-server"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 29543,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-01-30T06:22:19.848560278Z",
            "FinishedAt": "2023-01-30T06:22:14.562177701Z"
        },
        "Image": "sha256:d445c0adc9a5596c50e5b73b8b636f79de24ad9da00671714aea768b3fa4ee9b",
        "ResolvConfPath": "/var/lib/docker/containers/75c688fade05cf116c219c98a3291681b31bd594bdee9e20ca4356de2be49d54/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/75c688fade05cf116c219c98a3291681b31bd594bdee9e20ca4356de2be49d54/hostname",
        "HostsPath": "/var/lib/docker/containers/75c688fade05cf116c219c98a3291681b31bd594bdee9e20ca4356de2be49d54/hosts",
        "LogPath": "/var/lib/docker/containers/75c688fade05cf116c219c98a3291681b31bd594bdee9e20ca4356de2be49d54/75c688fade05cf116c219c98a3291681b31bd594bdee9e20ca4356de2be49d54-json.log",
        "Name": "/rabbitmq",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "15672/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "15672"
                    }
                ],
                "5672/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "5672"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "always",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "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/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6-init/diff:/var/lib/docker/overlay2/d1f57dc40a37ea144eda52008ce77c6a204315fd463503ebb44d738f9def02d6/diff:/var/lib/docker/overlay2/94e59be20d43ea92c6db5d340e4e487aa48173f4c437ac301cf3020231ef53df/diff:/var/lib/docker/overlay2/91ebb4d307f75386fbfa2c870770cebba9f9cec0e88c3204758386bd7231b0f9/diff:/var/lib/docker/overlay2/26ae282efeabcfc1e1b0b487a2550d7ca1504048e58fd6a244835b9616b9b3af/diff:/var/lib/docker/overlay2/55d42966a32b217c6b97ac2b04fe56a8b1434538c78abda79a1fe01cef7d7570/diff:/var/lib/docker/overlay2/b37a30724d2e5cda683b3646dfdaa589661da410e29331fd5d0396eb9a5aaa29/diff:/var/lib/docker/overlay2/487d3efd72670cb9762b0d7abc407f152f5bf39a5777cf353fc325bd79acbf19/diff:/var/lib/docker/overlay2/9bbf703d2ce4a6d1d78cc443297250cca265a1a1204620eabe104358372b5e4a/diff:/var/lib/docker/overlay2/925b67bb37abd8a6d62c5188e66f0bd28d308d10274e13ccf88bb9d1449a1fb6/diff",
                "MergedDir": "/var/lib/docker/overlay2/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6/merged",
                "UpperDir": "/var/lib/docker/overlay2/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6/diff",
                "WorkDir": "/var/lib/docker/overlay2/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "ad29726ea314115baebd50cab42b12676e5074dd90b48a4a87dcda7be5da7849",
                "Source": "/var/lib/docker/volumes/ad29726ea314115baebd50cab42b12676e5074dd90b48a4a87dcda7be5da7849/_data",
                "Destination": "/var/lib/rabbitmq",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "rabbitmq",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "15672/tcp": {},
                "15691/tcp": {},
                "15692/tcp": {},
                "25672/tcp": {},
                "4369/tcp": {},
                "5671/tcp": {},
                "5672/tcp": {}
            },
            "Tty": false,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/opt/rabbitmq/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "OPENSSL_VERSION=1.1.1m",
                "OPENSSL_SOURCE_SHA256=f89199be8b23ca45fc7cb9f1d8d3ee67312318286ad030f5316aca6462db6c96",
                "OPENSSL_PGP_KEY_IDS=0x8657ABB260F056B1E5190839D9C4D26D0E604491 0x5B2545DAB21995F4088CEFAA36CEE4DEB00CFE33 0xED230BEC4D4F2518B9D7DF41F0DB4D21C1D35231 0xC1F33DD8CE1D4CC613AF14DA9195C48241FBF7DD 0x7953AC1FBC3DC8B3B292393ED5E9E43F7DF9EE8C 0xE5E52560DD91C556DDBDA5D02064C53641C25E5D",
                "OTP_VERSION=24.2",
                "OTP_SOURCE_SHA256=af0f1928dcd16cd5746feeca8325811865578bf1a110a443d353ea3e509e6d41",
                "RABBITMQ_DATA_DIR=/var/lib/rabbitmq",
                "RABBITMQ_VERSION=3.9.11",
                "RABBITMQ_PGP_KEY_ID=0x0A9AF2115F4687BD29803A206B73A36E6026DFCA",
                "RABBITMQ_HOME=/opt/rabbitmq",
                "RABBITMQ_LOGS=-",
                "HOME=/var/lib/rabbitmq",
                "LANG=C.UTF-8",
                "LANGUAGE=C.UTF-8",
                "LC_ALL=C.UTF-8"
            ],
            "Cmd": [
                "rabbitmq-server"
            ],
            "Image": "rabbitmq",
            "Volumes": {
                "/var/lib/rabbitmq": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "58608bd980cd569828f65ee1dbb3d86ece25313520d0a13b4d713c2207056d2b",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "15672/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "15672"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "15672"
                    }
                ],
                "15691/tcp": null,
                "15692/tcp": null,
                "25672/tcp": null,
                "4369/tcp": null,
                "5671/tcp": null,
                "5672/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5672"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "5672"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/58608bd980cd",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "6129ccc527280178c4be39392eae262820f3dde58bb79080292ff4e4d8df2ef3",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "de078644f985fd6525e1562f58f81aa149f6e8d6fc2ed67fa648493e078e41b1",
                    "EndpointID": "6129ccc527280178c4be39392eae262820f3dde58bb79080292ff4e4d8df2ef3",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

可以看到容器的元数据特别的多,这里我们使用grep命令来检索GraphDriver(图形驱动程序),只展示8行主要信息

docker inspect rabbitmq | grep GraphDriver -A 8

示例:

[root@iZbp1dg7mun11n29b6ndm6Z ~]# docker inspect rabbitmq | grep GraphDriver -A 8
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6-init/diff:/var/lib/docker/overlay2/d1f57dc40a37ea144eda52008ce77c6a204315fd463503ebb44d738f9def02d6/diff:/var/lib/docker/overlay2/94e59be20d43ea92c6db5d340e4e487aa48173f4c437ac301cf3020231ef53df/diff:/var/lib/docker/overlay2/91ebb4d307f75386fbfa2c870770cebba9f9cec0e88c3204758386bd7231b0f9/diff:/var/lib/docker/overlay2/26ae282efeabcfc1e1b0b487a2550d7ca1504048e58fd6a244835b9616b9b3af/diff:/var/lib/docker/overlay2/55d42966a32b217c6b97ac2b04fe56a8b1434538c78abda79a1fe01cef7d7570/diff:/var/lib/docker/overlay2/b37a30724d2e5cda683b3646dfdaa589661da410e29331fd5d0396eb9a5aaa29/diff:/var/lib/docker/overlay2/487d3efd72670cb9762b0d7abc407f152f5bf39a5777cf353fc325bd79acbf19/diff:/var/lib/docker/overlay2/9bbf703d2ce4a6d1d78cc443297250cca265a1a1204620eabe104358372b5e4a/diff:/var/lib/docker/overlay2/925b67bb37abd8a6d62c5188e66f0bd28d308d10274e13ccf88bb9d1449a1fb6/diff",
                "MergedDir": "/var/lib/docker/overlay2/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6/merged",
                "UpperDir": "/var/lib/docker/overlay2/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6/diff",
                "WorkDir": "/var/lib/docker/overlay2/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6/work"
            },
            "Name": "overlay2"
        },

通过查看容器详情的GraphDriver.Data,我们可以知道有下面几个重要目录

  • LowerDir:image 镜像层(镜像本身,只读)
  • UpperDir:容器的上层(读写)
  • MergedDir:容器的文件系统,使用Union FS(联合文件系统)将lowerdir 和upperdir 合并给容器使用
  • WorkDir:容器在 宿主机的工作目录

    所以,每一个容器运行的分层的文件系统,其实都可以在宿主机中找到对应的(通常在docker安装目录下/var/lib/docker/overlay2)

    而MergedDir就是我们要找的容器挂载点,我们可以直接进入到该目录下:

    [root@iZbp1dg7mun11n29b6ndm6Z lib]# cd /var/lib/docker/overlay2/346e18c0e8e1bc04f3be2d7c848dbedd25ddd63639ff41ce6e19c8936e711db6/merged
    [root@iZbp1dg7mun11n29b6ndm6Z merged]# ls
    bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  plugins  proc  root  run  sbin  srv  sys  tmp  usr  var
    

    高效传输文件到Docker容器

    既然知道了容器的挂载点,我们可以使用Xftp或者其它工具,直接将想要放到容器的文件丢在上面传输,方便快捷,一步到位!

    Docker查看正在运行的容器目录,第6张