如何更改“docker create volume"的默认位置命令?

问题描述:

通过卷 API 创建卷时,即容器卷模式现在不再是最佳实践:

When creating volumes through the volume API, that is, as the container volume pattern is now not necessarily the best practice anymore:

# docker volume inspect test-data
[
    {
        "Name": "test-data",
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/test-data/_data"
    }
]

例如,我希望/data 中存在 docker 卷(安装在不同的物理卷中).

I would like to, for example, have docker volumes exist in /data (which is mounted in a different physical volume).

这对于符号链接是不可能的,可以通过绑定安装来实现,但是我想知道 Docker 中是否有一些配置可以更改每个单独卷的默认位置.

This is not possible to do with symbolic links, it is possible to do with bind mounts, but would I'm wondering if there is some configuration in Docker to change the default location for each separate volume.

您可以通过更改其中一个 启动参数 称为--data-root.

You can change where Docker stores its files including volumes by changing one of its startup parameters called --data-root.

如果您使用 systemd 进行服务管理,该文件通常位于 /lib/systemd/system/docker.service.像这样编辑文件:

If you're using systemd for service management, the file is usually located at /lib/systemd/system/docker.service. Edit the file as such:

# Old - taken from the generated docker.service file in Ubuntu 16.04's docker.io package
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS

# New
ExecStart=/usr/bin/dockerd --data-root /new_location/ -H fd:// $DOCKER_OPTS

或者,您可以编辑默认为 /etc/docker/daemon.json 的 Docker 守护程序配置文件.

Alternatively, you can edit the Docker daemon configuration file which defaults to /etc/docker/daemon.json.

重新启动 Docker 守护进程,您的卷将位于 /new_location/volumes/{volume_name}/_data

Restart the Docker daemon and your volumes will be under /new_location/volumes/{volume_name}/_data

注意:生产和本地都要小心!您还必须将现有数据从 /var/lib/docker/ 移动到新位置,以便 docker 安装按预期工作.

Note: be careful in production and also locally! You also have to move the existing data from /var/lib/docker/ to the new location for your docker install to work as expected.

如果您希望特定文件夹位于特定位置,您可以使用来自新位置的符号链接.

You can use symlinks from the new location if you want specific folders to be in specific place.