V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yuzenan888
V2EX  ›  Docker

现在 Docker 重启容器之后为什么修改过的东西还在?

  •  
  •   yuzenan888 · 2018-04-02 16:07:33 +08:00 · 6262 次点击
    这是一个创建于 2446 天前的主题,其中的信息可能已经有所发展或是发生改变。

    去年中的时候玩过一段时间的 Docker,后来感觉对我没啥用就没完了。

    这几天想复习下,就又开始玩了。

    然后创建了一个容器:

    docker container run -it --name=test centos
    

    在里面创建一个空白文件:

    touch 111
    

    然后按 Ctrl+D 关闭容器。

    再开启容器:

    docker container start -a test
    

    然后查看文件:

    ls 111
    

    会发现这个文件还在……

    111
    

    为什么会这样,我记得以前我一重启容器里面的文件都会丢失的。

    附上 inspect,好像里面也没使用数据卷。

    [
        {
            "Id": "f149e9ffd626dcffc86f3c8325abf0a7c3ddbd62e1834f4db652aae58a992dd4",
            "Created": "2018-04-02T08:01:58.043257934Z",
            "Path": "/bin/bash",
            "Args": [],
            "State": {
                "Status": "exited",
                "Running": false,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 0,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2018-04-02T08:04:00.805282255Z",
                "FinishedAt": "2018-04-02T08:05:40.914734517Z"
            },
            "Image": "sha256:2d194b392dd16955847a14f969b2dd319251471ffa6356be6d8f16c5bf53db9b",
            "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/f149e9ffd626dcffc86f3c8325abf0a7c3ddbd62e1834f4db652aae58a992dd4/resolv.conf",
            "HostnamePath": "/mnt/sda1/var/lib/docker/containers/f149e9ffd626dcffc86f3c8325abf0a7c3ddbd62e1834f4db652aae58a992dd4/hostname",
            "HostsPath": "/mnt/sda1/var/lib/docker/containers/f149e9ffd626dcffc86f3c8325abf0a7c3ddbd62e1834f4db652aae58a992dd4/hosts",
            "LogPath": "/mnt/sda1/var/lib/docker/containers/f149e9ffd626dcffc86f3c8325abf0a7c3ddbd62e1834f4db652aae58a992dd4/f149e9ffd626dcffc86f3c8325abf0a7c3ddbd62e1834f4db652aae58a992dd4-json.log",
            "Name": "/test",
            "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,
                "Dns": [],
                "DnsOptions": [],
                "DnsSearch": [],
                "ExtraHosts": null,
                "GroupAdd": null,
                "IpcMode": "shareable",
                "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,
                "DiskQuota": 0,
                "KernelMemory": 0,
                "MemoryReservation": 0,
                "MemorySwap": 0,
                "MemorySwappiness": null,
                "OomKillDisable": false,
                "PidsLimit": 0,
                "Ulimits": null,
                "CpuCount": 0,
                "CpuPercent": 0,
                "IOMaximumIOps": 0,
                "IOMaximumBandwidth": 0
            },
            "GraphDriver": {
                "Data": {
                    "LowerDir": "/mnt/sda1/var/lib/docker/overlay2/3435bb5d5dbb430fcb6a9af122d0c5200de27131d5234aac3fc336c8d7070efd-init/diff:/mnt/sda1/var/lib/docker/overlay2/933b109e2e6219526f5393e8bca8dcadc22f592b41a5fcb8eadb52221eaf4838/diff",
                    "MergedDir": "/mnt/sda1/var/lib/docker/overlay2/3435bb5d5dbb430fcb6a9af122d0c5200de27131d5234aac3fc336c8d7070efd/merged",
                    "UpperDir": "/mnt/sda1/var/lib/docker/overlay2/3435bb5d5dbb430fcb6a9af122d0c5200de27131d5234aac3fc336c8d7070efd/diff",
                    "WorkDir": "/mnt/sda1/var/lib/docker/overlay2/3435bb5d5dbb430fcb6a9af122d0c5200de27131d5234aac3fc336c8d7070efd/work"
                },
                "Name": "overlay2"
            },
            "Mounts": [],
            "Config": {
                "Hostname": "f149e9ffd626",
                "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"
                ],
                "ArgsEscaped": true,
                "Image": "centos",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": null,
                "OnBuild": null,
                "Labels": {
                    "build-date": "20180302",
                    "license": "GPLv2",
                    "name": "CentOS Base Image",
                    "vendor": "CentOS"
                }
            },
            "NetworkSettings": {
                "Bridge": "",
                "SandboxID": "25cdd513903dff03fd8adfa4f8ebd1a61f75532eb23e493e23cb2350bda3de07",
                "HairpinMode": false,
                "LinkLocalIPv6Address": "",
                "LinkLocalIPv6PrefixLen": 0,
                "Ports": {},
                "SandboxKey": "/var/run/docker/netns/25cdd513903d",
                "SecondaryIPAddresses": null,
                "SecondaryIPv6Addresses": null,
                "EndpointID": "",
                "Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "IPAddress": "",
                "IPPrefixLen": 0,
                "IPv6Gateway": "",
                "MacAddress": "",
                "Networks": {
                    "bridge": {
                        "IPAMConfig": null,
                        "Links": null,
                        "Aliases": null,
                        "NetworkID": "a34d1f55d960138eedeaac53eb341429a1b69444abdeec387afe999c0c96b4f9",
                        "EndpointID": "",
                        "Gateway": "",
                        "IPAddress": "",
                        "IPPrefixLen": 0,
                        "IPv6Gateway": "",
                        "GlobalIPv6Address": "",
                        "GlobalIPv6PrefixLen": 0,
                        "MacAddress": "",
                        "DriverOpts": null
                    }
                }
            }
        }
    ]
    
    
    6 条回复    2018-04-03 11:47:32 +08:00
    halfcrazy
        1
    halfcrazy  
       2018-04-02 17:19:47 +08:00
    重新 run 一个才是新的,start 的还是之前的那个,所以文件容器内容还是有文件。
    rrfeng
        2
    rrfeng  
       2018-04-02 17:26:23 +08:00 via Android
    docker ls -a

    只是暂停,没有销毁
    Luckyray
        3
    Luckyray  
       2018-04-02 17:35:23 +08:00 via iPhone
    文件都在你宿主机上放着呐,不指定 volume 并不是说就不保存了。
    est
        4
    est  
       2018-04-02 17:39:10 +08:00
    container vs image 了解一下。
    vyronlee
        5
    vyronlee  
       2018-04-02 17:46:29 +08:00
    你需要的是 --rm,停止后自动销毁容器
    th00000
        6
    th00000  
       2018-04-03 11:47:32 +08:00
    执行 docker kill 再 docker rm 就没了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2652 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:41 · PVG 22:41 · LAX 06:41 · JFK 09:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.