使用 CLI 命令挂载 Docker 卷

使用 CLI 命令挂载 Docker 卷

我创建了两个卷,希望用来存储两个文件夹/etc/php/var/www容器内部的内容:

$ docker volume create dvwa_etcphp
$ docker volume create dvwa_www

我有一个容器,我使用以下命令运行它:

docker run --rm -it -p 80:80 vulnerables/web-dvwa --name dvwatest \
--mount type=volume,source=dvwa_www,target=/var/www \
--mount type=volume,source=dvwa_etcphp,tagret=/etc/php

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                NAMES
c700546a86b7        vulnerables/web-dvwa   "/main.sh --name dvw…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp   quirky_hawking

但是当我这样做时:

$ docker inspect c70

它给了我(我已经删除了所有在我看来多余的东西):

[
    {
        "Id": "c700546a86b74de5f2f941ee92fd72406e2a29e2e06ca85532658d1fa6ddbae5",
        "Created": "2020-01-22T13:36:23.976013357Z",
        "Path": "/main.sh",
        "Args": [
            "--name",
            "dvwatest",
            "--mount",
            "type=volume,source=dvwa_www,target=/var/www",
            "--mount",
            "type=volume,source=dvwa_etcphp,tagret=/etc/php"
        ],

        "Name": "/quirky_hawking",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "VolumeDriver": "",
            "VolumesFrom": null,
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "c700546a86b7",
            },
            "Cmd": [
                "--name",
                "dvwatest",
                "--mount",
                "type=volume,source=dvwa_www,target=/var/www",
                "--mount",
                "type=volume,source=dvwa_etcphp,tagret=/etc/php"
            ],
            "Image": "vulnerables/web-dvwa",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/main.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "[email protected]"
            }
        },
    }
]

我想使用卷将更改保存在两个文件夹中:/etc/php/var/www。当我停止容器并运行一个新容器时,我希望它使用这两个卷以及编辑后的配置文件。

Docker版本是19.03.2

谢谢你!

答案1

您可以尝试按照以下格式进行创建:

 docker run -it --mount type=volume,src=<VOLUME-NAME>,dst=<CONTAINER-PATH> --mount type=volume,src=<VOLUME-NAME>,dst=<CONTAINER-PATH> -p host_port:container_port --name myservice <IMAGE>

编辑:创建命令已编辑

以上对我有用:

docker run -ti --mount type=volume,src=cust_vol2,dst=/cust_vol2 --mount type=volume,src=cust_vol1,dst=/cust_vol1  -p 8024:8024  --name mycontainer centos

$docker inspect mycontainer
"Mounts": [
            {
                "Type": "volume",
                "Source": "cust_vol2",
                "Target": "/cust_vol2"
            },
            {
                "Type": "volume",
                "Source": "cust_vol1",
                "Target": "/cust_vol1"
            }
        ]

并在可能的情况下提供详细输出。

相关内容